c++ 单词转换程序
单词转换文件内容:em them
cuz because
gratz grateful
i I
nah no
pos suppose
sez said
tanx thanks
wuz was
需要转换的文件内容:nah i sez tanx cuz i wuz pos to
not cuz i wuz gratz
程序输出的结果:no I said thanks beause I was supposed to
not beause I was grateful
程序:
//一个翻译程序
#include<iostream>
#include<string>
#include<map>
#include<utility>
#include<stdexcept>
#include<fstream>
#include<sstream>
using namespace std;
//测试打开文件是否成功的函数;
ifstream& open_file(ifstream &in,const string &file)
{
in.close();
in.clear();
in.open(file.c_str());
return in;
}
int main(int argc,char **argv)
{
map<string,string> trans_map;//trans_map存储单词转换文件
string key,value;
if(argc!=3)
throw runtime_error("wrong numbers of arguments");
ifstream map_file;
//测试文件是否打开成功
if(!open_file(map_file,argv[1]));
throw runtime_error("no transformation file");
//如果打开成功,将单词转换文件的内容存入map容器对象
while(map_file>>key>>value)
trans_map.insert(make_pair(key,value));
ifstream input;
//打开需要转换的文件
if(!open_file(input,argv[2]))
throw runtime_error("no input file");
string line;
//按行读入到string对象
while(getline(input,line)){
//关联到字符串流
istringstream stream(line);
string word;
bool firstword=true;
//对一行里面的所有单词进行检测,测试是否需要转换
while(stream>>word)
{
map<string,string>::const_iterator map_it=trans_map.find(word);
if(map_it!=trans_map.end())
word=map_it->second;
if(firstword)
firstword==0;
else
cout<<" ";
//输出单词
cout<<word;
}
cout<<endl;
}
return 0;
}
我的问题: 在linux下怎么采用命令行的方式将两个文件读入,我用了下./10-17 map_t.txt map.txt(10-17是我生成的可执行文件,map_t.txt是单词转换文件,map.txt是学要转换的文件)
我的执行结果:
[root@localhost 10]# ./10-17 map_t.txt map.txt
terminate called after throwing an instance of 'std::runtime_error'
what(): no transformation file
已放弃 (core dumped)
[root@localhost 10]#
cuz because
gratz grateful
i I
nah no
pos suppose
sez said
tanx thanks
wuz was
需要转换的文件内容:nah i sez tanx cuz i wuz pos to
not cuz i wuz gratz
程序输出的结果:no I said thanks beause I was supposed to
not beause I was grateful
程序:
//一个翻译程序
#include<iostream>
#include<string>
#include<map>
#include<utility>
#include<stdexcept>
#include<fstream>
#include<sstream>
using namespace std;
//测试打开文件是否成功的函数;
ifstream& open_file(ifstream &in,const string &file)
{
in.close();
in.clear();
in.open(file.c_str());
return in;
}
int main(int argc,char **argv)
{
map<string,string> trans_map;//trans_map存储单词转换文件
string key,value;
if(argc!=3)
throw runtime_error("wrong numbers of arguments");
ifstream map_file;
//测试文件是否打开成功
if(!open_file(map_file,argv[1]));
throw runtime_error("no transformation file");
//如果打开成功,将单词转换文件的内容存入map容器对象
while(map_file>>key>>value)
trans_map.insert(make_pair(key,value));
ifstream input;
//打开需要转换的文件
if(!open_file(input,argv[2]))
throw runtime_error("no input file");
string line;
//按行读入到string对象
while(getline(input,line)){
//关联到字符串流
istringstream stream(line);
string word;
bool firstword=true;
//对一行里面的所有单词进行检测,测试是否需要转换
while(stream>>word)
{
map<string,string>::const_iterator map_it=trans_map.find(word);
if(map_it!=trans_map.end())
word=map_it->second;
if(firstword)
firstword==0;
else
cout<<" ";
//输出单词
cout<<word;
}
cout<<endl;
}
return 0;
}
我的问题: 在linux下怎么采用命令行的方式将两个文件读入,我用了下./10-17 map_t.txt map.txt(10-17是我生成的可执行文件,map_t.txt是单词转换文件,map.txt是学要转换的文件)
我的执行结果:
[root@localhost 10]# ./10-17 map_t.txt map.txt
terminate called after throwing an instance of 'std::runtime_error'
what(): no transformation file
已放弃 (core dumped)
[root@localhost 10]#
...全文
相关推荐
简单C++程序
简单的C++应用,适用于初学者,有需要的就下载吧
noip-2011-c++题解.docx
NOIP的系列试题答案资源,整理合集,有2009~2017的试题与答案,需要资源的同鞋么们可以下载哦
cpp代码-语料字典1
cpp代码-语料字典1
用c++实现将文本每个单词首字母转换为大写
利用C++,读入一个英文文本,并将文本中的每个英文单词首字母大写。本程序练习了从一个文本中读入流,用到了fstream流。 在文本的转换过程中,用到了isalpha()——是否是字母,toupper()——转换为大写字符 这两个函数(针对string对象的单个字符的操作。类似的操作还有isalnum()——是否是字母或数字,iscntrl()——是否是控制字符, isdigit()——是否是数字,isgraph()——是否不是空格,但可以打印,islower()——是否是小写字母, isprint()——是否是可以打印的字符,ispunct()——是否是标点符号,isspace()——是否是空
单词的词法分析设计程序
通过设计、编制、调试一个具体的词法分析程序,加深对词法分析原理的理解,并掌握在对程序设计语言源程序进行扫描过程中将其分解为各类单词的词法分析方法。


