wxWidgets+wxSmith版电子词典
课程首页在: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
- #ifndef DICT_H_INCLUDED
- #define DICT_H_INCLUDED
- using namespace std;
- //定义词条类
- class Word
- {
- public:
- void set(string e, string c, string wc);
- int compare(string); //英语部分与给定字符串比較。等于返回,大于返回。小于返回-1
- string getChinese();
- string getWord_class();
- private:
- string english;
- string chinese;
- string word_class;
- };
- //定义字典类
- class Dictionary
- {
- public:
- Dictionary();
- string searchWord(string k);
- private:
- int BinSeareh(int low, int high, string k);
- int wordsNum;
- Word words[8000]; //用于保存词库
- };
- #endif // DICT_H_INCLUDED
dict.cpp
- #include <fstream>
- #include <cstdlib>
- #include "dict.h"
- using namespace std;
- 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;
- }
- Dictionary::Dictionary()
- {
- string e,c,wc;
- wordsNum=0;
- //将文件里的数据读入到对象数组中
- ifstream infile("dictionary.txt",ios::in); //以输入的方式打开文件
- if(!infile) //測试是否成功打开
- {
- //cout<<"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; //继续在w[low..mid-1]中查找
- else
- low=mid+1; //继续在w[mid+1..high]中查找
- }
- return -1; //当low>high时表示查找区间为空,查找失败
- }
- string Dictionary::searchWord(string key)
- {
- int low=0,high=wordsNum-1; //置当前查找区间上、下界的初值
- int index=BinSeareh(low, high, key);
- if(index>=0)
- return words[index].getWord_class()+words[index].getChinese();
- else
- return "查无此词";
- }
这部分工作能够先行进行測试。測试工作不须要窗体,建立console application来完毕更靠谱。见链接http://blog.csdn.net/sxhelijian/article/details/28230293和http://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
- #ifndef DICTFRAME_H
- #define DICTFRAME_H
- //(*Headers(dictFrame)
- #include <wx/stattext.h>
- #include <wx/textctrl.h>
- #include <wx/panel.h>
- #include <wx/button.h>
- #include <wx/frame.h>
- //*)
- #include "dict.h" //自己加,要用到字典类
- class dictFrame: public wxFrame
- {
- public:
- dictFrame(wxWindow* parent,wxWindowID id=wxID_ANY,const wxPoint& pos=wxDefaultPosition,const wxSize& size=wxDefaultSize);
- virtual ~dictFrame();
- //(*Declarations(dictFrame)
- wxStaticText* StaticText2;
- wxButton* Button1;
- wxPanel* Panel1;
- wxStaticText* StaticText1;
- wxTextCtrl* TextCtrl2;
- wxTextCtrl* TextCtrl1;
- //*)
- protected:
- //(*Identifiers(dictFrame)
- static const long ID_TEXTCTRL1;
- static const long ID_BUTTON1;
- static const long ID_STATICTEXT1;
- static const long ID_TEXTCTRL2;
- static const long ID_STATICTEXT2;
- static const long ID_PANEL1;
- //*)
- private:
- Dictionary dict; //自己加:将字典类对象作为Frame类的成员
- //(*Handlers(dictFrame)
- void OnButton1Click(wxCommandEvent& event);
- void OnClose(wxCloseEvent& event);
- //*)
- DECLARE_EVENT_TABLE()
- };
- #endif
dictFrame.cpp
- #include "dictFrame.h"
- #include "dict.h"
- //(*InternalHeaders(dictFrame)
- #include <wx/intl.h>
- #include <wx/string.h>
- //*)
- //(*IdInit(dictFrame)
- const long dictFrame::ID_TEXTCTRL1 = wxNewId();
- const long dictFrame::ID_BUTTON1 = wxNewId();
- const long dictFrame::ID_STATICTEXT1 = wxNewId();
- const long dictFrame::ID_TEXTCTRL2 = wxNewId();
- const long dictFrame::ID_STATICTEXT2 = wxNewId();
- const long dictFrame::ID_PANEL1 = wxNewId();
- //*)
- BEGIN_EVENT_TABLE(dictFrame,wxFrame)
- //(*EventTable(dictFrame)
- //*)
- END_EVENT_TABLE()
- dictFrame::dictFrame(wxWindow* parent,wxWindowID id,const wxPoint& pos,const wxSize& size):dict() //加上对子对象dict的初始化,读入字典
- {
- //(*Initialize(dictFrame)
- Create(parent, id, _("我的字典"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE, _T("id"));
- SetClientSize(wxSize(354,111));
- Move(wxDefaultPosition);
- Panel1 = new wxPanel(this, ID_PANEL1, wxPoint(64,56), wxDefaultSize, wxTAB_TRAVERSAL, _T("ID_PANEL1"));
- TextCtrl1 = new wxTextCtrl(Panel1, ID_TEXTCTRL1, wxEmptyString, wxPoint(64,32), wxSize(192,22), 0, wxDefaultValidator, _T("ID_TEXTCTRL1"));
- Button1 = new wxButton(Panel1, ID_BUTTON1, _("查字典"), wxPoint(264,32), wxSize(51,24), 0, wxDefaultValidator, _T("ID_BUTTON1"));
- StaticText1 = new wxStaticText(Panel1, ID_STATICTEXT1, _("释义"), wxPoint(32,72), wxDefaultSize, 0, _T("ID_STATICTEXT1"));
- TextCtrl2 = new wxTextCtrl(Panel1, ID_TEXTCTRL2, wxEmptyString, wxPoint(64,64), wxSize(192,22), 0, wxDefaultValidator, _T("ID_TEXTCTRL2"));
- TextCtrl2->Disable();
- StaticText2 = new wxStaticText(Panel1, ID_STATICTEXT2, _("英文"), wxPoint(32,40), wxDefaultSize, 0, _T("ID_STATICTEXT2"));
- Connect(ID_BUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&dictFrame::OnButton1Click);
- //*)
- TextCtrl1->SetFocus (); //自己加:希望输入英文的文本框获得“焦点”
- }
- dictFrame::~dictFrame()
- {
- //(*Destroy(dictFrame)
- //*)
- }
- void dictFrame::OnButton1Click(wxCommandEvent& event)
- { //函数由添加事件处理生成,但函数体要自己实现
- string s=string(TextCtrl1->GetLineText(0)); //获得文本框中文字
- wxString ws = dict.searchWord(s); //查字典
- TextCtrl2->SetLabelText(ws); //在还有一个文本框中显示查询结果
- }
第四部分工作 写主控程序
每个wxWidgets程序都要写以下的代码。按套路来即可:
MyApp.h
- #include <wx/wx.h>
- class MyApp : public wxApp
- {
- public:
- virtual bool OnInit();
- };
MyApp.cpp
- #include "MyApp.h"
- #include "dictFrame.h"
- IMPLEMENT_APP(MyApp)
- bool MyApp::OnInit()
- {
- dictFrame *mydict = new dictFrame(NULL);
- mydict->Show(true);
- return true;
- }
本文程序的执行截图:
附录:可能错误的处理
关于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。从 下载,并将这个文件拷贝到项目所在文件夹中。
|
wxWidgets+wxSmith版电子词典的更多相关文章
- C++第15周(春)项目3 - OOP版电子词典(一)
课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759.内有完整教学方案及资源链接 [项目3-OOP版电子词典](本程序中须要的相 ...
- C++第15周(春)项目3 - OOP版电子词典(二)
课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759,内有完整教学方案及资源链接 [项目3-OOP版电子词典](本程序须要的相关 ...
- 第十四周(OOP版电子词典)
/* *copyright(c) 2015,烟台大学计算机学院 *All rights reserved. *文件名:第十四周(OOP版电子词典) *作者:王忠 *完毕日期:2015.6.10 *版本 ...
- 第14周 项目三-OOP版电子词典
做一个简单的电子词典.在文件dictionary.txt中,保存的是英汉对比的一个词典,词汇量近8000个,英文.中文释义与词性间用'\t'隔开. (1)编程序,由用户输入英文词.显示词性和中文释义. ...
- OOP版电子词典
输入代码: /* * Copyright (c) 2014, 烟台大学计算机学院 * All rights reserved. * 文件名:sum123.cpp * 作 者:林海云 * 完毕日期:20 ...
- 吴裕雄--天生自然python学习笔记:python 用firebase实现英文电子词典
Firebase 版电子词典 学英语是许多 人一辈子的麻烦 . 所以本例中,我们开发一个英汉词典,用户执 行程序后,单击“翻译”按钮即可显示该单词的中文翻译 . 英汉词典标准版 因为这个案例的数据必须 ...
- 电子词典的相关子函数db.c程序
整个电子词典是分块做的:包含的Dic_Server.c,Dic_Client.c,db.c,query.c,xprtcl.c,dict.h,xprtcl.h,dict.txt(单词文件) 下面是db. ...
- OC4_电子词典
// // MyDictionary.h // OC4_电子词典 // // Created by zhangxueming on 15/6/15. // Copyright (c) 2015年 zh ...
- 使用Android简单实现有道电子词典
前言: 毕业设计的内容,仅仅有Java基础.没学过Android. 本着用到什么学什么.花费了10多个晚上完毕毕业设计. 当然,仅仅是简单的实线了电子词典功能,自始至终没有考虑过性能等问题. 本电子词 ...
随机推荐
- Android - 位置定位(Location)服务(Service)类的基本操作
位置定位(Location)服务(Service)类的基本操作 本文地址: http://blog.csdn.net/caroline_wendy 定位服务(Location Service),能够确 ...
- XSS学习笔记(一个)-点击劫持
所谓XSS这个场景被触发XSS地方,在大多数情况下,攻击者被嵌入在网页中(问题)该恶意脚本(Cross site Scripting),这里的攻击始终触发浏览器端,攻击的者的目的.一般都是获取用户的C ...
- Java集合类汇总记录-- apache.commons4(TreeList)
通常.Tree是Tree,List是List,两者不太可能混在一起.但apache-commons库却用tree实现了实现了List的接口,也就是TreeList类.与标准的LinkedList相比. ...
- 实现Android ListView 自动加载更多内容
研究了几个小时终于实现了Android ListView 自动加载的效果. 说说我是怎样实现的.分享给大家. 1.给ListView增加一个FooterView,调用addFooterView(foo ...
- linux grep命令详解(转)
简介 grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它 ...
- VC6 鼠标钩子 最简单样例
Windows系统是建立在事件驱动的机制上的,说穿了就是整个系统都是通过消息的传递来实现的.而钩子是Windows系统中非常重要的系统接口,用它能够截获并处理送给其它应用程序的消息,来完毕普通应用程序 ...
- error: png.h not found.
跑php设备 --enable-mbstring --enable-ftp --enable-gd-native-ttf --with-openssl --enable-pcntl --enable- ...
- ubuntu10.10和windows双系统启动顺序的修改
我想大部分童鞋装ubuntu的时候,硬盘上的windows肯定还是保留着的,启动电 脑时可以选择,想进windows就进windows,想进ubuntu就进ubuntu.但装完ubuntu后,它默认启 ...
- 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 ...
- Mongodb入门——安装与配置
作者:zhanhailiang 日期:2014-11-07 1. 安装mongodb: [root@~/wade/nodejs/nodeclub]# yum search mongodb [root@ ...