第十六周项目3电子词典
生活随笔
收集整理的這篇文章主要介紹了
第十六周项目3电子词典
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
/*Copyright (c) 2016,煙臺(tái)大學(xué)計(jì)算機(jī)學(xué)院
*All rights reserved.
*文件名稱 :
*作 者 : 李釗
*完成日期 : 2016年6月14號(hào)
*版 本 號(hào) : v1.0
*
*問(wèn)題描述 : 電子詞典
*輸入描述 : 要查詢的單詞
*程序輸出 : 單詞的中文意思
*/
#include <fstream>
#include<iostream>
#include<string>
#include<cstdlib>
using namespace std;
class Word
{
public:void set(string e, string c, string wc);int compare(string);string getChinese();string getWord_class();
private:string english;string chinese;string word_class;
};void Word::set(string e, string c, string wc)
{english=e;chinese=c;word_class=wc;
}int Word::compare(string k)
{return english.compare(k);
}string Word::getChinese()
{return chinese;
}string Word::getWord_class()
{return word_class;
}class Dictionary
{
public:Dictionary();void searchWord(string k);
private:int BinSeareh(int low, int high, string k);int wordsNum;Word words[8000];
};Dictionary::Dictionary()
{string e,c,wc;wordsNum=0;ifstream infile("dictionary.txt",ios::in);if(!infile){cerr<<"dictionary open error!"<<endl;exit(1);}while (!infile.eof()){infile>>e>>c>>wc;words[wordsNum].set(e, c, wc);++wordsNum;}infile.close();
}int Dictionary::BinSeareh(int low, int high, string key)
{int mid;while(low<=high){mid=(low + high) / 2;if(words[mid].compare(key)==0){return mid;}if(words[mid].compare(key)>0)high=mid-1;elselow=mid+1;}return -1;
}
void Dictionary::searchWord(string key)
{int low=0,high=wordsNum-1;int index=BinSeareh(low, high, key);if(index>=0)cout<<key<<": "<<words[index].getWord_class()+"\t"<<words[index].getChinese();elsecout<<"查無(wú)此詞";cout<<endl<<endl;
}int main( )
{Dictionary dict;string key;do{cout<<"請(qǐng)輸入要翻譯的英文單詞:(esc退出)"<<endl;cin>>key;if (key!="esc"){dict.searchWord(key);}}while(key!="esc");cout<<"退出程序"<<endl<<endl;return 0;
}
運(yùn)行結(jié)果:
總結(jié)
以上是生活随笔為你收集整理的第十六周项目3电子词典的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: linux mysql max_allo
- 下一篇: 荐书丨10 本豆瓣高分技术图书,强烈推荐