生活随笔
收集整理的這篇文章主要介紹了
项目-OOP版电子词典
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
問題及代碼:
/*
*Copyright (c) 2016,煙臺大學計算機學院
*All rights reserved.
*文件名稱:main.cpp
*作 者:李磊濤
*完成時間:2016年6月23日
*版 本 號:v1.0
*
*問題描述:項目-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; //繼續在w[low..mid-1]中查找 else low=mid+1; //繼續在w[mid+1..high]中查找 } return -1; //當low>high時表示查找區間為空,查找失敗
}
void Dictionary::sword(string k)
{ int low=0,high=wordsNum-1; //置當前查找區間上、下界的初值 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<<"請輸入待查詢的關鍵詞(英文),0000結束:"<<endl; cin>>key; if (key!="0000") { dict.sword(key); } } while(key!="0000"); cout<<"歡迎再次使用!"<<endl<<endl; return 0;
}
運行結果:
知識點總結:
通過該程序,強化了我對簡單程序結構的認識。
學習心得:
期間有很多小錯誤,要繼續寫程序爭取早日掌握C++。
問題及代碼:
運行結果:
知識點總結:
通過該程序,強化了我對簡單程序結構的認識。
學習心得:
期間有很多小錯誤,要繼續寫程序爭取早日掌握C++。
總結
以上是生活随笔為你收集整理的项目-OOP版电子词典的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。