课程首页在: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/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

#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博客专栏=================
|== IT学子成长指导专栏 专栏文章的分类文件夹(不定期更新) ==|
|== C++ 课堂在线专栏  贺利坚课程教学链接(分课程年级) ==|
|== 我写的书——《逆袭大学——传给IT学子的正能量》    ==|
===== 为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. ubuntu 12.04英文版设置成中文版

    适用于ubuntu 12.04英文版的系统,其他版本号的设置应该是大同小异的. 进入ubuntu系统,在顶部齿状标志找到system... 2.在personal找到Language Support ...

  2. cocos2d-x2.x环境搭建配置

    [安装工具] VS2012 Cocos2D-X 2.2.3 Python 2.7.8 一.运行cocos2dx中的hello world! 1.在Cocos2D-X 2.2.3目录下,点击cocos2 ...

  3. cocos2d-html5游戏图片资源选择

    cocos2d-html5游戏图片资源能够选择,单张的图片作为一个精灵或者场景的载入对象.也能够把图片给做成plist文件.通过plist来訪问图片资源.其中优缺点.使用方式在个人的測试其中体现例如以 ...

  4. 三星Samsung 4.4.2该负责人制度,简化名单

    installed uninstalled AccessControl.apk AllshareControlShare.apk AirMotionTryActually.apk AllshareFi ...

  5. c语言结构体使用方法

      结构(struct)      结构是由基本数据类型构成的.并用一个标识符来命名的各种变量的组合.  结构中能够使用不同的数据类型.      1. 结构说明和结构变量定义      在Turbo ...

  6. 对HGE游戏引擎的一次封装

    HGE游戏引擎是一个开源2D游戏引擎,基于directX. 它的渲染及逻辑是基于帧回调的框架模式, 其提供一些主要的图像操作和输入控制功能. 我在之前写一个2D游戏的时候对它整个框架进行了一次封装,非 ...

  7. log4j 日志大小限制 分成30一个 不按日期分日志 按大小分成 按生产日期

    首先说说生成按日期.不解释,大家都懂的,这种方法的缺点是很吃硬盘空间 log4j.rootLogger=INFO,logfile,stdout log4j.logger.java.sql=DEBUG, ...

  8. bin home

    bin=$(cd `dirname $0`;pwd)home=$(dirname $bin)

  9. 栈实现java

    栈是一种“先去后出”的抽象的数据结构.例如:我们在洗盘子的时候,洗完一个盘子,将其放在一摞盘子的最上面,但我们全部洗完后,要是有盘子时,我们会先从最上面的盘子开始使用,这种例子就像栈的数据结构一样,先 ...

  10. Socket通信原理

    对TCP/IP.UDP.Socket编程这些词你不会很陌生吧?随着网络技术的发展,这些词充斥着我们的耳朵.那么我想问: 1.         什么是TCP/IP.UDP?2.         Sock ...