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 ...
随机推荐
- 备份和导入Outlook 2016 电子邮件签名
在本文中,我将分享您在Outlook 2013和Outlook 2016中备份或导入签名的过程 在清除Outlook配置文件之前,请确保您通过在文件资源管理器中的配置文件中的APPDATA文件夹中复制 ...
- Python+Selenium之摘取网页上全部邮箱
本文转载:http://blog.csdn.net/u011541946/article/details/68485981 练习场景:在某一个网页上有些字段是我们感兴趣的,我们希望摘取出来,进行其他操 ...
- Android商城开发系列(一)——开篇
最近在看尚硅谷的硅谷商城视频,想系统学习一下Android的商城开发流程,打算跟着视频的一步步做出一个商城,然后写博客总结记录一下整个商城的开发过程以及使用到的技术知识点,这个商城的最终效果如下图所示 ...
- 【LeetCode】2.Add Two Numbers 链表数相加
题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...
- 使Win10用户获得特殊权限以便删除相应文件(夹)
依次访问: 本地用户和组(右击“此电脑”): 用户: 右击:当前用户名: 属性: 添加: 输入:System Managed Accounts Group: 检查名称(可选): 确定: 重启电脑. 参 ...
- Codeforces 464E #265 (Div. 1) E. The Classic Problem 主席树+Hash
E. The Classic Problem http://codeforces.com/problemset/problem/464/E 题意:给你一张无向带权图,求S-T的最短路,并输出路径.边权 ...
- HDU 5489 Removed Interval 2015 ACM/ICPC Asia Regional Hefei Online (LIS变形)
定义f[i]表示以i为开头往后的最长上升子序列,d[i]表示以i为结尾的最长上升子序列. 先nlogn算出f[i], 从i-L开始枚举f[i],表示假设i在最终的LIS中,往[0,i-L)里找到满足a ...
- UVA 110020 Efficient Solutions (STL)
把一个人看出一个二维的点,优势的点就是就原点为左下角,这个点为右上角的矩形,包含除了右上角以外边界,其他任意地方不存在点. 那么所有有优势的点将会形成一条下凹的曲线. 因为可能有重点,用multise ...
- Dojo操作dom元素的样式
1.使用dom-style的set方法,可以直接设置dom元素的样式属性,这和使用dom元素的style属性效果一样. 2.使用dom-class的replace方法可以替换某个dom元素的样式,ad ...
- Python读写文件实际操作的五大步骤
Python读写文件在计算机语言中被广泛的应用,如果你想了解其应用的程序,以下的文章会给你详细的介绍相关内容,会你在以后的学习的过程中有所帮助,下面我们就详细介绍其应用程序. 一.打开文件 Pytho ...