小型词典

来源:互联网 发布:活动噱头有哪些 知乎 编辑:程序博客网 时间:2024/04/27 18:13
我的程序:01./*  02.* 程序的版权和版本声明部分:  03.* Copyright (c) 2013, 烟台大学计算机学院  04.* All rights reserved.  05.* 文件名称:test.cpp  06.* 作    者:王鲁峰  07.* 完成日期:2013 年12月9日  08.* 版 本 号:v1.0  09.* 对任务及求解方法的描述部分: 10.* 输入描述:无  11.* 问题描述:小型词典12.* 程序输入:略  13.* 程序输出:略 14.* 算法设计:略  15.*/    #include<iostream>#include <fstream>//读取文件#include<cstdlib>#include<string>using namespace std;string e[8000],c[8000]; //英文和中文数组,要由文件中读入int wordsNum=0; //词库中实际的词汇条数int BinSeareh(int low, int high, string k);int main( ){  string key;      //查询关键词 ifstream infile("dictionary.txt",ios::in);  //以输入的方式打开文件 if(!infile) {  cerr<<"open error!"<<endl;  exit(1); } while (infile>>e[wordsNum]>>c[wordsNum])  //读取成功,则重复从文件中读 {  ++wordsNum; } infile.close();//()里面等待赋值 这一步的作用是让光标一直闪 等待下一步操作 //输入待查关键词并用二分查找法进行查询 do {  cout<<"请输入要查的词(end结束):";  cin>>key;  if (key=="end")   break;  else  {   int low=0,high=wordsNum-1;  //置当前查找区间上、下界的初值   int index=BinSeareh(low, high, key);   if (index == -1)    cout<<"查无此词!"<<endl<<endl;   else    cout<<key<<"的中文意思是:"<<c[index]<<endl<<endl;  } } while(1); cout<<"欢迎再次使用!"<<endl<<endl; return 0;}//二分查找,结果为所查词在数组中的下标int BinSeareh(int low, int high, string k){ int mid; while(low<=high) {  mid=(low + high) / 2;  if(e[mid]==k)  {   return mid; //查找成功返回  }  if(e[mid]>k)   high=mid-1; //继续在e[low..mid-1]中查找  else   low=mid+1; //继续在e[mid+1..high]中查找 } return -1; //当low>high时表示查找区间为空,查找失败 返回给 index}


 

运行结果:

心得体会:

          这个项目做起来太痛苦了,真不是那么好做的,不过做出来还是挺好的,一开始做这个项目,不是少这个就是少那个,参考老师的之后就明白了,总之还是挺高兴的。

原创粉丝点击