第17周项目7-电子词典结构体版

来源:互联网 发布:python 替换字符串 编辑:程序博客网 时间:2024/06/07 17:23
/* *Copyright (c) 2014, 烟台大学计算机学院 *All rights reserved. *文件名称:week17-project7.cpp *作者:高赞 *完成日期:2014年 12 月 22 日 *版本号:v1.0 * */#include <fstream>#include <iostream>#include <iomanip>#include <cstdlib>#include <cstring>using namespace std;int main( ){    struct Word    {        string english;        string wordclass;        string chinese;    } w[8000];    int i=0,sum=0;    string a,b,c,word;    ifstream infile("dictionary.txt",ios::in);    if(!infile)    {        cerr<<"未找到该文件!"<<endl;        exit(1);    }    while (infile >> a >> b >>c)    {        w[i].english=a;        w[i].chinese=b;        w[i].wordclass=c;        ++i;        ++sum;    }    infile.close();    do    {        int middle, low=0,high=sum;        cout << "输入要查询的单词:" << endl;        cin >> word;        getchar();        middle=(low+high)/2;        while(word!=w[middle].english)        {            if(word>w[middle].english)            {                low=middle+1;                middle=(low+high)/2;            }            else            {                high=middle-1;                middle=(low+high)/2;            }            if (low>high)            {                cout << "对不起,查无此词。" << endl;                break;            }        }        if(low<=high)            cout << w[middle].wordclass <<'\t' << w[middle].chinese << endl;        cout << endl             << "回车键继续或其他任意键退出..." << endl             << endl;    }    while (getchar()=='\n');    return 0;}


 

0 0