在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
代码如下// 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 }
1.关于main函数中的三个参数 ,ref: int main(int argc,char* argv[])详解
2.读写文件,ref:C++文件读写详解(ofstream,ifstream,fstream)
3. getlineref:cin 输入空格符和 getline() 忽略开头换行符 C++中cin、cin.get()、cin.getline()、getline()、gets()等函数的用法 用cmd执行argv[1]是exe的转义字符路径,argv[2]是 rules文件的转义字符路径,argv[3]是待转换文件的转义字符路径。
|
请发表评论