第16周实验报告任务3

/* (程序头部注释开始)  
* 程序的版权和版本声明部分 
 
* Copyright (c) 2011, 烟台大学计算机学院学生  
 
* All rights reserved.* 文件名称:派生类的继承
* 作 者: 郭岩岩 

* 完成日期:2012 年6月 6日 

* 版 本 号: vc.1 

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

* 输入描述:  

* 问题描述:  

* 程序输出:  

*程序头部的注释结束 

*/  






#include<fstream>
#include<iostream>
#include<string>
using namespace std;

class Word
{
public:
	void set_word(string c,string e,string t);
	void disply();
	int compare(string k);
private:
	string chinese;
	string english;
	string type;
};
int search(int low,int high,Word *w,string key);
void Word::set_word (string c,string e,string t)
{
	chinese=c;
	english=e;
	type=t;
}
void Word::disply() 
{
	cout<<chinese<<"  "<<english<<"  "<<type<<endl;
}
int Word::compare (string k)
{
	return english.compare(k);
}
int search(int low,int high,Word *w,string k)
{
	int mid;
	while (low<=high)
	{
		mid=(low+high)/2;
		if(w[mid].compare (k)==0)
		{
			return mid;
		}
		if(w[mid].compare (k)>0)
		{
			high=mid-1;
		}
		else
			low=mid+1;
	}
	
	return -1;
	}
int main()
{
	Word words[8000];
	string c,e,t;
	string key;
	int wordsnum=0;
	ifstream infile("dictionary.txt",ios::in);  
	if(!infile)       
	{  
		cerr<<"open error!"<<endl;  
		exit(1);  
	}  
	while (!infile.eof())  
	{  
		infile>>e>>c>>t;  
		words[wordsnum].set_word(e, c, t);  
		++wordsnum;  
	}
	do
	{
		cout<<"请输入要查询的单词:";
		cin>>key;
		if(key!="0000")
		{
			int low=0,high=wordsnum-1;
			int index=search(low, high, words, key); 
			if(index==-1)
				cout<<"查无此词!"<<endl;
			else
				words[index].disply ();
		}
	}
	while(key!="0000");
	cout<<"欢迎使用,退出请按“0000”!"<<endl;
	system("pause");  
	return 0;
}

上机感言:愁死我啦,老是查无此词,有点想把电脑砸了的冲动!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值