自己做的一个课程设计,覆盖问题覆盖种类的求解,但运行后发现求解的数稍微大点时 就狂吃内存。达到2G直接导致程序无法运行,请帮忙看下是什么原因!
#define ElemType rectclasstypedef struct LNodeS
{
ElemType data;
LNodeS *next;
int count;
}LStack,*LinkStack; //栈class rectage //棋盘类
{
private:
int size; //棋盘尺寸3*2size
rect *rt; //棋子首地址
int ncount; //覆盖种类
int time;
int width; //图形显示中 小方块的边长 CBrush *red;
CBrush *blue;
CDC *pDC;
CDC dc;
CBitmap bitmap,*pold;
CRect wrect;
CView *view;
public:
void Destory(LinkStack L); rectage(int size,int tine);
~rectage(); void Init(); //初始化棋盘 ,将各小方块编号
void cove(); //开始覆盖
void setrect(rect *r); //设置棋盘中棋子排列
int getcount();
rect* getrect();
int docove(int n); //对有n个竖列的情况遍历
bool addlist(LinkStack s); //向线性存储表中添加记录 void insertSort(rectclass a[],int count); /*count为排序数字个数*/
LinkStack InitStack(); //初始化栈
LinkStack Push(LStack *L,ElemType e); //入栈
LinkStack Pop(LStack *L,ElemType &e); //出栈
bool GetTop(LStack *L,ElemType &e); //获得头元素
bool travel(LStack* L);
rectage *coved;
rectage *next;
};rectage::rectage(int size,int time)
{
this->size = size;
this->time = time;
ncount = 0;
coved = NULL;
next = NULL;
rt = new rect[6*size];
if(size<2)
width = 500/3;
else
width = 350/size; red = new CBrush(RGB(255,0,0));
blue = new CBrush(RGB(0,0,255)); pDC = AfxGetApp()->GetMainWnd()->GetDC();
AfxGetApp()->GetMainWnd()->GetClientRect(wrect);
if(dc.CreateCompatibleDC(pDC))
{
if(bitmap.CreateCompatibleBitmap(pDC,wrect.Width(),wrect.Height()))
{
POSITION pos = AfxGetApp()->GetFirstDocTemplatePosition();
CDocTemplate *doctemp =AfxGetApp()->GetNextDocTemplate(pos);
pos = doctemp->GetFirstDocPosition();
CDocument *doc = doctemp->GetNextDoc(pos);
pos = doc->GetFirstViewPosition();
view = doc->GetNextView(pos);
view->OnPrepareDC(&dc,NULL);
pold = dc.SelectObject(&bitmap);
}
}
}rectage::~rectage()
{
dc.SelectObject(pold);
dc.DeleteDC();
DeleteObject(&bitmap);
delete []rt;
delete red;
delete blue;
}void rectage::insertSort(rectclass a[],int count) /*count为排序数字个数*/
{
int i,j;
rectclass temp;
for(i=1;i<count;i++) /*依次插入数字到它前面已经排好序的数字中去*/
{
temp=a[i];
j=i-1;
while(a[j].getlienum()>temp.getlienum() && j>=0)
{
a[j+1]=a[j];
j--;
}
if(j!=(i-1)) /*第i个数字比前面的都大,不需要重新插入*/
{
a[j+1]=temp;
}
}
}LinkStack rectage::InitStack() //初始化栈
{
LStack *L;
L = (LStack*)malloc(sizeof(LStack));
// L->data = NULL;
L->next = NULL;
L->count = 0;
return L;
}LinkStack rectage::Push(LStack *L,ElemType e) //入栈
{
LStack *p;
p = (LStack*)malloc(sizeof(LStack));
if(!p)
return NULL;
p->data = e;
p->next = L;
p->count = L->count+1;
L = p;
return L;
}LinkStack rectage::Pop(LStack *L,ElemType &e) //出栈
{
LStack *p;
if(L!=NULL)
{
p = L;
e = p->data;
L = L->next;
free(p);
return L;
}
else return NULL;
}bool rectage::GetTop(LStack *L,ElemType &e) //获得头元素
{
e = L->data;
return true;
}void rectage::Destory(LinkStack L)
{
LinkStack p;
while(L->next!=NULL)
{
p = L;
L=L->next;
free(p);
}
free(L);
}
bool rectage::travel(LStack* L) //判断栈中元素是否满足要求:即栈底为偶数,任两相邻列组间距为偶数 上下方块组均为偶数个
{
LStack *p,*q;
int flag=0;
int up=0,down=0;
p = L;
rectclass* rclass = new rectclass[L->count]; //将方块组按列数从小到大排成一个数组,判断相邻两列间距是否为偶数
int i = 0;
while(p->next!=NULL && p->next->data.getnum()!=-842150451) //-842150451为vc++6.0默认int值
{
q = p;
if(p->data.getone()->getpos().x>0)
down++;
else
up++;
rclass[i] = p->data;
i++;
p = p->next;
if(abs(p->data.getnum()-q->data.getnum())%2!=1) //两方块相距不为偶数
flag++;
}
rclass[L->count-1] = p->data;
insertSort(rclass,L->count); //将方块组按列数从小到大排列
for(i=0;i<L->count-1;i++)
{
if((rclass[i+1].getlienum()-rclass[i].getlienum())%2!=1)//相邻两列方块组列数之差不为偶数
flag++;
}
if(p->data.getone()->getpos().x>0)
down++;
else
up++;
if(down%2!=0 || up%2!=0) //上下方块组不均为偶数个
flag++;
if(p->data.getnum()%2>0) //栈底不为偶数
flag++;
delete []rclass;
if(flag>0)
return false;
else
return true;
}rect* rectage::getrect()
{
return rt;
}int rectage::getcount()
{
return this->ncount;
}int rectage::docove(int n) //对有n个列组的情况进行遍历
{
int ncount = 0;
if(n == 0)
{
addlist(NULL);
ncount += 1;
}
else
{
LinkStack stack = new LStack;
stack = InitStack();
int point = 0;
bool notend = true;
while(notend)
{
rectclass *rclass = new rectclass();
if(rclass->makeclass(&rt[point],&rt[point+2*size])==true)
{
stack = Push(stack,*rclass);
if(stack->count == n) //栈满,存储
{
if(travel(stack)==true) //判断栈中元素是否满足要求:即栈底为偶数,任两相邻间距为偶数 上下方块组均为偶数个
{
addlist(stack);
ncount++;
}
rectclass e;
stack = Pop(stack,e);
//将出栈后的方块组还原
e.backup(); }
}
else
delete rclass;
point++;
if(point >= 4*size) //point 指针越界
{
if(stack->count == 0) //栈空 说明已经遍历完成
{
notend = false;
this->Destory(stack);
}
else
{
rectclass e;
stack = Pop(stack,e); //退栈 继续向下遍历
point = e.getnum()+1;
e.backup();
}
}
}
}
return ncount;
}void rectage::Init()
{ for(int i = 0;i<3;i++)
for(int j = 0;j<2*size;j++)
{
rt[i*2*size+j].SetPos(i,j);
rt[i*2*size+j].SetNum(i*2*size+j);
rt[i*2*size+j].SetType(false); //设置默认类型为行
}
}void rectage::cove()
{
for(int i=0;i<=2*size;i+=2) //列数只可能有偶数个
docove(i);
}void rectage::setrect(rect *r)
{
for(int i=0;i<6*size;i++)
rt[i] = r[i];
}bool rectage::addlist(LinkStack stack) //绘制图形 显示输出
{
int n,flag=0,nsize;
this->ncount++;
if(stack==NULL)
nsize = 3*size;
else
{
nsize = 3*size-stack->count;
LinkStack p;
p = stack;
while(p!=NULL && p->data.getnum()!=-842150451) //-842150451为vc++6.0默认int值
{
dc.SelectObject(red);
dc.Rectangle(p->data.getone()->getpos().y*width+40,p->data.getone()->getpos().x*width+40,(p->data.gettwo()->getpos().y+1)*width+40,(p->data.gettwo()->getpos().x+1)*width+40);
p = p->next;
}
}
rectclass *rclass = new rectclass[nsize];
CString str;
str.Format("第%d种覆盖方法:",ncount);
dc.TextOut(10,10,str);
for(int i = 0;i<3;i++)
for(int j = 0;j<2*size;j++)
{
n = i*2*size+j;
if(rt[n].getflag()==0)
if(rclass[flag].makeclass(&rt[n],&rt[n+1])==true) //将未覆盖的小方块编组为行,使显示更加合理
flag++; }
for(i=0;i<nsize;i++)
{
dc.SelectObject(blue);
dc.Rectangle(rclass[i].getone()->getpos().y*width+40,rclass[i].getone()->getpos().x*width+40,(rclass[i].gettwo()->getpos().y+1)*width+40,(rclass[i].gettwo()->getpos().x+1)*width+40);
}
pDC->BitBlt(0,0,wrect.Width(),wrect.Height(),&dc,0,0,SRCCOPY);
Sleep(time);
for(i=0;i<nsize;i++)
rclass[i].backup();
delete []rclass;
return true;
}
#define ElemType rectclasstypedef struct LNodeS
{
ElemType data;
LNodeS *next;
int count;
}LStack,*LinkStack; //栈class rectage //棋盘类
{
private:
int size; //棋盘尺寸3*2size
rect *rt; //棋子首地址
int ncount; //覆盖种类
int time;
int width; //图形显示中 小方块的边长 CBrush *red;
CBrush *blue;
CDC *pDC;
CDC dc;
CBitmap bitmap,*pold;
CRect wrect;
CView *view;
public:
void Destory(LinkStack L); rectage(int size,int tine);
~rectage(); void Init(); //初始化棋盘 ,将各小方块编号
void cove(); //开始覆盖
void setrect(rect *r); //设置棋盘中棋子排列
int getcount();
rect* getrect();
int docove(int n); //对有n个竖列的情况遍历
bool addlist(LinkStack s); //向线性存储表中添加记录 void insertSort(rectclass a[],int count); /*count为排序数字个数*/
LinkStack InitStack(); //初始化栈
LinkStack Push(LStack *L,ElemType e); //入栈
LinkStack Pop(LStack *L,ElemType &e); //出栈
bool GetTop(LStack *L,ElemType &e); //获得头元素
bool travel(LStack* L);
rectage *coved;
rectage *next;
};rectage::rectage(int size,int time)
{
this->size = size;
this->time = time;
ncount = 0;
coved = NULL;
next = NULL;
rt = new rect[6*size];
if(size<2)
width = 500/3;
else
width = 350/size; red = new CBrush(RGB(255,0,0));
blue = new CBrush(RGB(0,0,255)); pDC = AfxGetApp()->GetMainWnd()->GetDC();
AfxGetApp()->GetMainWnd()->GetClientRect(wrect);
if(dc.CreateCompatibleDC(pDC))
{
if(bitmap.CreateCompatibleBitmap(pDC,wrect.Width(),wrect.Height()))
{
POSITION pos = AfxGetApp()->GetFirstDocTemplatePosition();
CDocTemplate *doctemp =AfxGetApp()->GetNextDocTemplate(pos);
pos = doctemp->GetFirstDocPosition();
CDocument *doc = doctemp->GetNextDoc(pos);
pos = doc->GetFirstViewPosition();
view = doc->GetNextView(pos);
view->OnPrepareDC(&dc,NULL);
pold = dc.SelectObject(&bitmap);
}
}
}rectage::~rectage()
{
dc.SelectObject(pold);
dc.DeleteDC();
DeleteObject(&bitmap);
delete []rt;
delete red;
delete blue;
}void rectage::insertSort(rectclass a[],int count) /*count为排序数字个数*/
{
int i,j;
rectclass temp;
for(i=1;i<count;i++) /*依次插入数字到它前面已经排好序的数字中去*/
{
temp=a[i];
j=i-1;
while(a[j].getlienum()>temp.getlienum() && j>=0)
{
a[j+1]=a[j];
j--;
}
if(j!=(i-1)) /*第i个数字比前面的都大,不需要重新插入*/
{
a[j+1]=temp;
}
}
}LinkStack rectage::InitStack() //初始化栈
{
LStack *L;
L = (LStack*)malloc(sizeof(LStack));
// L->data = NULL;
L->next = NULL;
L->count = 0;
return L;
}LinkStack rectage::Push(LStack *L,ElemType e) //入栈
{
LStack *p;
p = (LStack*)malloc(sizeof(LStack));
if(!p)
return NULL;
p->data = e;
p->next = L;
p->count = L->count+1;
L = p;
return L;
}LinkStack rectage::Pop(LStack *L,ElemType &e) //出栈
{
LStack *p;
if(L!=NULL)
{
p = L;
e = p->data;
L = L->next;
free(p);
return L;
}
else return NULL;
}bool rectage::GetTop(LStack *L,ElemType &e) //获得头元素
{
e = L->data;
return true;
}void rectage::Destory(LinkStack L)
{
LinkStack p;
while(L->next!=NULL)
{
p = L;
L=L->next;
free(p);
}
free(L);
}
bool rectage::travel(LStack* L) //判断栈中元素是否满足要求:即栈底为偶数,任两相邻列组间距为偶数 上下方块组均为偶数个
{
LStack *p,*q;
int flag=0;
int up=0,down=0;
p = L;
rectclass* rclass = new rectclass[L->count]; //将方块组按列数从小到大排成一个数组,判断相邻两列间距是否为偶数
int i = 0;
while(p->next!=NULL && p->next->data.getnum()!=-842150451) //-842150451为vc++6.0默认int值
{
q = p;
if(p->data.getone()->getpos().x>0)
down++;
else
up++;
rclass[i] = p->data;
i++;
p = p->next;
if(abs(p->data.getnum()-q->data.getnum())%2!=1) //两方块相距不为偶数
flag++;
}
rclass[L->count-1] = p->data;
insertSort(rclass,L->count); //将方块组按列数从小到大排列
for(i=0;i<L->count-1;i++)
{
if((rclass[i+1].getlienum()-rclass[i].getlienum())%2!=1)//相邻两列方块组列数之差不为偶数
flag++;
}
if(p->data.getone()->getpos().x>0)
down++;
else
up++;
if(down%2!=0 || up%2!=0) //上下方块组不均为偶数个
flag++;
if(p->data.getnum()%2>0) //栈底不为偶数
flag++;
delete []rclass;
if(flag>0)
return false;
else
return true;
}rect* rectage::getrect()
{
return rt;
}int rectage::getcount()
{
return this->ncount;
}int rectage::docove(int n) //对有n个列组的情况进行遍历
{
int ncount = 0;
if(n == 0)
{
addlist(NULL);
ncount += 1;
}
else
{
LinkStack stack = new LStack;
stack = InitStack();
int point = 0;
bool notend = true;
while(notend)
{
rectclass *rclass = new rectclass();
if(rclass->makeclass(&rt[point],&rt[point+2*size])==true)
{
stack = Push(stack,*rclass);
if(stack->count == n) //栈满,存储
{
if(travel(stack)==true) //判断栈中元素是否满足要求:即栈底为偶数,任两相邻间距为偶数 上下方块组均为偶数个
{
addlist(stack);
ncount++;
}
rectclass e;
stack = Pop(stack,e);
//将出栈后的方块组还原
e.backup(); }
}
else
delete rclass;
point++;
if(point >= 4*size) //point 指针越界
{
if(stack->count == 0) //栈空 说明已经遍历完成
{
notend = false;
this->Destory(stack);
}
else
{
rectclass e;
stack = Pop(stack,e); //退栈 继续向下遍历
point = e.getnum()+1;
e.backup();
}
}
}
}
return ncount;
}void rectage::Init()
{ for(int i = 0;i<3;i++)
for(int j = 0;j<2*size;j++)
{
rt[i*2*size+j].SetPos(i,j);
rt[i*2*size+j].SetNum(i*2*size+j);
rt[i*2*size+j].SetType(false); //设置默认类型为行
}
}void rectage::cove()
{
for(int i=0;i<=2*size;i+=2) //列数只可能有偶数个
docove(i);
}void rectage::setrect(rect *r)
{
for(int i=0;i<6*size;i++)
rt[i] = r[i];
}bool rectage::addlist(LinkStack stack) //绘制图形 显示输出
{
int n,flag=0,nsize;
this->ncount++;
if(stack==NULL)
nsize = 3*size;
else
{
nsize = 3*size-stack->count;
LinkStack p;
p = stack;
while(p!=NULL && p->data.getnum()!=-842150451) //-842150451为vc++6.0默认int值
{
dc.SelectObject(red);
dc.Rectangle(p->data.getone()->getpos().y*width+40,p->data.getone()->getpos().x*width+40,(p->data.gettwo()->getpos().y+1)*width+40,(p->data.gettwo()->getpos().x+1)*width+40);
p = p->next;
}
}
rectclass *rclass = new rectclass[nsize];
CString str;
str.Format("第%d种覆盖方法:",ncount);
dc.TextOut(10,10,str);
for(int i = 0;i<3;i++)
for(int j = 0;j<2*size;j++)
{
n = i*2*size+j;
if(rt[n].getflag()==0)
if(rclass[flag].makeclass(&rt[n],&rt[n+1])==true) //将未覆盖的小方块编组为行,使显示更加合理
flag++; }
for(i=0;i<nsize;i++)
{
dc.SelectObject(blue);
dc.Rectangle(rclass[i].getone()->getpos().y*width+40,rclass[i].getone()->getpos().x*width+40,(rclass[i].gettwo()->getpos().y+1)*width+40,(rclass[i].gettwo()->getpos().x+1)*width+40);
}
pDC->BitBlt(0,0,wrect.Width(),wrect.Height(),&dc,0,0,SRCCOPY);
Sleep(time);
for(i=0;i<nsize;i++)
rclass[i].backup();
delete []rclass;
return true;
}
在debug版本下,堆栈中的每个字节都被初始化为CC, 让人产生了int有默认值的错觉,其实只是0xcccccccc
debug下下面的代码:
int i;
cout << i;//输出结果为0xcccccccc对应的10进制值;
在release版本下,堆栈中的数据是没有初始化的
建议楼主养成初始化变量的好习惯
2、用专业工具检测内存泄漏问题
我现在吧所有代码发上,请帮忙调试下!
rectage.h#pragma once#include "rect.h"
#include "rectclass.h"#define ElemType rectclasstypedef struct LNodeS
{
ElemType data;
LNodeS *next;
int count;
}LStack; //栈class rectage //棋盘类
{
private:
int size; //棋盘尺寸
rect *rt; //棋子首地址
int ncount; //覆盖种类
int time;
int width; //图形显示中 小方块的边长 CBrush red,blue,*pbold;
CDC *pDC;
CDC dc;
CBitmap bitmap,*pold;
CRect wrect;
CView *view;
public:
void Destory(LStack* L); rectage(int size,int tine);
~rectage(); void Init(); //初始化棋盘 ,将各小方块编号
void cove(); //开始覆盖
void setrect(rect *r); //设置棋盘中棋子排列
int getcount();
rect* getrect();
int docove(int n); //对有n个竖列的情况遍历
bool addlist(LStack* s); //向线性存储表中添加记录 void insertSort(rectclass a[],int count); /*count为排序数字个数*/
void InitStack(LStack* L); //初始化栈
LStack* Push(LStack *L,ElemType e); //入栈
LStack* Pop(LStack *L,ElemType &e); //出栈
bool GetTop(LStack *L,ElemType &e); //获得头元素
bool travel(LStack* L); //判断栈中元素是否满足要求:即栈底为偶数,任两相邻列组间距为偶数 上下方块组均为偶数个
};rectage.cpp#include "stdafx.h"
#include "rectage.h"
#include <stdio.h>rectage::rectage(int size,int time)
{
this->size = size;
this->time = time;
ncount = 0;
rt = new rect[6*size];
if(size<2)
width = 500/3;
else
width = 350/size; red.CreateSolidBrush(RGB(255,0,0));
blue.CreateSolidBrush(RGB(0,0,255)); pDC = AfxGetApp()->GetMainWnd()->GetDC();
AfxGetApp()->GetMainWnd()->GetClientRect(wrect);
if(dc.CreateCompatibleDC(pDC))
{
if(bitmap.CreateCompatibleBitmap(pDC,wrect.Width(),wrect.Height()))
{
POSITION pos = AfxGetApp()->GetFirstDocTemplatePosition();
CDocTemplate *doctemp =AfxGetApp()->GetNextDocTemplate(pos);
pos = doctemp->GetFirstDocPosition();
CDocument *doc = doctemp->GetNextDoc(pos);
pos = doc->GetFirstViewPosition();
view = doc->GetNextView(pos);
view->OnPrepareDC(&dc,NULL);
pold = dc.SelectObject(&bitmap);
}
}
pbold = dc.SelectObject(&blue);
}rectage::~rectage()
{
dc.SelectObject(pbold);
dc.SelectObject(pold);
dc.DeleteDC();
DeleteObject(&bitmap);
delete []rt;
}void rectage::insertSort(rectclass a[],int count) /*count为排序数字个数*/
{
int i,j;
rectclass temp;
for(i=1;i<count;i++) /*依次插入数字到它前面已经排好序的数字中去*/
{
temp=a[i];
j=i-1;
while(a[j].getlienum()>temp.getlienum() && j>=0)
{
a[j+1]=a[j];
j--;
}
if(j!=(i-1)) /*第i个数字比前面的都大,不需要重新插入*/
{
a[j+1]=temp;
}
}
}void rectage::InitStack(LStack* L) //初始化栈
{
// L->data = NULL;
L->next = NULL;
L->count = 0;
}LStack* rectage::Push(LStack *L,ElemType e) //入栈
{
LStack *p;
p = new LStack;
if(!p)
return NULL;
p->data = e;
p->next = L;
p->count = L->count+1;
L = p;
return L;
}LStack* rectage::Pop(LStack *L,ElemType &e) //出栈
{
LStack *p;
if(L!=NULL)
{
p = L;
e = p->data;
L = L->next;
delete p;
return L;
}
else return NULL;
}bool rectage::GetTop(LStack *L,ElemType &e) //获得头元素
{
e = L->data;
return true;
}void rectage::Destory(LStack* L)
{
LStack* p;
while(L->next!=NULL)
{
p = L;
L = L->next;
delete p;
}
delete L;
}bool rectage::travel(LStack* L) //判断栈中元素是否满足要求:即栈底为偶数,任两相邻列组间距为偶数 上下方块组均为偶数个
{
LStack *p,*q;
int flag=0;
int up=0,down=0;
p = L;
rectclass* rclass = new rectclass[L->count]; //将方块组按列数从小到大排成一个数组,判断相邻两列间距是否为偶数
int i = 0;
while(p->next!=NULL && p->next->data.getnum()!=-1) //-1为头指针的值
{
q = p;
if(p->data.getone()->getpos().x>0)
down++;
else
up++;
rclass[i] = p->data;
i++;
p = p->next;
if(abs(p->data.getnum()-q->data.getnum())%2!=1) //两方块相距不为偶数
flag++;
}
rclass[L->count-1] = p->data;
insertSort(rclass,L->count); //将方块组按列数从小到大排列
for(i=0;i<L->count-1;i++)
{
if((rclass[i+1].getlienum()-rclass[i].getlienum())%2!=1)//相邻两列方块组列数之差不为偶数
flag++;
}
if(p->data.getone()->getpos().x>0)
down++;
else
up++;
if(down%2!=0 || up%2!=0) //上下方块组不均为偶数个
flag++;
if(p->data.getnum()%2>0) //栈底不为偶数
flag++;
if(flag>0)
return false;
else
return true;
}rect* rectage::getrect()
{
return rt;
}int rectage::getcount()
{
return this->ncount;
}int rectage::docove(int n) //对有n个列组的情况进行遍历
{
int ncount = 0;
if(n == 0)
{
addlist(NULL);
ncount += 1;
}
else
{
LStack* stack = new LStack;
InitStack(stack);
int point = 0;
bool notend = true;
while(notend)
{
rectclass *rclass = new rectclass();
if(rclass->makeclass(&rt[point],&rt[point+2*size])==true)
{
stack = Push(stack,*rclass);
if(stack->count == n) //栈满,存储
{
if(travel(stack)==true) //判断栈中元素是否满足要求:即栈底为偶数,任两相邻间距为偶数 上下方块组均为偶数个
{
addlist(stack);
ncount++;
}
rectclass e;
stack = Pop(stack,e);
//将出栈后的方块组还原
e.backup(); }
}
else
delete rclass;
point++;
if(point >= 4*size) //point 指针越界
{
if(stack->count == 0) //栈空 说明已经遍历完成
{
notend = false;
this->Destory(stack);
}
else
{
rectclass e;
stack = Pop(stack,e); //退栈 继续向下遍历
point = e.getnum()+1;
e.backup();
}
}
}
}
return ncount;
}void rectage::Init()
{ for(int i = 0;i<3;i++)
for(int j = 0;j<2*size;j++)
{
rt[i*2*size+j].SetPos(i,j);
rt[i*2*size+j].SetNum(i*2*size+j);
rt[i*2*size+j].SetType(false); //设置默认类型为行
}
}void rectage::cove()
{
for(int i=0;i<=2*size;i+=2) //列数只可能有偶数个
docove(i);
}void rectage::setrect(rect *r)
{
for(int i=0;i<6*size;i++)
rt[i] = r[i];
}bool rectage::addlist(LStack* stack) //绘制图形 显示输出
{
int n,flag=0,nsize;
this->ncount++;
if(stack==NULL)
nsize = 3*size;
else
{
nsize = 3*size-stack->count;
LStack* p;
p = stack;
while(p!=NULL && p->data.getnum()!= -1) //-1为头指针的值
{
dc.SelectObject(red);
dc.Rectangle(p->data.getone()->getpos().y*width+40,p->data.getone()->getpos().x*width+40,(p->data.gettwo()->getpos().y+1)*width+40,(p->data.gettwo()->getpos().x+1)*width+40);
p = p->next;
}
}
rectclass *rclass = new rectclass[nsize];
CString str;
str.Format("第%d种覆盖方法:",ncount);
dc.TextOut(10,10,str);
for(int i = 0;i<3;i++)
for(int j = 0;j<2*size;j++)
{
n = i*2*size+j;
if(rt[n].getflag()==0)
if(rclass[flag].makeclass(&rt[n],&rt[n+1])==true) //将未覆盖的小方块编组为行,使显示更加合理
flag++; }
for(i=0;i<nsize;i++)
{
dc.SelectObject(blue);
dc.Rectangle(rclass[i].getone()->getpos().y*width+40,rclass[i].getone()->getpos().x*width+40,(rclass[i].gettwo()->getpos().y+1)*width+40,(rclass[i].gettwo()->getpos().x+1)*width+40);
}
pDC->BitBlt(0,0,wrect.Width(),wrect.Height(),&dc,0,0,SRCCOPY);
Sleep(time);
for(i=0;i<nsize;i++)
rclass[i].backup(); delete []rclass;
return true;
}
{
int x;
int y;
}Pos;class rect //小方块
{
private:
bool islie; //是否是列
int num; //编号
Pos ps; //位置
int flag; //0.表示未被覆盖 1.已被覆盖
public:
rect();
rect(int num);
~rect();
void SetFlag(bool cove); //设置方块是被覆盖基数:true 覆盖一次;false 解覆盖一次
void SetPos(Pos ps); //设置方块在棋盘中的位置
void SetPos(int x,int y); //设置方块在棋盘中的位置
void SetNum(int num); //设置方块在棋盘中的编号
void SetType(bool lie); //设置方块是否和别的方块组成列,否则表示组成行 Pos getpos();
int getnum();
int getflag(); bool Islie(); //返回是否是列
};rect.cpp#include "stdafx.h"
#include "rect.h"rect::rect()
{
this->flag = 0;
this->ps.x = -1;
this->ps.y = -1;
this->num = -1;
islie = false;
}rect::rect(int num)
{
this->num = num;
this->flag = 0;
}rect::~rect()
{
}void rect::SetFlag(bool cove)
{
if(cove)
this->flag++;
else
this->flag--;
}void rect::SetPos(Pos ps)
{
this->ps = ps;
}void rect::SetPos(int x,int y)
{
this->ps.x = x;
this->ps.y = y;
}void rect::SetNum(int num)
{
this->num = num;
}Pos rect::getpos()
{
return ps;
}int rect::getnum()
{
return num;
}bool rect::Islie()
{
return this->islie;
}void rect::SetType(bool islie)
{
this->islie = islie;
}int rect::getflag()
{
return this->flag;
}rectcalss.h
#pragma once#include "rect.h"class rectclass //2*1方块所形成的方块组
{
private:
int num; //方块组编号 即方块组中上方块的编号
int lienum; //方块组列数
int type; //方块组类型 0表示横;1表示列
rect *one; //方块组成员方块1
rect *two; //方块组成员方块2
public:
rectclass();
~rectclass();
bool makeclass(rect* one,rect* two); //将one,two所指的小方块组成方块组
void backup(); //还原小方块 int getnum();
int getlienum(); //返回方块组所在的列数
rect *getone(); //返回one所指的方块
rect *gettwo(); //返回two所指的方块
void settype(int type);
int gettype();};rectclass.cpp#include "stdafx.h"
#include "rectclass.h"rectclass::rectclass()
{
num = -1;
this->lienum = -1;
this->one = NULL;
this->two = NULL;
this->type = -1;
}rectclass::~rectclass()
{
}int rectclass::getnum()
{
return num;
}int rectclass::getlienum()
{
return lienum;
}bool rectclass::makeclass(rect* one,rect* two)
{
if(one->getflag()>0 || two->getflag()>0)
return false;
if((one->getpos().y == two->getpos().y) && (abs(one->getpos().x-two->getpos().x) == 1) )//俩小方块能组成列
{
this->one = one;
this->two = two;
num = one->getnum();
lienum = one->getpos().y;
one->SetType(true);
one->SetFlag(true);
two->SetType(true);
two->SetFlag(true);
this->settype(1);
return true;
}
else if(two->getnum()-one->getnum()==1) //两方快能组成行
{
this->one = one;
this->two = two;
num = one->getnum();
lienum = one->getpos().y;
one->SetType(false);
one->SetFlag(true);
two->SetType(false);
two->SetFlag(true);
this->settype(0);
return true;
}
else
{
one = NULL;
two = NULL;
return false;
}
}
void rectclass::backup()
{
one->SetType(false);
one->SetFlag(false);
two->SetType(false);
two->SetFlag(false);
}rect* rectclass::getone()
{
return one;
}rect* rectclass::gettwo()
{
return two;
}void rectclass::settype(int type)
{
this->type = type;
}int rectclass::gettype()
{
return type;
}
这个解决了吗?
另外,要学习工具的使用。
例如,boundchecker在运行玩后,有很多提示,可以找到内存泄漏和资源泄漏,可以定位到泄漏行
工具提示:
Allocation Conflict: Attempting to call free on 0x01284CD8; pointer was allocated by global_operator_new.但不提示是在哪一行
请帮忙调试下吧
新建一个MFC单文档工程 将上面的代码加入 建立一个线程
线程函数体为:其中COption为一个对话框,就两文本行加2按钮
UINT CMyView::Run(LPVOID pParam)
{
int n,time;
COption option;
if(option.DoModal()==IDOK)
{
n = option.m_n;
time = option.m_time;
}
else
return 0;
if(n<1 || time<0)
{
AfxMessageBox("输入非法!");
return 0;
}
rectage ra(n,time);
ra.Init();
CString str;
str.Format("开始遍历3*%d的情况!",2*n);
AfxMessageBox(str);
ra.cove();
str.Format("遍历完成!共有%d种覆盖方法!",ra.getcount());
AfxMessageBox(str);
return 1;
}
blue = new CBrush(RGB(0,0,255)); 做的不好,而且又没释放掉。应该通过中间量传导,然后把这个释放掉。你不停new应该会内存狂加