课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759,内有完整教学方案及资源链接

【项目3-OOP版电子词典】(本程序须要的相关文件,请到http://pan.baidu.com/s/1qW59HTi下载。

  做一个简单的电子词典。

在文件dictionary.txt中,保存的是英汉对比的一个词典,词汇量近8000个,英文、中文释义与词性间用’\t’隔开。

  编程序。由用户输入英文词,显示词性和中文释义。
  【项目3拓展1(选做)】使这个词典。读入一篇文章,输出对当中的所词的解释。比如。对aboutcpp.txt,输出例如以下左图结果所看到的(也能够看到当中待改进的地方)。

  【项目3拓展2(选做)】试用wxwidgets做一个窗体版的电子词典,例如以下右图所看到的:

  

本文是拓展2的參考解答:

第一部分工作:编写业务逻辑

  一个程序中。最主要的是其业务。

先定义词(Word)类和字典(Dictionary)类例如以下:

dict.h

  1. #ifndef DICT_H_INCLUDED
  2. #define DICT_H_INCLUDED
  3. using namespace std;
  4. //定义词条类
  5. class Word
  6. {
  7. public:
  8. void set(string e, string c, string wc);
  9. int compare(string); //英语部分与给定字符串比較。等于返回,大于返回。小于返回-1
  10. string getChinese();
  11. string getWord_class();
  12. private:
  13. string english;
  14. string chinese;
  15. string word_class;
  16. };
  17.  
  18. //定义字典类
  19. class Dictionary
  20. {
  21. public:
  22. Dictionary();
  23. string searchWord(string k);
  24. private:
  25. int BinSeareh(int low, int high, string k);
  26. int wordsNum;
  27. Word words[8000]; //用于保存词库
  28. };
  29.  
  30. #endif // DICT_H_INCLUDED

dict.cpp

  1. #include <fstream>
  2. #include <cstdlib>
  3. #include "dict.h"
  4. using namespace std;
  5. void Word::set(string e, string c, string wc)
  6. {
  7. english=e;
  8. chinese=c;
  9. word_class=wc;
  10. }
  11.  
  12. int Word::compare(string k)
  13. {
  14. return english.compare(k);
  15. }
  16.  
  17. string Word::getChinese()
  18. {
  19. return chinese;
  20. }
  21.  
  22. string Word::getWord_class()
  23. {
  24. return word_class;
  25. }
  26.  
  27. Dictionary::Dictionary()
  28. {
  29. string e,c,wc;
  30. wordsNum=0;
  31. //将文件里的数据读入到对象数组中
  32. ifstream infile("dictionary.txt",ios::in); //以输入的方式打开文件
  33. if(!infile) //測试是否成功打开
  34. {
  35. //cout<<"dictionary open error!"<<endl;
  36. exit(1);
  37. }
  38. while (!infile.eof())
  39. {
  40. infile>>e>>c>>wc;
  41. words[wordsNum].set(e, c, wc);
  42. ++wordsNum;
  43. }
  44. infile.close();
  45. }
  46.  
  47. int Dictionary::BinSeareh(int low, int high, string key)
  48. {
  49. int mid;
  50. while(low<=high)
  51. {
  52. mid=(low + high) / 2;
  53. if(words[mid].compare(key)==0)
  54. {
  55. return mid; //查找成功返回
  56. }
  57. if(words[mid].compare(key)>0)
  58. high=mid-1; //继续在w[low..mid-1]中查找
  59. else
  60. low=mid+1; //继续在w[mid+1..high]中查找
  61. }
  62. return -1; //当low>high时表示查找区间为空,查找失败
  63. }
  64.  
  65. string Dictionary::searchWord(string key)
  66. {
  67. int low=0,high=wordsNum-1; //置当前查找区间上、下界的初值
  68. int index=BinSeareh(low, high, key);
  69. if(index>=0)
  70. return words[index].getWord_class()+words[index].getChinese();
  71. else
  72. return "查无此词";
  73. }

  这部分工作能够先行进行測试。測试工作不须要窗体,建立console application来完毕更靠谱。见链接http://blog.csdn.net/sxhelijian/article/details/28230293http://blog.csdn.net/sxhelijian/article/details/28231661

第二部分工作 用wxSmith制作界面

  能够按wxWidgets教程(见http://blog.csdn.net/sxhelijian/article/details/26158709),通过代码制作界面,简单的办法是用wxSmith。作为专业学生,应该学会代码。以掌握类库工作的原理,但要高速开发,wxSmith也提倡使用。

  在code::Blocks中,点菜单wxSmith-->Add wxFrame。制作好的界面例如以下。本文中,相应的资源文件名称是dictFrame.wxs:

  

第三部分工作 为界面上的元素配备代码

  在生成如上界面的同一时候,产生两个文件:dictFrame.h和dictFrame.cpp。

.wxs文件资源文件的目标也就是生成这两个文件。这是应用程序中的代码部分。

以下列出的是这两个文件里的内容。大部分由框架提供,须要自加的部分,给出了凝视。

dictFrame.h

  1. #ifndef DICTFRAME_H
  2. #define DICTFRAME_H
  3.  
  4. //(*Headers(dictFrame)
  5. #include <wx/stattext.h>
  6. #include <wx/textctrl.h>
  7. #include <wx/panel.h>
  8. #include <wx/button.h>
  9. #include <wx/frame.h>
  10. //*)
  11.  
  12. #include "dict.h" //自己加,要用到字典类
  13.  
  14. class dictFrame: public wxFrame
  15. {
  16. public:
  17.  
  18. dictFrame(wxWindow* parent,wxWindowID id=wxID_ANY,const wxPoint& pos=wxDefaultPosition,const wxSize& size=wxDefaultSize);
  19. virtual ~dictFrame();
  20.  
  21. //(*Declarations(dictFrame)
  22. wxStaticText* StaticText2;
  23. wxButton* Button1;
  24. wxPanel* Panel1;
  25. wxStaticText* StaticText1;
  26. wxTextCtrl* TextCtrl2;
  27. wxTextCtrl* TextCtrl1;
  28. //*)
  29.  
  30. protected:
  31.  
  32. //(*Identifiers(dictFrame)
  33. static const long ID_TEXTCTRL1;
  34. static const long ID_BUTTON1;
  35. static const long ID_STATICTEXT1;
  36. static const long ID_TEXTCTRL2;
  37. static const long ID_STATICTEXT2;
  38. static const long ID_PANEL1;
  39. //*)
  40.  
  41. private:
  42. Dictionary dict; //自己加:将字典类对象作为Frame类的成员
  43. //(*Handlers(dictFrame)
  44. void OnButton1Click(wxCommandEvent& event);
  45. void OnClose(wxCloseEvent& event);
  46. //*)
  47.  
  48. DECLARE_EVENT_TABLE()
  49. };
  50.  
  51. #endif

dictFrame.cpp

  1. #include "dictFrame.h"
  2. #include "dict.h"
  3.  
  4. //(*InternalHeaders(dictFrame)
  5. #include <wx/intl.h>
  6. #include <wx/string.h>
  7. //*)
  8.  
  9. //(*IdInit(dictFrame)
  10. const long dictFrame::ID_TEXTCTRL1 = wxNewId();
  11. const long dictFrame::ID_BUTTON1 = wxNewId();
  12. const long dictFrame::ID_STATICTEXT1 = wxNewId();
  13. const long dictFrame::ID_TEXTCTRL2 = wxNewId();
  14. const long dictFrame::ID_STATICTEXT2 = wxNewId();
  15. const long dictFrame::ID_PANEL1 = wxNewId();
  16. //*)
  17.  
  18. BEGIN_EVENT_TABLE(dictFrame,wxFrame)
  19. //(*EventTable(dictFrame)
  20. //*)
  21. END_EVENT_TABLE()
  22.  
  23. dictFrame::dictFrame(wxWindow* parent,wxWindowID id,const wxPoint& pos,const wxSize& size):dict() //加上对子对象dict的初始化,读入字典
  24. {
  25. //(*Initialize(dictFrame)
  26. Create(parent, id, _("我的字典"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE, _T("id"));
  27. SetClientSize(wxSize(354,111));
  28. Move(wxDefaultPosition);
  29. Panel1 = new wxPanel(this, ID_PANEL1, wxPoint(64,56), wxDefaultSize, wxTAB_TRAVERSAL, _T("ID_PANEL1"));
  30. TextCtrl1 = new wxTextCtrl(Panel1, ID_TEXTCTRL1, wxEmptyString, wxPoint(64,32), wxSize(192,22), 0, wxDefaultValidator, _T("ID_TEXTCTRL1"));
  31. Button1 = new wxButton(Panel1, ID_BUTTON1, _("查字典"), wxPoint(264,32), wxSize(51,24), 0, wxDefaultValidator, _T("ID_BUTTON1"));
  32. StaticText1 = new wxStaticText(Panel1, ID_STATICTEXT1, _("释义"), wxPoint(32,72), wxDefaultSize, 0, _T("ID_STATICTEXT1"));
  33. TextCtrl2 = new wxTextCtrl(Panel1, ID_TEXTCTRL2, wxEmptyString, wxPoint(64,64), wxSize(192,22), 0, wxDefaultValidator, _T("ID_TEXTCTRL2"));
  34. TextCtrl2->Disable();
  35. StaticText2 = new wxStaticText(Panel1, ID_STATICTEXT2, _("英文"), wxPoint(32,40), wxDefaultSize, 0, _T("ID_STATICTEXT2"));
  36.  
  37. Connect(ID_BUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&dictFrame::OnButton1Click);
  38. //*)
  39. TextCtrl1->SetFocus (); //自己加:希望输入英文的文本框获得“焦点”
  40. }
  41.  
  42. dictFrame::~dictFrame()
  43. {
  44. //(*Destroy(dictFrame)
  45. //*)
  46. }
  47.  
  48. void dictFrame::OnButton1Click(wxCommandEvent& event)
  49. { //函数由添加事件处理生成,但函数体要自己实现
  50. string s=string(TextCtrl1->GetLineText(0)); //获得文本框中文字
  51. wxString ws = dict.searchWord(s); //查字典
  52. TextCtrl2->SetLabelText(ws); //在还有一个文本框中显示查询结果
  53. }

第四部分工作 写主控程序

  每个wxWidgets程序都要写以下的代码。按套路来即可:

MyApp.h

  1. #include <wx/wx.h>
  2.  
  3. class MyApp : public wxApp
  4. {
  5. public:
  6. virtual bool OnInit();
  7. };

MyApp.cpp

  1. #include "MyApp.h"
  2. #include "dictFrame.h"
  3.  
  4. IMPLEMENT_APP(MyApp)
  5.  
  6. bool MyApp::OnInit()
  7. {
  8. dictFrame *mydict = new dictFrame(NULL);
  9. mydict->Show(true);
  10.  
  11. return true;
  12. }

本文程序的执行截图:

  

附录:可能错误的处理

  关于wxWidgets的入门知识及学习方法指导,见:http://blog.csdn.net/sxhelijian/article/details/26158709

  假设在编译和连接中遇到问题,见:http://blog.csdn.net/sxhelijian/article/details/26164181

  关于wxSmith,见http://blog.csdn.net/sxhelijian/article/details/26165237

  程序执行不出现窗体直接结束。最大的可能是:字典文件不能读入。注意这个应用须要用到字典文件dictionary.txt。从   下载,并将这个文件拷贝到项目所在文件夹中。

  1. ================= 迂者 贺利坚 CSDN博客专栏=================
  2. |== IT学子成长指导专栏 专栏文章的分类文件夹(不定期更新) ==|
  3. |== C++ 课堂在线专栏  贺利坚课程教学链接(分课程年级) ==|
  4. |== 我写的书——《逆袭大学——传给IT学子的正能量》    ==|
  5. ===== IT菜鸟起飞铺跑道。和学生一起享受快乐和激情的大学 =====

wxWidgets+wxSmith版电子词典的更多相关文章

  1. C++第15周(春)项目3 - OOP版电子词典(一)

    课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759.内有完整教学方案及资源链接 [项目3-OOP版电子词典](本程序中须要的相 ...

  2. C++第15周(春)项目3 - OOP版电子词典(二)

    课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759,内有完整教学方案及资源链接 [项目3-OOP版电子词典](本程序须要的相关 ...

  3. 第十四周(OOP版电子词典)

    /* *copyright(c) 2015,烟台大学计算机学院 *All rights reserved. *文件名:第十四周(OOP版电子词典) *作者:王忠 *完毕日期:2015.6.10 *版本 ...

  4. 第14周 项目三-OOP版电子词典

    做一个简单的电子词典.在文件dictionary.txt中,保存的是英汉对比的一个词典,词汇量近8000个,英文.中文释义与词性间用'\t'隔开. (1)编程序,由用户输入英文词.显示词性和中文释义. ...

  5. OOP版电子词典

    输入代码: /* * Copyright (c) 2014, 烟台大学计算机学院 * All rights reserved. * 文件名:sum123.cpp * 作 者:林海云 * 完毕日期:20 ...

  6. 吴裕雄--天生自然python学习笔记:python 用firebase实现英文电子词典

    Firebase 版电子词典 学英语是许多 人一辈子的麻烦 . 所以本例中,我们开发一个英汉词典,用户执 行程序后,单击“翻译”按钮即可显示该单词的中文翻译 . 英汉词典标准版 因为这个案例的数据必须 ...

  7. 电子词典的相关子函数db.c程序

    整个电子词典是分块做的:包含的Dic_Server.c,Dic_Client.c,db.c,query.c,xprtcl.c,dict.h,xprtcl.h,dict.txt(单词文件) 下面是db. ...

  8. OC4_电子词典

    // // MyDictionary.h // OC4_电子词典 // // Created by zhangxueming on 15/6/15. // Copyright (c) 2015年 zh ...

  9. 使用Android简单实现有道电子词典

    前言: 毕业设计的内容,仅仅有Java基础.没学过Android. 本着用到什么学什么.花费了10多个晚上完毕毕业设计. 当然,仅仅是简单的实线了电子词典功能,自始至终没有考虑过性能等问题. 本电子词 ...

随机推荐

  1. Android - 位置定位(Location)服务(Service)类的基本操作

    位置定位(Location)服务(Service)类的基本操作 本文地址: http://blog.csdn.net/caroline_wendy 定位服务(Location Service),能够确 ...

  2. XSS学习笔记(一个)-点击劫持

    所谓XSS这个场景被触发XSS地方,在大多数情况下,攻击者被嵌入在网页中(问题)该恶意脚本(Cross site Scripting),这里的攻击始终触发浏览器端,攻击的者的目的.一般都是获取用户的C ...

  3. Java集合类汇总记录-- apache.commons4(TreeList)

    通常.Tree是Tree,List是List,两者不太可能混在一起.但apache-commons库却用tree实现了实现了List的接口,也就是TreeList类.与标准的LinkedList相比. ...

  4. 实现Android ListView 自动加载更多内容

    研究了几个小时终于实现了Android ListView 自动加载的效果. 说说我是怎样实现的.分享给大家. 1.给ListView增加一个FooterView,调用addFooterView(foo ...

  5. linux grep命令详解(转)

    简介 grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它 ...

  6. VC6 鼠标钩子 最简单样例

    Windows系统是建立在事件驱动的机制上的,说穿了就是整个系统都是通过消息的传递来实现的.而钩子是Windows系统中非常重要的系统接口,用它能够截获并处理送给其它应用程序的消息,来完毕普通应用程序 ...

  7. error: png.h not found.

    跑php设备 --enable-mbstring --enable-ftp --enable-gd-native-ttf --with-openssl --enable-pcntl --enable- ...

  8. ubuntu10.10和windows双系统启动顺序的修改

    我想大部分童鞋装ubuntu的时候,硬盘上的windows肯定还是保留着的,启动电 脑时可以选择,想进windows就进windows,想进ubuntu就进ubuntu.但装完ubuntu后,它默认启 ...

  9. Codeforces Round #256 (Div. 2) A. Rewards

    A. Rewards time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  10. Mongodb入门——安装与配置

    作者:zhanhailiang 日期:2014-11-07 1. 安装mongodb: [root@~/wade/nodejs/nodeclub]# yum search mongodb [root@ ...