C++ Primer课后练习11.33-11.36

//练习11.33
#include
  
  
   
   
#include
   
   
    
    
#include
    
    
#include
     
     
      
      
#include
      
      
        #include 
       
         using namespace std; //函数buildmap读入给定文件,建立起转换映射 map 
        
          buildmap(ifstream & mapfile) { map 
         
           trans_map; //保持转换规则 string key; //要转换的单词 string value; //替换后的内容 //读取第一个单词存入key中,行中剩余内容存入value; while (mapfile >> key && getline(mapfile, value)) { if (value.size() > 1) { trans_map[key] = value.substr(1); //跳过空格那个当 } else throw runtime_error("no rule for" + key); } return trans_map; } const string &Transform(const string & s, const map 
          
            & m) { //实际的转换工作,此部分是程序核心 auto map_it = m.find(s); //如果单词在转换规则map中 if (map_it != m.end()) { return map_it->second; //使用替换短语 } else return s; //否则返回原string } //单词转换程序 void word_transform(ifstream & mapfile, ifstream & input) { auto trans_map = buildmap(mapfile); //保存转换规则 string text; //保存输入中的每一行 while (getline(input, text)) //读取一行输入 { istringstream stream(text); //读取每一个单词 string word; bool firstword = true; //控制是否打印空格 while (stream >> word) { if (firstword) firstword = false; else cout << " "; //在单词间打印一个空格 cout << Transform(word, trans_map); //打印输出 } cout << endl; } } //生成转换文本 int main(void) { ifstream map_file("rule.txt"); ifstream in_put("target.txt"); word_transform(map_file, in_put); return 0; } /**********************************************************************************************************************/ 
           
          
         
        
      
     
     
   
   
  
  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值