第十四周项目之oppo 电子词典
2015-06-06 13:47
411 查看
/* * Copyright (c) 2014, 烟台大学计算机学院 * All rights reserved. * 文件名称:test.cpp * 作 者:刘佳琦 * 完成日期:2015年 6 月 6日 * 版 本 号:v1.0 * * 问题描述:编程序,由用户输入英文词,显示词性和中文释义 * 输入描述:查询的某个单词 * 程序输出:查出的单词 */ #include <fstream> #include<iostream> #include<cstdlib> #include<string> using namespace std; string e[8000],c[8000],w[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]>>w[wordsNum]) //读取成功,则重复从文件中读 { ++wordsNum; } infile.close(); //输入待查关键词并用二分查找法进行查询 do { cout<<"请输入要查的词(0000结束):"; cin>>key; if (key=="0000") 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]<<" 词性为"<<w[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时表示查找区间为空,查找失败 }
运行结果:
学习心得:
由于期末考在二分法上吃过亏,因此记忆比较深刻,这个项目算上这次大概是第三次了,比之前是熟练了很多。
相关文章推荐
- OpenStack command-line interface cheat sheet
- 基于日志的Openstack的故障监控
- Linux 下鼠标对应的文件
- linux-进程监控
- Win7下安装Hadoop
- php-wamp滴定仪网站的根目录
- 用函数fopen_s打开数据文件
- 大型网站技术架构
- 学习docker
- shell变量详解
- OpenCms 9.5.1 新建一个站点
- Centos的安装
- Tomcat学习之Wrapper
- 《Unix环境高级编程第3版》apue.3e在Linux上无法安装的问题
- window nginx 启动无提示错误,却没有listen 80端口
- ecshop、织梦、discuz、wordpress比较
- TopCoder SRM 660 Div2 Problem 500 - PrivateD2party (贪心)
- 21讲项目实战CMS-wordpress主题网站模板制作
- 30讲项目实战:网站面包屑导航+表单功能开发
- 28讲项目实战:网站搜索功能+单页面开发