|
一、想法来源:(源于百度之星程序设计大赛)
题目描述:请编写程序,根据指定的对应关系,把一个文本中的字符串替换成另外的字符串。
输入数据:程序读入已被命名为text.txt和dict.txt的两个输入数据文本文件,text.txt为一个包含大量字符串(含中文)的文 本,以whitespace为分隔符;dict.txt为表示字符串(s1)与字符串(s2)的对应关系的另一个文本(含中文),大约在1万行左右,每行 两个字符串(即s1和s2),用一个\t或空格分隔。dict.txt中各行的s1没有排序,并有可能有重复,这时以最后出现的那次s1所对应的s2为 准。text.txt和dict.txt中的每个字符串都可能包含除whitespace之外的任何字符。text.txt中的字符串必须和 dict.txt中的某s1完全匹配才能被替换。(为便于调试,您可下载测试text.txt和dict.txt文件,实际运行时我们会使用不同内容的输 入文件。)
二、 主要功能:
1、翻译文档;
2、添加单词;
3、查找单词;
4、修改词典
三、思路:使用C++容器,map。Map是一类平衡二叉树,是一种关联容器。在插入、删除等具有较小的时间复杂度。
四、Map的用法
1、包含的头文件:
#include<map> 2、 创建map容器:
map<string,string> ditcmap; /*表示创建一个map类的容器,名称为dictmap。同时两个string表示的是一个对象所包含的两个属性是string类型。若是定义的类型为map<int,int>dictmap;则是表示为两个属性是int类型。*/ 3、 插入一条记录
dictmap.insert(make_pair(str1, str2)) ;//插入一条记录,包含两个属性为str1和str2 。
4、删除记录
dictmap.erase(str);//删除与str有关的记录,str表示的是属性1的关键字。 5、输出所有记录
map<string, string>::iterator it; //定义一个遍历器
for (it=dictmap.begin();it!=dictmap.end();it++)
{
cout<<it->first.c_str()<<"\t"<<it->second.c_str()<<endl;
}
//it=dictmap.begin(),表示从第一条记录开始;it!=dictmap.end(),如果没有结束,则是继续进行循环;it++,遍历器增加。
6、删除一条记录
map<string,string>::iterator mapIter;
if ((mapIter=dictmap.find(str))!=dictmap.end())
{
cout<<mapIter->first.c_str()<<"\t"<<mapIter->second.c_str()<<endl;
}
/*定义遍历器mapIter,dictmap.find(str),返回的是str所在的地址,赋值给mapIter,mapIter判断是不是在结尾,如果不是的话输出查找的内容。*/
五、总体框架
1、需要的几个变量
struct data
{
string EN;
string CN;
};
data DT; //存储字典记录。
int choice;
bool t=false;
string str1, str2,str3;
map<string, string> dictmap;
string str;
const char *p;
2、包含几个主要的函数
int Read(); //从文件中读取字典数据
void Menu(); //主界面菜单
void Do(); //选择菜单的操作
void Translate(); //翻译文件中的内容
void Add(); //在字典中增加记录
void Search(); //在字典中查找
void Change(); //在字典中替换记录
void Save(const char *p); //把字典保存在文件中
六、源代码
#pragma warning(disable : 4786)
#include <iostream>
#include <cstdlib>
#include <cctype>
#include <ctime>
#include <map>
#include <fstream>
#include <string>
using namespace std;
struct data
{
string EN;
string CN;
};
data DT;
int choice;
bool t=false;
string str1, str2,str3;
map<string, string> dictmap;
string str;
const char *p;
int Read();
void Menu();
void Do();
int main();
void Translate();
void Add();
void Search();
void Change();
void Save(const char *p);
int Read()
{
cout<<"请输入字典的的存储路径:"<<endl;
cin>>str;
p=str.c_str();
FILE *fp;
fp=fopen(p,"r");
char Line[1024];
char *ppos = NULL;
while ( fgets(Line, 1024, fp) != NULL )
{
if ( (ppos = strchr(Line, ' ')) == NULL
&& (ppos = strchr(Line, '\t')) == NULL )
{
continue;
}
*ppos++ = '\0';
if ( ppos[strlen(ppos) - 1] = '\n' )
{
ppos[strlen(ppos) - 1] = '\0';
}
DT.EN = Line;
DT.CN = ppos;
map<string, string>::iterator it;
if (( it=dictmap.find(DT.EN)) != dictmap.end() )
{
dictmap.erase(DT.EN);
}
dictmap.insert(make_pair(DT.EN, DT.CN));
}
map<string, string>::iterator it;
for (it=dictmap.begin();it!=dictmap.end();it++)
{
cout<<it->first.c_str()<<"****"<<it->second.c_str()<<endl;
}
system("pause");
fclose(fp);
return 0;
}
void Save(const char *p)
{
ofstream fout(p);
map<string,string>::iterator it;
for (it=dictmap.begin();it!=dictmap.end();it++)
{
fout<<it->first.c_str()<<" "<<it->second.c_str()<<endl;
}
fout.close();
cout<<"保存成功 !"<<endl;
}
void Add()
{
cout<<"请输入两个字符串,以空格分隔:"<<endl;
cin>>str1>>str2;
map<string,string>::iterator mapIter;
if ((mapIter=dictmap.find(str1))!=dictmap.end())
{
cout<<"此词已经存在!"<<endl;
}
else
{
dictmap.insert(make_pair(str1,str2));
t=true;
}
}
void Search()
{
cout<<"请输入需要查找的单词:"<<endl;
cin>>str1;
map<string,string>::iterator mapIter;
if ((mapIter=dictmap.find(str1))!=dictmap.end())
{
cout<<"\""<<str1<<"\""<<" 在字典中翻译为: "<<"\""<<mapIter->second.c_str()<<"\""<<endl;
}
else
{
cout<<"此词不存在字典中!"<<endl;
}
}
void Menu()
{
system("cls");
cout<<"\t================================================"<<endl;
cout<<"\t* *"<<endl;
cout<<"\t* 1、翻译文档 *"<<endl;
cout<<"\t* 2、添加单词 *"<<endl;
cout<<"\t* 3、查找单词 *"<<endl;
cout<<"\t* 4、修改字典 *"<<endl;
cout<<"\t* 0、退 出 *"<<endl;
cout<<"\t* *"<<endl;
cout<<"\t================================================"<<endl;
cout<<"请输入您的选项:";
cin>>choice;
while(choice<-1||choice>4)
{
cout<<"输入错误!"<<endl<<"请重新输入:"<<endl;
cin>>choice;
}
Do();
}
void Do()
{
char a;
switch(choice)
{
case 1:
system("cls");
Translate();
system("pause");
system("cls");
Menu();
break;
case 2:
system("cls");
Add();
system("pause");
system("cls");
Menu();
break;
case 3:
system("cls");
Search();
system("pause");
system("cls");
Menu();
break;
case 4:
system("cls");
Change();
system("pause");
system("cls");
Menu();
break;
case 0:
if (t)
{
cout<<"是否保存 (Y/N)?"<<endl;
cin>>a;
if(a=='Y')
{
Save(p);
}
}
exit(0);
break;
}
}
void Translate()
{
string str_1,str_2;
const char *p1,*p2;
cout<<"请输入翻译原文路径:"<<endl;
cin>>str_1;
p1=str_1.c_str();
cout<<"请输入译文存储路径:"<<endl;
cin>>str_2;
p2=str_2.c_str();
ifstream fin2(p1);
ofstream fout1(p2);
if (!fin2)
{
cout<<"打开翻译源文件失败 !"<<endl;
}
if (!fout1)
{
cout<<"创建译文文件失败 !"<<endl;
}
map<string, string>::iterator mapIter;
string strText;
while (fin2)
{
fin2>>strText;
if ( (mapIter = dictmap.find(strText)) != dictmap.end() )
{
fout1<<mapIter->second.c_str()<<"\t";
}
else
{
fout1<<strText.c_str()<<"\t";
}
}
cout<<"翻译成功 !"<<endl;
}
void Change()
{
string chgf,chgt;
cout<<"请输入您要替换的单词:"<<endl;
cin>>chgf;
map<string,string>::iterator mapIter;
if ((mapIter=dictmap.find(chgf))!=dictmap.end())
{
cout<<"\""<<chgf<<"\""<<" 在字典中翻译为: "<<"\""<<mapIter->second.c_str()<<"\""<<endl;
cout<<"请输入您所要翻译成的意思:";
cin>>chgt;
dictmap.erase(chgf);
dictmap.insert(make_pair(chgf, chgt));
cout<<"修改成功!"<<endl;
cout<<"\""<<chgf<<"\""<<" 在字典中翻译成为: "<<"\""<<chgt<<"\""<<endl;
t=true;
}
else
{
cout<<"此词不存在字典中!"<<endl;
return;
}
}
int main()
{
Read();
Menu();
return 1;
}
以上成果是小组的共同努力!
附:程序源文件 |