MFC拾遗
c++处理文件,对txt进行处理,学了那么久才发现我还不会这些操作,另外c++处理文本真是快得可怕啊
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
ifstream infile;
infile.open("E:\\word1.txt");
string s;
ofstream outfile;
outfile.open("E:\\word2.txt");
while(getline(infile,s)) {
for(int i=; s[i]; i++)
if(s.substr(i,)=="n."||s.substr(i,)=="ad."||s.substr(i,)=="a."
||s.substr(i,)=="vi."||s.substr(i,)=="vt."||s.substr(i,)=="conj.") {
outfile<<s.substr(,i)<<" "<<s.substr(i)<<endl;
break;
}
}
return ;
}
各种调顺序,也是很有意思的,MFC也是面对对象啦
我做的课题是基于红黑树的map实现。
再列出一些自己觉得有用的吧
老师提供的宽字节转换,为了兼容汉字
ifstream in(L".\\word.txt");
char s[][];
CString st[];
while (in >> s[] >> s[]){
for (int i = ; i<; i++)
{
int charLen = strlen(s[i]);
int len = MultiByteToWideChar(CP_ACP, , s[i], charLen, NULL, );
TCHAR *buf = new TCHAR[len + ];
MultiByteToWideChar(CP_ACP, , s[i], charLen, buf, len);
buf[len] = '\0';
st[i] = buf;
}
dic.Insert(st[],st[]);
m_num++;
}
实现的时间统计。比timegettime精确地多
LARGE_INTEGER large_interger;
double dff;
__int64 c1, c2;
QueryPerformanceFrequency(&large_interger);
dff = large_interger.QuadPart;
QueryPerformanceCounter(&large_interger);
c1 = large_interger.QuadPart;
CString s2 = dic.query(s1);
QueryPerformanceCounter(&large_interger);
c2 = large_interger.QuadPart;
所以现在看在我实现的东西并不是很多
RBTree
#define _RB_TREE_H_
#include<iostream>
#include<string>
#include<sstream>
#include<fstream>
#include <time.h>
using namespace std;
template<class KEY, class U>
class RB_Tree
{
private:
RB_Tree(const RB_Tree& input){}
const RB_Tree& operator=(const RB_Tree& input){}
private:
enum COLOR{ RED, BLACK };
class RB_Node
{
public:
RB_Node()
{
RB_COLOR = BLACK;
right = NULL;
left = NULL;
parent = NULL;
}
COLOR RB_COLOR;
RB_Node* right;
RB_Node* left;
RB_Node* parent;
KEY key;
U data;
};
public:
RB_Tree()
{
this->m_nullNode = new RB_Node();
this->m_root = m_nullNode;
this->m_nullNode->right = this->m_root;
this->m_nullNode->left = this->m_root;
this->m_nullNode->parent = this->m_root;
this->m_nullNode->RB_COLOR = BLACK;
}
bool Empty()
{
if (this->m_root == this->m_nullNode)
{
return true;
}
else
{
return false;
}
}
//find node who's key equals to key,else find the insert point;
RB_Node* find(KEY key)
{
RB_Node* index = m_root;
while (index != m_nullNode)
{
if (key<index->key)
{
index = index->left;
}
else if (key>index->key)
{
index = index->right;
}
else
{
break;
}
}
return index;
}
const U& RB_Tree<KEY, U>::query(const KEY& key)
{
RB_Node *p = find(key);
if (p == NULL)
return NULL;
else
return p->data ;
}
bool Insert(KEY key, U data)
{
RB_Node* insert_point = m_nullNode;
RB_Node* index = m_root;
while (index != m_nullNode)
{
insert_point = index;
if (key<index->key)
{
index = index->left;
}
else if (key>index->key)
{
index = index->right;
}
else
{
return false;
}
}
RB_Node* insert_node = new RB_Node();
insert_node->key = key;
insert_node->data = data;
insert_node->RB_COLOR = RED;
insert_node->right = m_nullNode;
insert_node->left = m_nullNode;
if (insert_point == m_nullNode) //empty tree
{
m_root = insert_node;
m_root->parent = m_nullNode;
m_nullNode->left = m_root;
m_nullNode->right = m_root;
m_nullNode->parent = m_root;
}
else
{
if (key<insert_point->key)
{
insert_point->left = insert_node;
}
else
{
insert_point->right = insert_node;
}
insert_node->parent = insert_point;
}
InsertFixUp(insert_node);
return true;
}
void InsertFixUp(RB_Node* node)
{
while (node->parent->RB_COLOR == RED)
{
if (node->parent == node->parent->parent->left)
{
RB_Node* uncle = node->parent->parent->right;
if (uncle->RB_COLOR == RED)
{
node->parent->RB_COLOR = BLACK;
uncle->RB_COLOR = BLACK;
node->parent->parent->RB_COLOR = RED;
node = node->parent->parent;
}
else if (uncle->RB_COLOR == BLACK)
{
if (node == node->parent->right)
{
node = node->parent;
RotateLeft(node);
}
node->parent->RB_COLOR = BLACK;
node->parent->parent->RB_COLOR = RED;
RotateRight(node->parent->parent);
}
}
else
{
RB_Node* uncle = node->parent->parent->left;
if (uncle->RB_COLOR == RED)
{
node->parent->RB_COLOR = BLACK;
uncle->RB_COLOR = BLACK;
uncle->parent->RB_COLOR = RED;
node = node->parent->parent;
}
else if (uncle->RB_COLOR == BLACK)
{
if (node == node->parent->left)
{
node = node->parent;
RotateRight(node);
}
else
{
node->parent->RB_COLOR = BLACK;
node->parent->parent->RB_COLOR = RED;
RotateLeft(node->parent->parent);
}
}
}
}
m_root->RB_COLOR = BLACK;
}
bool RotateLeft(RB_Node* node)
{
if (node == m_nullNode || node->right == m_nullNode)
{
return false;//can't rotate
}
RB_Node* lower_right = node->right;
lower_right->parent = node->parent;
node->right = lower_right->left;
if (lower_right->left != m_nullNode)
{
lower_right->left->parent = node;
}
if (node->parent == m_nullNode) //rotate node is root
{
m_root = lower_right;
m_nullNode->left = m_root;
m_nullNode->right = m_root;
//m_nullNode->parent = m_root;
}
else
{
if (node == node->parent->left)
{
node->parent->left = lower_right;
}
else
{
node->parent->right = lower_right;
}
}
node->parent = lower_right;
lower_right->left = node;
return true;
}
bool RotateRight(RB_Node* node)
{
if (node == m_nullNode || node->left == m_nullNode)
{
return false;//can't rotate
}
RB_Node* lower_left = node->left;
node->left = lower_left->right;
lower_left->parent = node->parent;
if (lower_left->right != m_nullNode)
{
lower_left->right->parent = node;
}
if (node->parent == m_nullNode) //node is root
{
m_root = lower_left;
m_nullNode->left = m_root;
m_nullNode->right = m_root;
//m_nullNode->parent = m_root;
}
else
{
if (node == node->parent->right)
{
node->parent->right = lower_left;
}
else
{
node->parent->left = lower_left;
}
}
node->parent = lower_left;
lower_left->right = node;
return true;
}
bool Delete(KEY key)
{
RB_Node* delete_point = find(key);
if (delete_point == m_nullNode)
{
return false;
}
if (delete_point->left != m_nullNode && delete_point->right != m_nullNode)
{
RB_Node* successor = InOrderSuccessor(delete_point);
delete_point->data = successor->data;
delete_point->key = successor->key;
delete_point = successor;
}
RB_Node* delete_point_child;
if (delete_point->right != m_nullNode)
{
delete_point_child = delete_point->right;
}
else if (delete_point->left != m_nullNode)
{
delete_point_child = delete_point->left;
}
else
{
delete_point_child = m_nullNode;
}
delete_point_child->parent = delete_point->parent;
if (delete_point->parent == m_nullNode)//delete root node
{
m_root = delete_point_child;
m_nullNode->parent = m_root;
m_nullNode->left = m_root;
m_nullNode->right = m_root;
}
else if (delete_point == delete_point->parent->right)
{
delete_point->parent->right = delete_point_child;
}
else
{
delete_point->parent->left = delete_point_child;
}
if (delete_point->RB_COLOR == BLACK && !(delete_point_child == m_nullNode && delete_point_child->parent == m_nullNode))
{
DeleteFixUp(delete_point_child);
}
delete delete_point;
return true;
}
void DeleteFixUp(RB_Node* node)
{
while (node != m_root && node->RB_COLOR == BLACK)
{
if (node == node->parent->left)
{
RB_Node* brother = node->parent->right;
if (brother->RB_COLOR == RED)
{
brother->RB_COLOR = BLACK;
node->parent->RB_COLOR = RED;
RotateLeft(node->parent);
}
else
{
if (brother->left->RB_COLOR == BLACK && brother->right->RB_COLOR == BLACK)
{
brother->RB_COLOR = RED;
node = node->parent;
}
else if (brother->right->RB_COLOR == BLACK)//left red and right black
{
brother->RB_COLOR = RED;
brother->left->RB_COLOR = BLACK;
RotateRight(brother);
}
else if (brother->right->RB_COLOR == RED)
{
brother->RB_COLOR = node->parent->RB_COLOR;
node->parent->RB_COLOR = BLACK;
brother->right->RB_COLOR = BLACK;
RotateLeft(node->parent);
node = m_root;
}
}
}
else
{
RB_Node* brother = node->parent->left;
if (brother->RB_COLOR == RED)
{
brother->RB_COLOR = BLACK;
node->parent->RB_COLOR = RED;
RotateRight(node->parent);
}
else
{
if (brother->left->RB_COLOR == BLACK && brother->right->RB_COLOR == BLACK)
{
brother->RB_COLOR = RED;
node = node->parent;
}
else if (brother->left->RB_COLOR == BLACK)
{
brother->RB_COLOR = RED;
brother->right->RB_COLOR = BLACK;
RotateLeft(brother);
}
else if (brother->left->RB_COLOR == RED)
{
brother->RB_COLOR = node->parent->RB_COLOR;
node->parent->RB_COLOR = BLACK;
brother->left->RB_COLOR = BLACK;
RotateRight(node->parent);
node = m_root;
}
}
}
}
m_nullNode->parent = m_root;
node->RB_COLOR = BLACK;
}
inline RB_Node* InOrderPredecessor(RB_Node* node)
{
if (node == m_nullNode) //null node has no predecessor
{
return m_nullNode;
}
RB_Node* result = node->left; //get node's left child
while (result != m_nullNode) //try to find node's left subtree's right most node
{
if (result->right != m_nullNode)
{
result = result->right;
}
else
{
break;
}
} //after while loop result==null or result's right child is null
if (result == m_nullNode)
{
RB_Node* index = node->parent;
result = node;
while (index != m_nullNode && result == index->left)
{
result = index;
index = index->parent;
}
result = index; // first right parent or null
}
return result;
}
inline RB_Node* InOrderSuccessor(RB_Node* node)
{
if (node == m_nullNode) //null node has no successor
{
return m_nullNode;
}
RB_Node* result = node->right; //get node's right node
while (result != m_nullNode) //try to find node's right subtree's left most node
{
if (result->left != m_nullNode)
{
result = result->left;
}
else
{
break;
}
} //after while loop result==null or result's left child is null
if (result == m_nullNode)
{
RB_Node* index = node->parent;
result = node;
while (index != m_nullNode && result == index->right)
{
result = index;
index = index->parent;
}
result = index; //first parent's left or null
}
return result;
}
void InOrderTraverse()
{
ofstream outfile;
outfile.open(".\\word.txt");
outfile.close();
InOrderTraverse(m_root);
}
void InOrderTraverse(RB_Node* node)
{
if (node == m_nullNode)
{
return;
}
else
{
InOrderTraverse(node->left);
ofstream outfile;
outfile.open(".\\word.txt", ios::app);
CStringA sa(node->key);
CStringA sb(node->data);
outfile << sa << " " << sb << endl;
outfile.close();
InOrderTraverse(node->right);
}
}
~RB_Tree()
{
clear(m_root);
delete m_nullNode;
}
private:
void clear(RB_Node* node)
{
if (node == m_nullNode)
{
return;
}
else
{
clear(node->left);
clear(node->right);
delete node;
}
}
private:
RB_Node *m_nullNode;
RB_Node *m_root;
};
MFC拾遗的更多相关文章
- vs2010静态链接MFC库报链接错误
由于需要将MFC程序在其它电脑上运行,所以需要将动态链接的MFC改成静态链接,本以为很简单,没想到链接的时候出现下面的链接错误: uafxcw.lib(afxmem.obj) : error LNK2 ...
- MFC中成员变量的声明顺序与析构顺序
第一次用博客,第一篇随笔,就写今天遇到的一个问题吧. 在VS2008的MFC对话框程序,窗口成员变量的声明顺序与其析构顺序相反,即,先声明的变量后析构,后声明的变量先析构.未在其他模式下测试. cla ...
- VC中的MFC到底是什么?
1. 微软基础类库(英语:Microsoft Foundation Classes,简称MFC)是一个微软公司提供的类库(class libraries),以C++类的形式封装了Windows API ...
- MFC&Halcon之实时视频监控
上一篇实现了在MFC的窗体内显示图片,本篇介绍如何在MFC窗体内实时显示摄像头的影像. 要实现的功能是点击一个“开始”按钮,可以显示影像,再点击“停止”按钮,可以停止显示. 因为实时显示影像需要在一个 ...
- Redis命令拾遗二(散列类型)
本文版权归博客园和作者吴双共同所有,欢迎转载,转载和爬虫请注明原文地址 :博客园蜗牛NoSql系列地址 http://www.cnblogs.com/tdws/tag/NoSql/ Redis命令拾 ...
- 基础拾遗------redis详解
基础拾遗 基础拾遗------特性详解 基础拾遗------webservice详解 基础拾遗------redis详解 基础拾遗------反射详解 基础拾遗------委托详解 基础拾遗----- ...
- MFC快速入门 - 菜单
本文仅用于学习交流,商业用途请支持正版!转载请注明:http://www.cnblogs.com/mxbs/p/6231104.html 打开VS2010,依次打开File – New – Proje ...
- MFC画线功能总结
本文仅用于学习交流,商业用途请支持正版!转载请注明:http://www.cnblogs.com/mxbs/p/6216464.html MFC画线功能要点有二:其一,鼠标按下时记录初始位置为线的起始 ...
- MFC消息映射机制以及画线功能实现
---此仅供用于学习交流,切勿用于商业用途,转载请注明http://www.cnblogs.com/mxbs/p/6213404.html. 利用VS2010创建一个单文档标准MFC工程,工程名为Dr ...
随机推荐
- Python+Selenium之摘取网页上全部邮箱
本文转载:http://blog.csdn.net/u011541946/article/details/68485981 练习场景:在某一个网页上有些字段是我们感兴趣的,我们希望摘取出来,进行其他操 ...
- Python-OpenCV——Image inverting
通常我们将读入的彩色图转化成灰度图,需要将灰度图反转得到掩码,如何正确快速的得到某个图像的反转图呢? 首先看一种看似很正确的写法,对其中每个像素进行如下处理: img[x,y] = abs(img[x ...
- PHP程序Laravel框架的优化技巧
Laravel是一套简洁.优雅的php Web开发框架(PHP Web Framework).它可以让你从杂乱的代码中解脱出来,可以帮你构建一个完美的网络app,而且每行代码都简洁.富于表达力.而性能 ...
- Problem X: C语言习题 学生成绩输入和输出
Problem X: C语言习题 学生成绩输入和输出 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 4722 Solved: 2284[Submit] ...
- python实现单链表翻转
题目描述: 翻转一个链表 您在真实的面试中是否遇到过这个题? Yes 样例 给出一个链表1->2->3->null,这个翻转后的链表为3->2->1->null 挑 ...
- HTML_2
html图像 <img>标签可以在网页上插入一张图片,它是独立使用的标签,通过‘src’属性定义图片的地址(可为绝对路径也可为相对路径),通过‘alt’属性定义图片加载时显示的文字,以及对 ...
- perl -p -i -w -e
.txt kllk nciuwbufcbew``````//.]];s[[..; klklkl x,dsncdk,;l,ex xw,eocxmcmck .txt .txt kkkkkkkkkkkkkk ...
- PAT (Basic Level) Practise (中文)- 1010. 一元多项式求导 (25)
http://www.patest.cn/contests/pat-b-practise/1010 设计函数求一元多项式的导数.(注:xn(n为整数)的一阶导数为n*xn-1.) 输入格式:以指数递降 ...
- web.xml 中 resource-ref 的注意事项
配置说明: web.xml 中配置 <resource-ref> <description>Employees Database for HR Applications< ...
- 将unity3d项目嵌入到Android App中使用
创建一个新的AndroidStudio app项目. 1.添加库文件:拷贝unity安装目录下的库文件:Unity\Editor\Data\PlaybackEngines\AndroidPlayer\ ...