zoukankan      html  css  js  c++  java
  • C++primer 练习11.33:实现你自己版本的单词转换程序

    // 11_33.cpp : 定义控制台应用程序的入口点。
    //
    
    #include "stdafx.h"
    #include<iostream>
    #include<string>
    #include<map>
    #include<sstream>
    #include<fstream>
    using namespace std;
    //用map文件来建立一个要转换的字符串对应的转换成的字符串
    map<string, string> buildMap(ifstream &map_file)
    {
        map<string, string> ma;
        string word;
        string convert;
        while (map_file >> word&&getline(map_file, convert))
        {
            ma[word] = convert.substr(1);
        }
        return ma;
    }
    
    //返回一个字符串在map中对应的字符串,如果没有,则返回字符串本身
    const string& transform(const string &s, const map<string, string> &m)
    {
        auto ite = m.find(s);
        if (ite != m.end())
            return ite->second;
        else return s;
    }
    
    //单词转换函数,输入一个转换文件,和一个输入文件,打印转换后的文本
    void word_transform(ifstream &map_file, ifstream &input)
    {
        map<string, string> ma = buildMap(map_file);
        string line, word;
        while (getline(input,line))
        {
            istringstream is(line);
            bool wordtag = true;
            while (is >> word)
            {
                if (wordtag)
                    wordtag = false;
                else cout << " ";
                cout << transform(word, ma);
            }
            cout << endl;
        }
    }
    
    int main()
    {
        word_transform(ifstream("D:\file\11.3.6_map.txt"), ifstream("D:\file\11.3.6_input.txt"));
        return 0;
    }
  • 相关阅读:
    汉文博士——支持生僻古难字检索的开放式免费汉语词典
    delphi 实现接口 Unsatisfied forward or external declaration
    注册服务程序
    递归的使用
    NTLDR is missing 错误处理方法
    测试
    常见数据类型的存储结构
    多维分析
    showmodule
    pos函数
  • 原文地址:https://www.cnblogs.com/csudanli/p/5356232.html
Copyright © 2011-2022 走看看