当前位置: 编程技术>综合
本页文章导读:
▪MFC中如何添加消息响应函数 目前,用MFC设计的Windows应用程序几乎都采用文档/视图结构。这种程序框架与简单程序框架之间的重要区别就在于形成应用程序的主窗口不只需要一个类的对象,而是需要3个对象:
框架窗口.........
▪资源未释放引起的内存漏洞模拟 在MFC编程中,我们常常创建一些GDI资源并通过指针使用它们;
例如:
CClientDC dc(this);
CBrush *m_pBrush=new CBrush(RGB(255,0,0));
dc.SelectObject(m_pBrush);
dc.Rectangle(0,0,100,100);.........
▪Trie树及其应用 一 Trie树
可参照百度百科: http://baike.baidu.com/view/1436495.htm
二 Trie树的构造及应用
/*
This is a free Program, You can modify or redistribute it under the terms of GNU
*Description:Trie树及其应用
*Language: C++
*Devel.........
[1]MFC中如何添加消息响应函数
来源: 互联网 发布时间: 2013-11-07
目前,用MFC设计的Windows应用程序几乎都采用文档/视图结构。这种程序框架与简单程序框架之间的重要区别就在于形成应用程序的主窗口不只需要一个类的对象,而是需要3个对象:
- 框架窗口类(CFrameWnd)对象
- 视图类(CView)对象
- 文档类(CDocument)对象
- 首先,我们建立一个简单的文档/视图结构的应用程序esayApp(在应用向导中基本遵从默认即可),如图所示:
- 在菜单的资源编辑器中添加“testMessage”选项,如下图所示:
在添加的选项上右键,然后点击属性,注意其ID(当添加新选项是,系统会默认生成一个ID),在这里我们为了强调,将默认ID改为”ID_TESTMESSAGE“。然后打开Resource.h文件,在其中会发现系统自动为新添加项增加了序号,如下图所示:(如果没有需要自己手动添加)
- 现在,我们在视图类中添加该菜单的消息映射和消息响应函数。
- 在simpleAppView.cpp中添加新的消息映射:
- 在simpleAppView.h中添加消息响应函数的声明:
- 最后在simpleAppView.cpp中添加消息响应函数是实现:
至此,具有消息响应功能的视图/文档结构的MFC应用程序就设计完成了,单击“文件-->testMessage”效果如下:
总结下消息响应函数添加的过程:
作者:eddy_liu 发表于2013-1-6 21:45:55 原文链接
阅读:0 评论:0 查看评论
[2]资源未释放引起的内存漏洞模拟
来源: 互联网 发布时间: 2013-11-07
在MFC编程中,我们常常创建一些GDI资源并通过指针使用它们;
例如:
CClientDC dc(this); CBrush *m_pBrush=new CBrush(RGB(255,0,0)); dc.SelectObject(m_pBrush); dc.Rectangle(0,0,100,100); delete m_pBrush; m_pBrush=NULL;
但有时候,我们常常会忘记释放资源指针;如果程序多次运行上述代码片段,在任务管理器中可以观察到程序占用内存将不断增加,同时产生大量内存碎片;严重可能会引起系统内存不够用,导致其他程序不能正常运行或死机。
其原理可用下述程式模拟:
char *pMem=NULL;
for(;;)
{
pMem=new char[1024];
}
作者:mrp_user 发表于2013-1-6 21:38:05 原文链接
阅读:0 评论:0 查看评论
[3]Trie树及其应用
来源: 互联网 发布时间: 2013-11-07
一 Trie树
可参照百度百科: http://baike.baidu.com/view/1436495.htm
二 Trie树的构造及应用
/*
This is a free Program, You can modify or redistribute it under the terms of GNU
*Description:Trie树及其应用
*Language: C++
*Development Environment: VC6.0
*Author: Wangzhicheng
*E-mail: 2363702560@qq.com
*Date: 2013/1/6
*/
#include <iostream>
#include <cstdlib>
#include <vector>
#include <string>
#include <cctype>
#include <ctime>
#include <algorithm>
using namespace std;
const int Num = 26; //定义26个字母
/*
* 定义结点类型
* UNCOMPLETED表示该结点对应的字符串还未创建完毕
* COMPLETED表示该结点对应的字符串已经创建完毕
*/
enum NodeType { UNCOMPLETED, COMPLETED };
class Trie; //向前引用
/*
* 定义结点类
* @type: 结点类型
* @ch: 结点所含的字符
* @child: 结点孩子指针向量
*/
class Node {
private:
friend class Trie;
NodeType type;
char ch;
vector<Node *>child;
};
/*
* 定义Trie树
*/
class Trie {
private:
Node *root; //根结点
/*
* 创建新结点
* @ch: 结点含有的字符
*/
Node *newNode(char ch) {
Node *p=new Node;
if(!p) {
cerr<<"内存不足!"<<endl;
exit(1);
}
p->type = UNCOMPLETED;
p->ch = ch;
p->child.assign(Num, NULL);
return p;
}
/*
* 返回字符在孩子指针向量中的位置
*/
int index(char ch) {
if(isupper(ch)) ch = tolower(ch);
return ch - 'a';
}
/*
* 删除指针ptr所指向的trie子树, 采用递归遍历子树
*/
void delTrie(Node *&ptr) {
vector<Node *>::iterator it;
if(ptr == NULL) return;
for(it = ptr->child.begin(); it != ptr->child.end(); it++) {
if(*it && (*it)->type != COMPLETED) delTrie(*it);
delete *it;
*it = NULL;
}
delete ptr;
ptr = NULL;
}
/*
* 遍历Trie树
*/
void tranverse(Node *ptr) {
vector<Node *>::iterator it;
if(ptr == NULL) return;
for(it = ptr->child.begin(); it != ptr->child.end(); it++) {
if(*it) cout<<(*it)->ch;
if(*it && (*it)->type != COMPLETED) {
tranverse(*it);
}
}
cout<<endl;
}
public:
/*
* 向trie树中插入单词word
*/
void insertNode(string word) {
Node *ptr = root;
string::iterator it;
int pos;
for(it= word.begin(); it != word.end(); it++) {
pos = index(*it);
if(ptr->child[pos] == NULL) {
ptr->child[pos] = newNode(*it);
}
ptr = ptr->child[pos];
}
ptr->type = COMPLETED;
}
/*
* 在Trie树中删除单词word
*/
void removeNode(string word) {
Node *ptr = root;
Node *del;
int pos;
pos = index(word[0]);
ptr = ptr->child[pos];
while(ptr) {
del = ptr;
ptr = ptr->child[pos];
delete del;
}
root->child[pos] = ptr;
}
Trie() {
root = newNode('@'); //创建根结点
}
~Trie() {
delTrie(root);
}
/*
* 在Trie树中查找单词word,找到返回true,否则返回false
*/
bool find(const string &word) {
Node *ptr = root;
string::const_iterator it;
int pos;
for(it = word.begin(); it != word.end(); it++) {
pos = index(*it);
if(ptr->child[pos] == NULL) break;
ptr = ptr->child[pos];
}
return it == word.end() && ptr->type == COMPLETED;
}
void tranverse() {
tranverse(root);
}
};
void main() {
cout<<"Trie树查询系统V1.0"<<endl;
Trie t;
string word;
string::const_iterator it;
clock_t start, finish;
double duration;
while(true) {
cout<<"请输入要查询的英语单词(输入exit退出):";
cin>>word;
for(it = word.begin(); it != word.end(); it++) {
if(isupper(*it) || islower(*it)) continue;
else {
cerr<<"输入单词非法!"<<endl;
exit(1);
}
}
if(word == "exit" ) break;
start = clock();
if(t.find(word) == false) {
cerr<<"您输入的单词未查找,现在向trie树插入该单词."<<endl;
t.insertNode(word);
continue;
}
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
cout<<word<<"已经查到, 查找时间是: "<<duration<<"秒"<<endl;
}
}
三 测试:
作者:wangzhicheng2013 发表于2013-1-6 21:37:29 原文链接
阅读:0 评论:0 查看评论
最新技术文章: