/*
*程序的版权和版本声明部分:
*Copyright(c)2014,烟台大学计算机学院学生
*All rights reserved.
*文件名称:
*作者:田成琳
*完成日期:2014 年 6 月 3 日
*版本号:v1.0
*对任务及求解方法的描述部分:
*输入描述: 文件导入单词
*问题描述:输出文件导入单词对应的意思
*程序输出:单词意思
*问题分析:
*算法设计:
*/
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
using namespace std;
class Word
{
public:
Word() {}
string getEnglish();
string getChinese();
string getWord_Class();
friend istream & operator >>(istream &in,Word &w);
friend ostream & operator <<(ostream &out,Word &w);
private:
string english;
string chinese;
string word_class;
};
class Dictionary
{
public:
Dictionary();//导入所有单词组成词典
string search(string keyword);
private:
Word words[8000];
int wordsum;
};
Dictionary::Dictionary()
{
wordsum=0;
ifstream infile ("D:\\dictionary.txt",ios::in);
if(!infile)
{
cerr<<"Open Error!"<<endl;
exit(1);
}
while(!infile.eof())
infile>>words[wordsum++];
infile.close();
}
string Dictionary::search(string keyword)
{
int head=0,end=wordsum,middle=(head+end)/2;
while(head<end&&words[middle].getEnglish()!=keyword)
{
if(words[middle].getEnglish()<keyword)
head=middle+1;
if(words[middle].getEnglish()>keyword)
end=middle-1;
middle=(head+end)/2;
}
if(words[middle].getEnglish()==keyword)
return words[middle].getChinese()+words[middle].getWord_Class();
else
return "查无此词";
}
string Word::getEnglish()
{
return english;
}
string Word::getChinese()
{
return chinese;
}
string Word::getWord_Class()
{
return word_class;
}
istream & operator >>(istream &in,Word &w)
{
in>>w.english>>w.chinese>>w.word_class;
return in;
}
ostream & operator <<(ostream &out,Word &w)
{
out<<w.getChinese()<<"\t"<<w.getWord_Class()<<endl;
return out;
}
int main()
{
Dictionary dic;
string english;
ifstream inFile ("D:\\aboutcpp.txt",ios::in);
if(!inFile)
{
cerr<<"Open Error!"<<endl;
exit(1);
}
while(inFile>>english&&!inFile.eof())
cout<<english<<"<----------"<<dic.search(english)<<endl;
inFile.close();
return 0;
}
运行结果:
心得体会: