您的位置:首页 > 其它

第十六周实验报告3

2012-06-04 18:28 501 查看
/* (程序头部注释开始)

* 程序的版权和版本声明部分

* Copyright (c) 2011, 烟台大学计算机学院学生 

* All rights reserved.

* 文件名称:

* 作    者:         赵桐辉                     

* 完成日期:     2012    年 06     月4  日

* 版 本 号:         

* 对任务及求解方法的描述部分

* 输入描述: 

* 问题描述: 

* 程序输出: 

* 程序头部的注释结束

*/
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;
int min(int a,int b);
class Word
{
public:
Word();
Word(string word,string word_meaning,string word_type);
void set_word(string word);
void set_word_meaning(string word_meaning);
void set_word_type(string word_type);
string get_word();
string get_word_meaning();
string get_word_type();
//friend ostream& operator << (ostream&,Word&); //重载流插入运算符“<<”  ;*/
private:
string word;
string word_meaning;
string word_type;
};
void input_word(Word  s[]);
Word search(string word,Word w1[]);
int main()
{
Word w1[8000],w2;
bool key=1;
string word;
input_word(w1);
while(key==1)
{
cout<<"请输入您要查找的单词:";
cin>>word;
w2=search(word,w1);
cout<<"您要查找单词的意思为:"<<w2.get_word_meaning()<<'\t'<<w2.get_word_type()<<endl<<endl;
cout<<"若想继续查词请按1,结束查词请按0";
cin>>key;
cout<<endl;
}
system("PAUSE");
return 0;
}
int min(int a,int b)
{
if(a>b)
a=b;
return a;
}
Word::Word()
{
word="unknow";
word_meaning="unknow";
word_type="unknow";
}
Word::Word(string word,string word_meaning,string word_type)
{
this->word=word;
this->word_meaning=word_meaning;
this->word_type=word_type;
}

void input_word(Word s[])
{
int i=0;
string word;
string word_meaning;
string word_type;
ifstream infile("dictionary.txt",ios::in);
if (!infile)
{
cerr<<"open error!"<<endl;
exit(1);
}
for (i=0;i<8000;i++)
{
infile>>word;
s[i].set_word(word);
infile>>word_meaning;
s[i].set_word_meaning(word_meaning);
infile>>word_type;
s[i].set_word_type(word_type);
}
infile.close();
//cout<<endl;

}
void Word::set_word(string word)
{
this->word=word;
}
void Word::set_word_meaning(string word_meaning)
{
this->word_meaning=word_meaning;
}
void Word::set_word_type(string word_type)
{
this->word_type=word_type;
}
string Word::get_word()
{
return word;
}
string Word::get_word_meaning()
{
return word_meaning;
}
string Word::get_word_type()
{
return word_type;
}
Word search(string word,Word w1[])
{
Word w2;
int i=0;
int a=0,b=0,c;
w2.set_word(word);
string s1,s2,s3,s4;
int low=0,high=7962,mid;
s1=w1[low].get_word();
s2=w1[high].get_word();
s4=w2.get_word();
mid=(low+high)/2;
s3=w1[mid].get_word();
while(low<=high)
{
a=word.size();
b=s3.size();
c=min(a,b);
if(s3==word)
return w1[mid];
else if(s3>word)
{
high=mid-1;
}
else
{
low=mid+1;
}
mid=(low+high)/2;
s1=w1[low].get_word();
s2=w1[high].get_word();
s3=w1[mid].get_word();
}
cerr<<"很遗憾,查无此词!";
exit(1);
return w2;
}


结果:



 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息