2015年大一下第13周项目3-OOP版电子词典
2015-06-09 20:49
344 查看
/* *Copyright (c) 2014,烟台大学计算机学院 *All rights reserved. *文件名称:Annpion.cpp *作者:王耀鹏 *完成日期:2015年6月10日 *版本号:v1.0 * *问题描述:做一个简单的电子词典。在文件dictionary.txt中,保存的是英汉对照的一个词典,词汇量近8000个,英文、中文释义与词性间用’\t’隔开。 *输入描述:英语单词。 *输出描述:单词的词性和中文释义。 */ #include <iostream> #include <cstring> #include <fstream> #include <cstdlib> using namespace std; class Dictionary; class Word { public: friend class Dictionary; private: string english; string chinese; string word_class; }; class Dictionary { public: Dictionary (); void searchwords(string word); private: Word words [8000]; int wordsNum; }; Dictionary::Dictionary() { wordsNum=0; ifstream infile("dictionary.txt",ios::in); if(!infile) { cerr<<"The file can't open!"<<endl; exit(1); } while(!infile.eof()) { infile>>words[wordsNum].english>>words[wordsNum].chinese>>words[wordsNum].word_class; ++wordsNum; } infile.close(); } void Dictionary::searchwords(string word) { int high=wordsNum-1,low=0,key=-1,mid; while(high>=low) { mid=(high+low)/2; if(word==words[mid].english) { key=mid; break; } if(word>words[mid].english) low=mid+1; else high=mid-1; } if(key>=0) cout<<word<<"--->"<<words[key].word_class<<'\t'<<words[key].chinese<<endl; else cout<<"查无此词!"<<endl; } int main() { Dictionary dict; string word; do { cout<<"请输入待查询的关键词(英文),0000结束:"<<endl; cin>>word; if (word!="0000") { dict.searchwords(word); } } while(word!="0000"); cout<<"欢迎再次使用!"<<endl<<endl; return 0; }
运行结果:
相关文章推荐
- shell计算器while循环控制
- all about the linux swap space
- 第七章:%type&标签&if&while&loop
- Centos更新源
- Citrix VDI 跨平台桌面虚拟化架构的研究
- Plexim Plecs.Standalone.v3.6.7 for Win32_64 .Linux32_64电力电子电路系统模拟软件
- Linux进程间通信--使用信号量
- Linux grep工具应用实例 精简配置文件 从系统管理命令输出中查找
- Linux下的内核测试工具——perf使用简介
- nginx学习(一)
- 关于Linux Kernel select epoll
- 编译安装linux内核模块
- linux命令总结(三)
- linux命令总结(二)
- spark之hadoop2.6环境搭建笔记
- openwrt gstreamer实例学习笔记(六. gstreamer Pads及其功能)
- linux xshell 支持图形界面
- 安装rlwrap for linux
- Linux 内核调试之 printk
- 30个实例详解TOP命令