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。从 下载,并将这个文件拷贝到项目所在文件夹中。
================= 迂者 贺利坚 CSDN博客专栏================= |
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多个晚上完毕毕业设计. 当然,仅仅是简单的实线了电子词典功能,自始至终没有考虑过性能等问题. 本电子词 ...
随机推荐
- 中文字符集编码Unicode ,gb2312 , cp936 ,GBK,GB18030
中文字符集编码Unicode ,gb2312 , cp936 ,GBK,GB18030 cp936是微软自己发布的用在文件系统中的编码方式.而bg2312是中国国家标准.我明白mount -t vfa ...
- Android 上实现非root的 Traceroute -- 非Root权限下移植可运行二进制文件 脚本文件
作者 : 万境绝尘 转载请著名出处 : http://blog.csdn.net/shulianghan/article/details/36438365 演示样例代码下载 : -- CSDN : h ...
- UVA 10831 - Gerg's Cake(数论)
UVA 10831 - Gerg's Cake 题目链接 题意:说白了就是给定a, p.问有没有存在x^2 % p = a的解 思路:求出勒让德标记.推断假设大于等于0,就是有解,小于0无解 代码: ...
- Oracle学习(十四):管理用户安全性
--用户(user) SQL> --创建一个名为 grace password是password 的用户,新用户没有不论什么权限 SQL> create user grace identi ...
- 如何使用 yum 安装/更新/移除 软件
如何使用 yum 安装/更新/移除 软件 一. 建立仓库(repository)和源 a) 拷贝所以相关rpm包到某个目录 b) 执行createrepo /目录/目录/目录/目录 注意:b)中 ...
- js如果你想删除您问
if (confirm("OK删除?") == true)
- VS2008下直接安装Boost库1.46.1版本号
Boost图书馆是一个移植.提供源代码C++库.作为一个备份标准库,这是C++发动机之间的一种标准化的过程. Boost图书馆由C++图书馆标准委员会工作组成员发起,一些内容有望成为下一代C++标准库 ...
- 悼念传奇,约翰询问·纳什和他的妻子艾丽西亚致敬,创建一个传奇,爱数学
约翰·阅读·纳什的传记.我渴望录制通道 我一直相信数字,无论逻辑方程使我们认为.但这种追求一生的后,我问自己:"这是什么逻辑?谁决定的理由?"我的探索让我从物理到形而上,最后到了妄 ...
- c++堆栈实现
A Stack is a data-structure that You can only add an element to the top of the Stack, andYou can onl ...
- 说说UI设计
近期的项目验收中,无数次的提到了UI的设计,首先来说说为什么UI设计如此重要. 对于用户来说产品的外观是最先映入眼帘的,无论你用了什么高端的技术,无论你后台代码封装的多么好,用户是无法体会到的,能体会 ...