• 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;
        }
    
    }
    
  • 相关阅读:
    Maven 打war包
    linuxan安装redis出现Newer version of jemalloc required错误
    Linux常用命令
    Linux重启和关机命令
    CentOS 端口和防火墙操作
    修改root密码
    Web登录验证之 Shiro
    No WebApplicationContext found: no ContextLoaderListener registered
    java写文件实现换行
    gcc命令详解
  • 原文地址:https://www.cnblogs.com/amonqsq/p/13577159.html
Copyright © 2020-2023  润新知