词典

来源:互联网 发布:论文公司企业数据 编辑:程序博客网 时间:2024/06/10 14:57
/*  烟台大学计算机学院  作者:任子仪  日期:2013年12月10日  问题描述:  样例输入:  样例输出:  问题分析:*/#include <fstream>#include<iostream>#include<cstdlib>#include<string>using namespace std;string e[8000],c[8000]; //英文和中文数组,要由文件中读入int wordsNum=0; //词库中实际的词汇条数int BinSeareh(int low, int high, string k);int main( ){ string key;      //查询关键词//将文件中的数据读入到对象数组中ifstream infile("dictionary.txt",ios::in);  //以输入的方式打开文件if(!infile)       //测试是否成功打开{cerr<<"open error!"<<endl;exit(1);}while (infile>>e[wordsNum]>>c[wordsNum])  //读取成功,则重复从文件中读{++wordsNum;}infile.close();//输入待查关键词并用二分查找法进行查询do{cout<<"请输入要查的词(0000结束):";cin>>key;if (key=="0000")break;else{int low=0,high=wordsNum-1;  //置当前查找区间上、下界的初值int index=BinSeareh(low, high, key);if (index == -1)cout<<"查无此词!"<<endl<<endl;elsecout<<key<<"的中文意思是:"<<c[index]<<endl<<endl;}}while(1);cout<<"欢迎再次使用!"<<endl<<endl;return 0;}//二分查找,结果为所查词在数组中的下标int BinSeareh(int low, int high, string k){int mid;while(low<=high){mid=(low + high) / 2;if(e[mid]==k){return mid; //查找成功返回}if(e[mid]>k)high=mid-1; //继续在e[low..mid-1]中查找elselow=mid+1; //继续在e[mid+1..high]中查找}return -1; //当low>high时表示查找区间为空,查找失败}


示例图片:

心得体会:原本想太难了,不做了吧。。。后来,一上课,发现好多人都做出来了。这节课因为只有一个项目可以做,做完后有时间就做了下,当然参考了下老师的答案,发现听好玩的。。。。。加油哦。。。以后不能因为难就不做了啊。只有这种题才能锻炼人的啊

0 0