模板生成系统

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector> 
#include<string>
#include<queue>
#include<map>
using namespace std;
string temp[105];
map<string,string> mp;
int main()
{
    int n,m;
    cin>>m>>n;
    getchar();    //吸收一个换行 
    for(int i=0;i<m;i++)
    {
        getline(cin,temp[i]);
    }
    string name,value,str;
    for(int i=0;i<n;i++)
    {
        getline(cin,str);
        for(int j=0;j<str.length();j++)
        {
            if(str[j]==' ')
            {
                name = str.substr(0,j);
                value = str.substr(j+2,str.length()-j-3);
                break;
            }
        }
        mp[name] = value;
        //cout<<name<<" "<<mp[name]<<endl;
    }
    for(int k=0;k<m;k++)
    {
        for(int i=0;i<temp[k].length();i++)
        {
            if(i+1<temp[k].length() &&temp[k][i] == '{' && temp[k][i+1] == '{' )
            {
                int j,t;
                string nm; 
                for(j=i+2;j<temp[k].length();j++)
                {
                    if(j+1<temp[k].length() &&temp[k][j] == '}' && temp[k][j+1] == '}' )
                    {
                        nm = temp[k].substr(i+2,j-1-i-2+1);
                        t = j+1;
                        break;
                    }
                }
                for(j=0;j<nm.length();j++)        //去首部空格 ,只考虑首尾有空格的情况 
                    if(nm[j]!=' ')
                        break;
                int end;
                for(end = nm.length()-1;end>=j;end--)         //去尾部
                     if(nm[end]!=' ')
                         break;
                if(nm[j]>='0' && nm[j]<='9')    
                {
                    cout<<temp[k][i];
                    continue;            //变量名不合法,当作非特殊标记处理 
                }
                int begin = j;
                for(j;j<=end;j++)        
                {
                    if((nm[j]>='0' && nm[j]<='9')||(nm[j]>='a' && nm[j]<='z') || (nm[j]>='A' && nm[j]<='Z') || nm[j]=='_')        
                         continue;
                    else
                        break;
                }
                if(j != end+1)    
                {
                    cout<<temp[k][i];
                    continue;            //变量名不合法,当作非特殊标记处理 
                }
                i = t;    //合法 
                nm = nm.substr(begin,end-begin+1);
                
                if(mp[nm] == "")
                    cout<<"";    
                else
                    cout<<mp[nm]; 
            }
            else
                cout<<temp[k][i];
        }
        cout<<endl;
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/fzuhyj/p/10516776.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值