C++写的这个程序终止不了,求高手帮帮看看(关于矩阵相乘问题。代码可以直接运行)
#include <stdlib.h>
#include<conio.h>
#include <iostream.h>
#include <windows.h>
#include <time.h>
//using namespace std;
class CMatrix
{
private:
int row;
int col;
int **p;
public:
int GetRow() const
{
return this->row;
}
int GetCol() const
{
return this->col;
}
CMatrix()
{
this->row=0;
this->col=0;
this->p=NULL;
}
CMatrix(int n,int m)
{
this->row=n;
this->col=m;
if(n!=0&&m!=0)
{
this->p=new int*[n];
for(int i=0;i<n;i++)
this->p[i]=new int[m];
}
}
virtual ~CMatrix()
{
if(this->p!=NULL)
{
this->p=NULL;
delete this->p;
}
delete this->p;
cout<<"进入析构函数\n";
}
CMatrix(const CMatrix& lp)
{
cout<<"进入复制构造函数\n";
row=lp.row;
col=lp.col;
p=lp.p;
}
friend CMatrix operator*(const CMatrix &a,const CMatrix &b)
{
if (a.col!=b.row)
{
cout<<"这两个矩阵不能相乘哦!"<<endl;
return CMatrix();
}
CMatrix temp(a.row,b.col);
for (int i=0;i<a.row;i++)
for(int j=0;j<b.col;j++)
{
temp.p[i][j]=0;
for(int t=0;t<b.col;t++)
temp.p[i][j]+=a.p[i][t]*b.p[t][j];
}
return temp;
}
CMatrix Strassen(CMatrix &a,CMatrix &b,CMatrix &c)
{
int n=a.row;
CMatrix C(n,n);
if(n==2)
return a*b;
else
{
CMatrix A11(n/2,n/2),A12(n/2,n/2),A21(n/2,n/2),A22(n/2,n/2);
CMatrix B11(n/2,n/2),B12(n/2,n/2),B21(n/2,n/2),B22(n/2,n/2);
CMatrix C11(n/2,n/2),C12(n/2,n/2),C21(n/2,n/2),C22(n/2,n/2);
CMatrix m1(n/2,n/2),m2(n/2,n/2),m3(n/2,n/2),m4(n/2,n/2),m5(n/2,n/2),m6(n/2,n/2),m7(n/2,n/2);
for(int i=0;i<n/2;i++)
for(int j=0;j<n/2;j++)
{
A11.p[i][j]=a.p[i][j];
A12.p[i][j]=a.p[i][j+n/2];
A21.p[i][j]=a.p[i+n/2][j];
A22.p[i][j]=a.p[i+n/2][j+n/2];
B11.p[i][j]=b.p[i][j];
B12.p[i][j]=b.p[i][j+n/2];
B21.p[i][j]=b.p[i+n/2][j];
B22.p[i][j]=b.p[i+n/2][j+n/2];
}
//B12.output(B12);
m1=(A11+A22)*(B11+B22);
m2=(A21+A22)*B11;
m3=A11*(B12-B22);
m4=A22*(B21-B11);
m5=(A11+A12)*B22;
m6=(A21-A11)*(B11+B12);
m7=(A12-A22)*(B21+B22);
C11=m1+m4-m5+m7;
C12=m3+m5;
C21=m2+m4;
C22=m1+m3-m2+m6;
for( i=0;i<n/2;i++)
for(int j=0;j<n/2;j++)
{
C.p[i][j]=C11.p[i][j];
C.p[i][j+n/2]=C12.p[i][j];
C.p[i+n/2][j]=C21.p[i][j];
C.p[i+n/2][j+n/2]=C22.p[i][j];
}//end_for
}//else
//C.output(C);
c=C;
return C;
}
friend CMatrix operator==(const CMatrix &a,const CMatrix &b)
{
cout<<"哈哈哈";
}
CMatrix& operator=(const CMatrix &a)
{
this->row=a.row;
this->col=a.col;
for(int i=0;i<a.row;i++)
for(int j=0;j<a.col;j++)
this->p[i][j]=a.p[i][j];
return *this;
}
friend CMatrix operator+(const CMatrix &a,const CMatrix &b)
{
CMatrix temp(a.row,a.col);
for(int i=0;i<a.row;i++)
for(int j=0;j<a.col;j++)
temp.p[i][j]=a.p[i][j]+b.p[i][j];
cout<<"进行矩阵加法操作\n";
return temp;
}
friend CMatrix operator-(const CMatrix &a,const CMatrix &b)
{
CMatrix temp(a.row,a.col);
for(int i=0;i<a.row;i++)
for(int j=0;j<a.col;j++)
temp.p[i][j]=a.p[i][j]-b.p[i][j];
return temp;
}
void autoinput(CMatrix &a)
{
int i,j;
srand((unsigned int)i+(unsigned int)time(NULL));
for(i=0;i<a.row;i++)
for(j=0;j<a.col;j++)
a.p[i][j]=rand();
}
void input(CMatrix &a)
{
int i,j;
cout<<"给数组赋值:\n";
for(i=0;i<a.row;i++)
for(j=0;j<a.col;j++)
cin>>a.p[i][j];
}
/* void output(const CMatrix a)
{
cout<<"dsafadf";
int i,j;
for(i=0;i<a.row;i++)
{
for(j=0;j<a.col;j++)
cout<<a.p[i][j]<<" ";
cout<<endl;
}
cout<<endl;
}
*/
void output()
{
cout<<"dsafadf\n";
int i,j;
for(i=0;i<this->row;i++)
{
for(j=0;j<this->col;j++)
cout<<this->p[i][j]<<" ";
cout<<endl;
}
cout<<endl;
}
int Bf(CMatrix &a,CMatrix &b)
{
int i,j,t;
// CMatrix C(a.row,b.col);
for(i=0;i<a.row;i++)
for(j=0;j<a.col;j++)
{
this->p[i][j]=0;
for(t=0;t<b.col;t++)
this->p[i][j]=this->p[i][j]+a.p[i][t]*b.p[t][j];
}
// this=C;
return 0;
}
};
void zidong()
{
int a,b;
cout<<"输入数组a的阶数"<<endl;
cin>>a;
CMatrix aa(a,a);
aa.autoinput(aa);
aa.output();
cout<<"输入数组b的阶数"<<endl;
cin>>b;
CMatrix bb(b,b);
bb.autoinput(bb);
bb.output();
CMatrix cc(a,b),dd(a,b);
time_t start=clock();
cc.Strassen(aa,bb,cc);
time_t end=clock();
double dt=static_cast<double>(end-start)/CLOCKS_PER_SEC*1000;
cout << "\n您的程序执行所耗费的时间为: " << dt << " 毫秒" << endl;
cout<<"分治法结果如下"<<endl;
cc.output();
cout<<"-----------------------"<<endl;
dd.Bf(aa,bb);
cout<<"蛮力法结果如下"<<endl;
dd.output();
cout<<"蛮力法结果如下"<<endl;
system("PAUSE");
}
void shoudong()
{
int a,b;
cout<<"输入数组a的阶数"<<endl;
cin>>a;
CMatrix aa(a,a);
aa.input(aa);
cout<<"输入数组b的阶数"<<endl;
cin>>b;
CMatrix bb(b,b);
bb.input(bb);
CMatrix cc(a,b);
time_t start=clock();
cc.Strassen(aa,bb,cc);
time_t end=clock();
double dt=static_cast<double>(end-start)/CLOCKS_PER_SEC*1000;
cout << "\n您的程序执行所耗费的时间为: " << dt << " 毫秒" << endl;
cout<<"分治法结果如下"<<endl;
// cc.output(cc);
cout<<"-----------------------"<<endl;
cout<<"蛮力法结果如下"<<endl;
cc.Bf(aa,bb);
// cc.output(cc);
system("PAUSE");
}
void deal()
{
int ch;
cout<<"------------------------\n";
cout<<" 1. 自动生成矩阵 \n ";
cout<<" 2. 手动输入 \n ";
cout<<"------------------------\n";
cin>>ch;
if(ch==1)
{
zidong();
}
if(ch==2)
{
shoudong();
}
}
void main()
{
char choose='\n',yes_no='\n';
do
{
deal();
cout<<" 要继续选择吗(Y/N?\n";
do
{
yes_no=getch();
}while(yes_no!='Y'&&yes_no!='y'&&yes_no!='N'&&yes_no!='n');
}while(yes_no=='Y'||yes_no=='y');
} C++
[解决办法]
析构函数中,一下两句位置应该互换
this->p=NULL;
delete this->p;
否则根部释放不了内存。
[解决办法]
还有 变量是用前要初始化把
void autoinput(CMatrix &a)
{
int i,j;
srand((unsigned int)i+(unsigned int)time(NULL));