201509-3 模板生成系统

实现

#include <iostream>
#include <vector>
#include <string>
#include <map>

using namespace std;

vector<string> html_lines;
map<string,string> vars_val;

int main() {
    int html_line_num, var_line_num;
    cin >> html_line_num >> var_line_num;
    cin.ignore();

    for (int i = 0;i < html_line_num;++i) {
        string html_line;
        getline(cin,html_line);
        html_lines.push_back(html_line);
    }

    for (int i = 0;i < var_line_num;++i) {
        string var;
        getline(cin,var);
        int size = var.size();
        int space_pos = var.find(" ");
        vars_val[var.substr(0,space_pos)] 
            = var.substr(space_pos + 2, size - space_pos - 3);
    }

    for (int i = 0;i < html_line_num;++i) {
        string html_line = html_lines[i];
        int cur_left_pos = 0;
        int cur_right_pos = 0;
        while ((cur_left_pos = html_line.find("{{",cur_left_pos) ) != -1
        && (cur_right_pos = html_line.find("}}",cur_left_pos) )!= -1) {
            string var_key 
            = html_line.substr(cur_left_pos + 3, cur_right_pos - cur_left_pos - 4);
            if (vars_val.find(var_key) != vars_val.end()) {
                string contend = vars_val[var_key];
                html_line.replace(cur_left_pos,cur_right_pos + 2 - cur_left_pos,contend);
                cur_left_pos = cur_left_pos + contend.size();
            } else {
                html_line.replace(cur_left_pos,cur_right_pos + 2 - cur_left_pos,"");
            }
        }
        cout << html_line << endl;
    }

}
posted @ 2020-08-28 14:11  amonqsq  阅读(82)  评论(0编辑  收藏  举报