* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2012, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:电子词典。
* 程序的版权和版本声明部分
* Copyright (c) 2012, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:电子词典。
* 作 者: 晁阳
* 完成日期: 2012 年 06 月 05 日
* 版 本 号: t1.0
* 对任务及求解方法的描述部分
* 输入描述:电子词典
* 问题描述: 做一个简单的电子词典,由用户输入英文单词,显示中文意思。
* 程序头部的注释结束
上机感言: 说是文件的输入输出流其实大部分还是类的运用
#include <fstream>
#include<iostream>
#include<string>
using namespace std;
class Word
{
public:
void set(string e,string c,string wc);
int compare(string);
void display();
private:
string english;
string chinese;
string word_class;
};
void Word::set(string e,string c,string wc)
{
english = e;
chinese = c;
word_class = wc;
}
void Word::display()
{
cout<<english<<'\t'<<word_class<<'\t'<<chinese<<endl<<endl;
}
int Word::compare(string k)
{
return english.compare(k);
}
int BinSearch(int low,int high,Word *w,string k);
int main()
{
Word words[8000];
string e,c,wc;
string key;
int wordsNum=0;
ifstream infile("dictionary.txt",ios::in);
if(!infile)
{
cout<<"open error!"<<endl;
exit(1);
}
while (!infile.eof())
{
infile>>e>>c>>wc;
words[wordsNum].set(e,c,wc);
++wordsNum;
}
do
{
cout<<"请输入待查询的关键词(英文):"<<endl;
cin>>key;
if (key!="0000")
{
int low=0,high=wordsNum-1;
int index=BinSearch(low, high, words, key);
if (index == -1)
cout<<"查无此词!"<<endl<<endl;
else
words[index].display();
}
}
while(key!="0000");
cout<<"欢迎再次使用!"<<endl<<endl;
system("pause");
return 0;
}
int BinSearch(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;
}