第十六周 任务三
/*
* 程序头部注释开始
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:电子词典
* 作 者:薛广晨
* 完成日期:2011 年 06 月 03 日
* 版 本号:x1.0
* 对任务及求解方法的描述部分
* 输入描述:文件dictionary.txt
* 问题描述:做一个简单的电子词典。在文件dictionary.txt 中,保存的是英汉对照的一个词典,词汇量近8000 个,
英文、中文释义与词性间用’\t’隔开。建一个表示词条的类Word,Word 类的一个对象可以描述一个词,
类对象数组可以存储词库。将文件中的内容读到对象数组中,由用户输入英文词,显示中文释义。
允许用户运行程序后,连续地查词典,直到输入”0000”结束
* 程序头部的注释结束
*/
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
class Word
{
private:
string english;
string chinese;
string cx;
public:
Word();
Word(string english, string chinese, string cx);
string get_English();
string get_Chinese();
string get_Cx();
void set_English(string english);
void set_Chinese(string chinese);
void set_Cx(string cx);
friend void ouput(Word w[]);
friend void seek(Word w[]);
};
int main()
{
Word word[8000];
ouput(word);
seek(word);
return 0;
}
Word::Word()
{
this->english = "0000";
this->chinese = "0000";
this->cx = "0000";
}
Word::Word(string english, string chinese, string cx)
{
this->english = english;
this->chinese = chinese;
this->cx = cx;
}
string Word::get_English()
{
return english;
}
string Word::get_Chinese()
{
return chinese;
}
string Word::get_Cx()
{
return cx;
}
void Word::set_English(string english)
{
this->english = english;
}
void Word::set_Chinese(string chinese)
{
this->chinese = chinese;
}
void Word::set_Cx(string cx)
{
this->cx = cx;
}
void ouput(Word w[])
{
int i;
string English;
string Chinese;
string Cx;
ifstream infile("dictionary.txt",ios::in);
if(!infile)
{
cerr<<"open error!"<<endl;
exit(1);
}
for(i=0;i<8000;i++)
{
infile>>English>>Chinese>>Cx;
w[i].set_English(English);
w[i].set_Chinese(Chinese);
w[i].set_Cx(Cx);
}
infile.close();
}
void seek(Word w[])
{
string cha;
int low, high, mid;
do
{
cout<<"请输入要查的词(0000结束):";
cin>>cha;
if(cha=="0000")break;
low=0,high=8000-1;
while(low<=high)
{
mid=(low+high)/2;
if(w[mid].get_English()==cha)
{
cout<<"“"<<w[mid].get_English()<<"”"<<"中文意思是 : "<<w[mid].get_Chinese() << " " << "词性是:" <<w[mid].get_Cx()<<endl<<endl;
break;
}
else
{
if(w[mid].get_English()>cha) high=mid-1;
else low=mid+1;
}
if(low>high) cout<<"查无此词"<<endl<<endl;
}
}while(1);
}
上机感言:有这代码查词就容易了,很好用哦,用自己的代码查词就是比用电子词典感觉爽啊。
还没有评论,来说两句吧...