首页 > 微信 > 详细

C++ 编写的解码器小程序 map

时间:2019-02-19 18:29:23      阅读:316      评论:0      收藏:0      [点我收藏+]

 c++ prime 5 ex11_4

代码如下

// ex11_4_word_transform.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include <map>
#include <vector>
#include <iostream>
#include <fstream>
#include <string>
#include <stdexcept>
#include <sstream>

using std::map; using std::string; using std::vector;
using std::ifstream; using std::cout; using std::endl;
using std::getline;
using std::runtime_error; using std::istringstream;

map<string, string> buildMap(ifstream &map_file)
{
    map<string, string> trans_map;   // holds the transformations
    string key;    // a word to transform
    string value;  // phrase to use instead
                   // read the first word into key and the rest of the line into value
    while (map_file >> key && getline(map_file, value))
        if (value.size() > 1) // check that there is a transformation
            trans_map[key] = value.substr(1); // skip leading space 
        else
            throw runtime_error("no rule for " + key);
    return trans_map;
}

const string &
transform(const string &s, const map<string, string> &m)
{
    // the actual map work; this part is the heart of the program
    auto map_it = m.find(s);
    // if this word is in the transformation map
    if (map_it != m.cend())
        return map_it->second; // use the replacement word
    else
        return s;              // otherwise return the original unchanged
}

// first argument is the transformations file; 
// second is file to transform
void word_transform(ifstream &map_file, ifstream &input)
{
    auto trans_map = buildMap(map_file); // store the transformations

                                         // for debugging purposes print the map after its built
    cout << "Here is our transformation map: \n\n";
    for (auto entry : trans_map)
        cout << "key: " << entry.first
        << "\tvalue: " << entry.second << endl;
    cout << "\n\n";

    // do the transformation of the given text 
    string text;                    // hold each line from the input
    while (getline(input, text)) {  // read a line of input
        istringstream stream(text); // read each word 
        string word;
        bool firstword = true;      // controls whether a space is printed 
        while (stream >> word) {
            if (firstword)
                firstword = false;
            else
                cout << " ";  // print a space between words
                              // transform returns its first argument or its transformation 
            cout << transform(word, trans_map); // print the output 
        }
        cout << endl;        // done with this line of input
    }
}

int main(int argc, char **argv)
{
    // open and check both files
    //if (argc != 3)
    //    throw runtime_error("wrong number of arguments");

    argv[1] = "……";
    argv[2] = "……";
    ifstream map_file(argv[1]); // open transformation file 
    if (!map_file)              // check that open succeeded
        throw runtime_error("no transformation file");

    ifstream input(argv[2]);    // open file of text to transform
    if (!input)                 // check that open succeeded
        throw runtime_error("no input file");

    word_transform(map_file, input);

    system("pause");

    return 0;  // exiting main will automatically close the files
}

 

技术分享图片

 

关于main函数中的三个参数 ,ref: int main(int argc,char* argv[])详解 

读写文件,ref:C++文件读写详解(ofstream,ifstream,fstream)

 

 


用cmd执行

argv[1]是exe的转义字符路径,argv[2]是 rules文件的转义字符路径,argv[3]是待转换文件的转义字符路径。

 

 技术分享图片

 

C++ 编写的解码器小程序 map

原文:https://www.cnblogs.com/lightmare/p/10402775.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!