生活随笔
收集整理的這篇文章主要介紹了
项目-OOP版电子词典
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
問題及代碼:
/*
*Copyright (c) 2016,煙臺大學(xué)計(jì)算機(jī)學(xué)院
*All rights reserved.
*文件名稱:main.cpp
*作 者:李磊濤
*完成時(shí)間:2016年6月23日
*版 本 號:v1.0
*
*問題描述:項(xiàng)目-OOP版電子詞典。
*輸入描述:要查找單詞。
*程序輸出:漢語意思。
*/#include <fstream>
#include<iostream>
#include<string>
#include<cstdlib>
using namespace std;
class word
{
public:void set(string a,string b,string c);int compare(string);string getchinese();string getword_class();
private:string english;string chinese;string word_class;
};
void word::set(string a,string b,string c)
{
english=a;
chinese=b;
word_class=c;
}
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 sword(string k);
private:int BinSeareh(int low, int high, string k); int wordsNum; word words[8000]; //用于保存詞庫
};
Dictionary ::Dictionary()
{string a,b,c;wordsNum=0;ifstream infile("dictionary.txt",ios::in); //以輸入的方式打開文件 if(!infile) //測試是否成功打開 { cerr<<"dictionary open error!"<<endl; exit(1); } while (!infile.eof()){infile>>a>>b>>c;words[wordsNum].set(a,b,c);++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; //繼續(xù)在w[low..mid-1]中查找 else low=mid+1; //繼續(xù)在w[mid+1..high]中查找 } return -1; //當(dāng)low>high時(shí)表示查找區(qū)間為空,查找失敗
}
void Dictionary::sword(string k)
{ int low=0,high=wordsNum-1; //置當(dāng)前查找區(qū)間上、下界的初值 int index=BinSeareh(low, high, k); if(index>=0) cout<<k<<"--->"<<words[index].getword_class()+"\t"<<words[index].getchinese(); else cout<<"查無此詞"; cout<<endl<<endl;
}
int main( )
{ Dictionary dict; string key; do { cout<<"請輸入待查詢的關(guān)鍵詞(英文),0000結(jié)束:"<<endl; cin>>key; if (key!="0000") { dict.sword(key); } } while(key!="0000"); cout<<"歡迎再次使用!"<<endl<<endl; return 0;
}
運(yùn)行結(jié)果:
知識點(diǎn)總結(jié):
通過該程序,強(qiáng)化了我對簡單程序結(jié)構(gòu)的認(rèn)識。
學(xué)習(xí)心得:
期間有很多小錯(cuò)誤,要繼續(xù)寫程序爭取早日掌握C++。
問題及代碼:
運(yùn)行結(jié)果:
知識點(diǎn)總結(jié):
通過該程序,強(qiáng)化了我對簡單程序結(jié)構(gòu)的認(rèn)識。
學(xué)習(xí)心得:
期間有很多小錯(cuò)誤,要繼續(xù)寫程序爭取早日掌握C++。
總結(jié)
以上是生活随笔為你收集整理的项目-OOP版电子词典的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。