十六周任务三电子词典

来源:互联网 发布:c语言知识点总结 编辑:程序博客网 时间:2024/05/27 20:45
/* (程序头部注释开始)* 程序的版权和版本声明部分* Copyright (c) 2011, 烟台大学计算机学院学生 * All rights reserved.* 文件名称:   十六周任务三电子词典                           * 作    者:石丽君                              * 完成日期:     2012    年     6  月      5  日* 版 本 号:          * 对任务及求解方法的描述部分* 输入描述: 电子词典做一个简单的电子词典。在文件dictionary.txt 中,保存的是英汉对照的一个词典,词汇量近8000 个,英文、中文释义与词性间用’\t’隔开。建一个表示词条的类Word,Word 类的一个对象可以描述一个词,类对象数组可以存储词库。将文件中的内容读到对象数组中,由用户输入英文词,显示中文释义。提示:文件中的词汇已经排序,故在查找时,用二分查找法提高效率。拓展1(选做):允许用户运行程序后,连续地查词典,直到输入”0000”结束。拓展2(选做):试着做一个窗口版的电子词典。拓展3(选做):使这个词典,读入一篇文章,输出其中的所有名词(或动词,或全部实词)。——搜索引擎用类似的功能,用于筛去虚词,因为并不是所有词都值得索引。* 问题描述: * 程序输出: * 程序头部的注释结束*/#include<iostream>#include<fstream>#include<string>using namespace std;class Word{public:Word(string e,string c,string t):english(e),chinese(c),type(t){}Word();friend void search_word(string a,Word word[]);friend void get_frome_file(Word word[]);private:string english;string chinese;string type;};Word::Word(){english="0000";chinese="0000";type="0000";}void search_word(string a,Word word[]){int height=8000,low=0,mid;while(a!="0000"){mid=(height+low)/2;if(a==word[mid].english){cout<<"中文意思:"<<word[mid].chinese<<'\t'<<"词性:"<<word[mid].type<<endl;break;}else{if(a<word[mid].english)height=mid-1;elselow=mid+1;}if(low>height){cout<<"查无此词!"<<endl;break;}}}void get_frome_file(Word word[]){ifstream infile("dictionary.txt",ios::in);if(!infile){cerr<<"open error!"<<endl;exit(1);}for(int i=0;i<8000;i++){infile>>word[i].english>>word[i].chinese >>word[i].type ;}infile.close();}int main(){Word word[8000];string b;get_frome_file( word);while(1){cout<<"请输入要查找的单词,按0000结束"<<endl;cin>>b;if(b=="0000") {cout<<"欢迎再次使用"<<endl;break;}search_word( b, word);}system("pause");return 0;}

原创粉丝点击