您的位置:首页 > 运维架构

第十四周项目3-OOP版电子词典

2015-06-08 15:20 399 查看
/*
*Copyright (c) 2014, 烟台大学计算机学院
*All rights reserved.
*文件名称:week14-3.cpp
*作者:高赞
*完成日期: 2015 年 6 月 8 日
*版本号:v1.0
*
*/
#include <iostream>
#include <cstring>
#include <fstream>
#include <cstdlib>
using namespace std;
class Word
{
private:
string english;
string chinese;
string wordclass;
public:
Word() {}
void getWord(string a,string b,string c)
{
english=a;
chinese=b;
wordclass=c;
}
string getenglish()
{
return english;
}
string getchinese()
{
return chinese;
}
string getwordclass()
{
return wordclass;
}
};

int main()
{
string word,a,b,c;
Word w[8000];
int i=0,sum=0;
ifstream infile("dictionary.txt",ios::in);
if(!infile)
{
cerr<<"未找到该文件!"<<endl;
exit(1);
}
while (!infile.eof())
{
infile>>a>>b>>c;
w[i].getWord(a,b,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].getenglish())
{
if(word>w[middle].getenglish())
{
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].getwordclass() <<" " <<w[middle].getchinese() << endl;
cout << endl
<< "回车键继续或其他任意键退出..." << endl
<< endl;
}
while (getchar()=='\n');

return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: