/*
*copyright (c)2014,烟台大学计算机学院
*All rights reserved
*文件名称:789.cpp
*作者:孙春红
*完成日期:2014年2月12日
*版本号:v1.0
*
*问题描述:做一个简单的电子词典,输入英语,输出他的汉语和词性。
*程序输入: 输入要查找的英语单词;
*程序输出:输出英语的汉语和词性。
*/
#include <fstream> //操作文件必写
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
struct Word
{
string english;
string chinese;
string word_class;
};
int main( )
{
Word words[8000];
int wordsNum=0;
string key;
//以输入的方式(ios::in)打开文件
ifstream infile("dictionary.txt",ios::in); //测试是否成功打开,打开失败时(如要读的数据文件不存在)退出
if(!infile)
{
cerr<<"open error!"<<endl;
exit(1);
}
while (!infile.eof())
{
infile>>words[wordsNum].english;
infile>>words[wordsNum].chinese;
infile>>words[wordsNum].word_class;
++wordsNum;
}
infile.close(); //读入完毕一定要关闭文件
cout<<"请输入您想要查找的单词(0000结束):"<<endl;
while (cin>>key&&key!="0000") //一直输入,直到输入0000
{
int low=0,high=wordsNum-1,mid,f1,f2;
while(low<=high)
{
mid=(low+high)/2;
if(words[mid].english==key)
{
f1=1;
f2=mid;
break;
}
else
{
if(words[mid].english>key)
high=mid-1;
else
low=mid+1;
}
}
if(f1==0)
{
cout<<"对不起,查无此词。"<<endl;
}
if(f1==1)
{
cout<<key<<"-->"<<words[f2].word_class<<"-->"<<words[f2].chinese<<endl;
f1--;
}
}
return 0;
}
第17周项目电子词典结构体版
最新推荐文章于 2023-04-27 20:27:53 发布