wxWidgets+wxSmith版电子词典

时间:2023-03-10 03:28:48
wxWidgets+wxSmith版电子词典

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

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

  做一个简单的电子词典。

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

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

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

  wxWidgets+wxSmith版电子词典

本文是拓展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.****.net/sxhelijian/article/details/28230293http://blog.****.net/sxhelijian/article/details/28231661

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

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

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

  wxWidgets+wxSmith版电子词典

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

  在生成如上界面的同一时候,产生两个文件: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+wxSmith版电子词典

附录:可能错误的处理

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

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

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

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

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