第十七周项目电子词典结构体版

来源:互联网 发布:无缝贴图软件中文版 编辑:程序博客网 时间:2024/05/22 12:26
<span style="color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15.555556297302246px; line-height: 35px;">做一个简单的电子词典。在文件dictionary.txt中,保存的是英汉对照的一个词典,词汇量近8000个,英文、中文释义与词性间用’\t’隔开。</span><br style="color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15.555556297302246px; line-height: 35px;" /><span style="color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15.555556297302246px; line-height: 35px;">编程序,由用户输入英文词,显示词性和中文释义。</span>
 /*  *Copyright (c) 2014, 烟台大学计算机学院  * All right reserved.  * 文件名称:test.cpp    * 作者:赵嵩  * 完成时间:2014年12月21号  * 版本号:v1.0  */ #include <iostream>#include <string>#include <fstream>#include<cstdlib>using namespace std;int binary_search(string key,int n);struct Word{    string english;    string chinese;    string word_class;};Word word[8000];int main(){    int wordsNum=0;    string key;    int tem;    //打开文件    ifstream infile("dictionary.txt",ios::in);    if (!infile)    {        cout<<"打开文件失败!";        exit(1);    }    while (infile>>word[wordsNum].english)    {        infile>>word[wordsNum].chinese;        infile>>word[wordsNum].word_class;        wordsNum++;    }    infile.close();  //关闭文件    cout<<"欢迎使用本词典 (0000)退出"<<endl;    while (1)    {        cin>>key;        if(key=="0000")            break;        tem=binary_search(key,wordsNum);        if (tem==-1)        {            cout<<"└─────查无此词"<<endl;            continue;        }        else            cout<<"└─────"<<word[tem].word_class<<word[tem].chinese<<endl;    }}int binary_search(string key,int n)  //二分法查找{    int i=-1;    int low=0,high=n-1,mid;    while (low<=high)    {        mid=(low+high)/2;        if (word[mid].english==key)        {            return mid;        }        else if (word[mid].english>key)            high=mid-1;        else            low=mid+1;    }    return i;}
运行结果:
<img src="http://img.blog.csdn.net/20141221205353530?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvenM5NTI4/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
0 0
原创粉丝点击