第14周-输入输出流,文本文件-项目3-OOP版电子词典
来源:互联网 发布:淘宝怎么看宝贝权重 编辑:程序博客网 时间:2024/04/30 12:12
/* * Copyright (c) 2014, 烟台大学计算机学院 * All rights reserved. * 文件名称:test.cpp * 作 者:刘畅* 完成日期:2015 年 6 月 9 日 * 版 本 号:v1.0 * * 问题描述:做一个简单的电子词典。在文件dictionary.txt中,保存的是英汉对照的一个词典,词汇量近8000个,英文、中文释义与词性间用’\t’隔开。* 输入描述:文本输入;* 程序输出:输出单词,词性,中文:
代码如下:
#include<iostream>#include <fstream>#include<string>#include<cstdlib>using namespace std;class Word{public: void set(string e, string c, string wc); int compare(string); string getChinese(); string getWord_class();private: string english; string chinese; string word_class;};void Word::set(string e, string c, string wc){ english=e; chinese=c; word_class=wc;}int Word::compare(string k){ return english.compare(k);}string Word::getChinese(){ return chinese;}string Word::getWord_class(){ return word_class;}class Dictionary{public: Dictionary(); void searchWord(string k);private: int BinSeareh(int low, int high, string k); int wordsNum; Word words[8000];};Dictionary::Dictionary(){ string e,c,wc; wordsNum=0; ifstream infile("dictionary.txt",ios::in); if(!infile) { cerr<<"dictionary open error!"<<endl; exit(1); } while (!infile.eof()) { infile>>e>>c>>wc; words[wordsNum].set(e, c, wc); ++wordsNum; } infile.close();}int Dictionary::BinSeareh(int low, int high, string key){ int mid; while(low<=high) { mid=(low + high) / 2; if(words[mid].compare(key)==0) { return mid; } if(words[mid].compare(key)>0) high=mid-1; else low=mid+1; } return -1;}void Dictionary::searchWord(string key){ int low=0,high=wordsNum-1; int index=BinSeareh(low, high, key); if(index>=0) cout<<key<<"<---"<<words[index].getWord_class()+"\t"<<words[index].getChinese(); else cout<<"查无此词"; cout<<endl<<endl;}int main( ){ Dictionary dic; string key; do { cout<<"请输入待查询的关键词(英文),0000结束:"<<endl; cin>>key; if (key!="0000") { dic.searchWord(key); } } while(key!="0000"); return 0;}
运行结果:
学习心得:
代码是照着贺老的一个个敲的,那些长长的标识符就用来学英语了,
曾经是用结构体做过,这次是用类和对象来做,孰优孰劣,却没多少感觉。
1 0
- 第14周-输入输出流,文本文件-项目3-OOP版电子词典
- 第14周【项目3-OOP版电子词典】
- 第14周项目3-OOP版电子词典
- 第13周项目OOP版电子词典
- 第14周 项目三-OOP版电子词典
- 第15周 项目3 oop版电子词典
- 十五周 项目3 oop版电子词典
- 第15周 项目三 oop版电子词典
- 项目-OOP版电子词典
- C++第15周(春)项目3 - OOP版电子词典(一)
- C++第15周(春)项目3 - OOP版电子词典(二)
- 第十四周项目3-OOP版电子词典
- 第十四周 项目3-oop版电子词典
- 第十四周【项目3-OOP版电子词典】
- 第十四周项目3:OOP版电子词典
- 第十四周项目3-OOP版电子词典
- 项目48-OOP版电子词典
- 第14周-输入输出流,文本文件-项目0-课后实践·程序阅读3
- thinkphp使用flash上传验证登录问题
- 20150602函数
- linux使用技巧和方法
- js实现小时钟,js中Date对象的使用?
- 程序员必读的职业规划书之摘要
- 第14周-输入输出流,文本文件-项目3-OOP版电子词典
- 20150603指针基本操作及应用
- WWDC 2015大会视频直播地址:看iOS9大展身手
- 第三章
- Netlink实现热拔插监控
- C语言指针数组和数组指针
- 给大数据文件的每一行产生唯一的id
- IntelMP(Intel Multiple Processor)相关结构
- 极大似然估计原理思想