/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.* 文件名称:派生类的继承
* 作 者: 郭岩岩
* 完成日期:2012 年6月 6日
* 版 本 号: vc.1
* 对任务及求解方法的描述部分 :词典
* 输入描述:
* 问题描述:
* 程序输出:
*程序头部的注释结束
*/
#include<fstream>
#include<iostream>
#include<string>
using namespace std;
class Word
{
public:
void set_word(string c,string e,string t);
void disply();
int compare(string k);
private:
string chinese;
string english;
string type;
};
int search(int low,int high,Word *w,string key);
void Word::set_word (string c,string e,string t)
{
chinese=c;
english=e;
type=t;
}
void Word::disply()
{
cout<<chinese<<" "<<english<<" "<<type<<endl;
}
int Word::compare (string k)
{
return english.compare(k);
}
int search(int low,int high,Word *w,string k)
{
int mid;
while (low<=high)
{
mid=(low+high)/2;
if(w[mid].compare (k)==0)
{
return mid;
}
if(w[mid].compare (k)>0)
{
high=mid-1;
}
else
low=mid+1;
}
return -1;
}
int main()
{
Word words[8000];
string c,e,t;
string key;
int wordsnum=0;
ifstream infile("dictionary.txt",ios::in);
if(!infile)
{
cerr<<"open error!"<<endl;
exit(1);
}
while (!infile.eof())
{
infile>>e>>c>>t;
words[wordsnum].set_word(e, c, t);
++wordsnum;
}
do
{
cout<<"请输入要查询的单词:";
cin>>key;
if(key!="0000")
{
int low=0,high=wordsnum-1;
int index=search(low, high, words, key);
if(index==-1)
cout<<"查无此词!"<<endl;
else
words[index].disply ();
}
}
while(key!="0000");
cout<<"欢迎使用,退出请按“0000”!"<<endl;
system("pause");
return 0;
}
上机感言:愁死我啦,老是查无此词,有点想把电脑砸了的冲动!