矩阵的转置和乘法课程设计程序.docx
《矩阵的转置和乘法课程设计程序.docx》由会员分享,可在线阅读,更多相关《矩阵的转置和乘法课程设计程序.docx(16页珍藏版)》请在冰豆网上搜索。
![矩阵的转置和乘法课程设计程序.docx](https://file1.bdocx.com/fileroot1/2023-1/26/010abcb2-e13d-44f9-8911-1910018d7b98/010abcb2-e13d-44f9-8911-1910018d7b981.gif)
矩阵的转置和乘法课程设计程序
矩阵的转置和乘法课程设计程序
#ifndefCMatrix_H_//************************************************条件编译
#defineCMatrix_H_
#include
#include
#include"vec.h"
//usingnamespacestd;
#defineMIN(a,b)((a)<(b))?
(a):
(b);
/*----------------------------------------定义类模板-------------------------------*/
template
classCMatrix
{
structnode
{
Vector**f;//*******************************************组成矩阵的向量指针
intrefcnt;//*************************************************被引用次数
intlength;//****************************************************矩阵的行数
T**tmppointer;//*******************************************头指针类型
}*p;
public:
//
Vector**begin()const{returnp->f;};
CMatrix();//***********************************************************默认的构造
CMatrix(intxsize,intysize,Tinit=0);//*********************************构造函数
CMatrix(intxlength,constVector*vec);//********************************构造函数
CMatrix(CMatrix&x);//***********************************************拷贝构造函数
~CMatrix();//************************************************************析构函数
CMatrix&operator=(constCMatrix&mat);//*************************重载赋值运算符
introw()const;//*******************************************************返回行数
intcol()const;//*******************************************************返回列数
Vector&operator[](inti);//******************************************重载[]
voidInver(CMatrix&mat);//*********************************************矩阵转置
operatorT**();//*********************************************************重载**
voidReadFromFile();//**************************************************从文件中读入矩阵
friendCMatrixcpy(CMatrix&v);//***********************************************拷贝函数
friendstd:
:
ostream&operator<<(std:
:
ostream&s,constCMatrix&mat);//重载输出函数
friendstd:
:
istream&operator>>(std:
:
istream&s,constCMatrix&mat);//重载输入函数
friendCMatrixoperator*(CMatrix&v1,CMatrix&v2);//***************矩阵乘法
friendCMatrixoperator*(constCMatrix&v,Tval);//**********************数乘
};
/*----------------------------------------类外定义缺省的构造函数----------------------------*/
template
CMatrix:
:
CMatrix()
{
p=newnode;
p->length=NULL;
p->f=0;
p->refcnt=1;
p->tmppointer=NULL;
}
/*----------------------------------------定义可扩展构造函数------------------------*/
template
CMatrix:
:
CMatrix(intxsize,intysize,Tinit)
{
if(xsize<=0||ysize<=0)cout<<"error!
!
";
p=newnode;
p->length=xsize;
p->f=newVector*[xsize];
for(inti(0);ip->f[i]=newVector(ysize,init);
p->refcnt=1;
p->tmppointer=NULL;
}
/*------------------------------------定义构造函数----------------------------*/
template
CMatrix:
:
CMatrix(intxlength,constVector*vec)
{
if(xlength<=0)cout<<"error!
!
";
p=newnode;
p->length=xlength;
p->f=newVector*[xlength];
for(inti(0);ip->f[i]=newVector(*vec);
}
/*------------------------------------定义拷贝的构造函数-------------------------*/
template
CMatrix:
:
CMatrix(CMatrix&x)
{
x.p->refcnt++;
p=x.p;
}
template
CMatrixcpy(CMatrix&v)
{
intmr=v.row();
intmc=v.col();
CMatrixx(mr,mc);
for(inti(0);i*(x.p->f[i])=*(v.p->f[i]);
returnx;
}
/*-----------------------------------定义析构函数-------------------------—*/
template
CMatrix:
:
~CMatrix()
{
if(--p->refcnt==0)
{
if(p->f!
=NULL)
{
intlen=p->length;
for(inti(0);ideletep->f[i];
if(p->tmppointer!
=NULL)
deletep->tmppointer;
deletep->f;
}
}
}
/*------------------------------定义函数返回行数-----------------------*/
template
intCMatrix:
:
row()const
{
returnp->length;
}
/*----------------------------定义函数返回列数----------------------*/
template
intCMatrix:
:
col()const
{
returnp->f[0]->dim();
}
/*----------------------------定义转置的函数-----------------------*/
template
voidInver(CMatrix&mat)
{
intm=mat.row();
intn=mat.col();
CMatrixtmp(n,m);
inti,j;
for(i=0;ifor(j=0;j}
mat=tmp;
}
/*---------------------------定义重载函数重载赋值操作符号=--------------------*/
template
CMatrix&CMatrix:
:
operator=(constCMatrix&vec)
{
vec.p->refcnt++;
if(--p->refcnt==0)
{
intlen=p->length;
for(inti(0);ideletep->f[i];
deletep->f;
if(p->tmppointer!
=NULL)
deletep->tmppointer;
deletep;
}
p=vec.p;
return*this;
}
/*-------------------------定义重载函数重载[]---------------------------*/
template
Vector&CMatrix:
:
operator[](inti)
{
if((i>=0)&&(ilength))
return*p->f[i];
else{
cout<<"error"<return*p->f[0];
}
}
/*--------------------------------定义重载函数重载**------------------*/
template
CMatrix:
:
operatorT**()
{
if(p->tmppointer==NULL){
intn=row();
p->tmppointer=newT*[n];
for(inti(0);ip->tmppointer[i]=p->f[i]->begin();
}
returnp->tmppointer;
}
template
voidCMatrix:
:
ReadFromFile()//******************************从文件中读入矩阵
{
charfilename[256];
cin>>filename;
ifstreaminfile;
//cout<<"****";
introw,col;
infile.open(filename,ios:
:
in);
if(!
infile)
{
cout<<"不能打开输入文件!
"<exit
(1);
};
infile>>row>>col;
CMatrixv1(row,col,0);
//infile>>v1[0][0];
//cout<for(inti(0);ifor(intj(0);j
infile>>v1[i][j];
*this=v1;
}
/*-----------------------定义函数重载输出《------------------------------*/
template
std:
:
ostream&operator<<(std:
:
ostream&os,CMatrix&v1)
{
//os<<"{"<Vector**f=v1.begin();
//cout<intlen=v1.row();
for(inti(0);ios<<*f[i]<<"\n";
returnos;
}
/*-------------------------定义函数重载输入---------------------------*/
template
std:
:
istream&operator>>(std:
:
istream&is,CMatrix&v1)
{
introw,col;
cout<<"请您分别输入矩阵的行数和列数:
\n";
is>>row>>col;
CMatrixx(row,col,0);
cout<<"请输入"<for(inti(0);ifor(intj(0);j
is>>x[i][j];
v1=x;
returnis;
}
/*--------------------------定义重载函数重载乘法*---------------------------*/
template
CMatrixoperator*(CMatrix&m1,CMatrix&m2)
{
inti,j;
intm1rows=m1.row();
intm1cols=m1.col();
intm2rows=m2.row();
intm2cols=m2.col();
if(m1cols!
=m2rows)
cout<<"error!
"<CMatrixv(m1rows,m2cols);
CMatrixflip(m2cols,m2rows);
for(i=0;ifor(j=0;jflip[j][i]=m2[i][j];
for(i=0;ifor(j=0;jv[i][j]=dot_prod(m1[i],flip[j]);
returnv;
}
/*----------------------------------定义函数重载数乘(整型,双精度型)-------------------------------*/
CMatrixoperator*(constCMatrix&v,intval)
{
CMatrixtemp;
temp=v;
for(inti(0);ilength;i++)
*(temp.p->f[i])=*(v.p->f[i])*val;
returntemp;
}
CMatrixoperator*(constCMatrix&v,doubleval)
{
CMatrixtemp;
temp=v;
for(inti(0);ilength;i++)
*(temp.p->f[i])=*(v.p->f[i])*val;
returntemp;
}
#endif
/*---------------------------------------------------定义几个选择函数----------------------------------------*/
voidchoiceid();//********************************************选择输入矩阵的类型
voidprocessint();//*****************************************选择输入矩阵的饿方式
voidprocessdouble();//***************************************选择输入矩阵的方式
template
voidprocess(CMatrix&cm,CMatrix&cm1,CMatrix&cm2);
voidmain()
{
cout<<"!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
欢迎您进入并使用矩阵转置和乘法程序!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
\n";
cout<<"\t(请您注意本程序对您输入的矩阵的项数不等于\n\t\t您事先设定的矩阵项数时无法识别,请您见量!
)\n\n";
choiceid();
}
/*---------------------------------------------------------定义选择函数-----------------------------------------*/
voidchoiceid()
{
cout<<"^----^请您输入矩阵的类型:
\n输入整型请按1\n输入浮点型请按2\n";
intchoice;
cin>>choice;
switch(choice)
{
case1:
processint();
break;
case2:
processdouble();
break;
default:
break;
}
}
voidprocessint()
{
CMatrixicm(2,2,0),icm1,icm2;
cout<<"^----^请您选择输入方式:
\n从键盘输入请按1\n从文件输入请按2\n";
intchoice;
while(cin>>choice)
{
switch(choice)
{
case1:
cout<<"请您输入第1个矩阵:
\n";
cin>>icm1;
cout<<"请您输入第2个矩阵:
\n";
cin>>icm2;
process(icm,icm1,icm2);
break;
case2:
cout<<"输入矩阵1的路径:
";
icm1.ReadFromFile();
cout<<"输入矩阵2的路径:
";
icm2.ReadFromFile();
process(icm,icm1,icm2);
break;
default:
break;
}
}
}
voidprocessdouble()
{
CMatrixicm,icm1,icm2;
cout<<"^---------^请您请选择输入方式:
\n1.从键盘输入矩阵\n2.从文件输入矩阵\n";
intchoice;
while(cin>>choice)
{
switch(choice)
{
case1:
cout<<"请您输入第1个矩阵:
\n";
cin>>icm1;
cout<<"请您输入第2个矩阵:
\n";
cin>>icm2;
process(icm,icm1,icm2);
break;
case2:
cout<<"输入矩阵1的路径:
";
icm1.ReadFromFile();
cout<<"输入矩阵2的路径:
";
icm2.ReadFromFile();
process(icm,icm1,icm2);
break;
default:
break;
}
}
}
template
voidprocess(CMatrix&cm,CMatrix&cm1,CMatrix&cm2)
{
intchoice;
doubleval;
cout<<"请您选择对矩阵的操作类型:
\n1.两矩阵相乘\n2.矩阵数乘\n3.矩阵转置\n其他键退出\n";
while(cin>>choice)
{
switch(choice)
{
case1:
cm=cm1*cm2;
cout<<"两矩阵相乘的结果为:
\n"<cout<<"1.退出\n2.继续\n";
cin>>choice;
if(choice==1){cout<<"谢谢您的使用!
再见!
\n";}exit(0);
if(choice==2)
{
cout<<"请您选择对矩阵的操作类型:
\n1.两矩阵相乘\n2.矩阵数乘\n3.矩阵转置\n其他键退出\n";
continue;
};break;
case2:
cout<<"请您输入要乘的数:
\n";
cin>>val;
cout<<"请您输入需要数乘的矩阵:
\n1.矩阵1\n2.矩阵2\n";
cin>>choice;
switch(choice)
{
case1:
cm=cm1*val;
cout<<"矩阵1:
\n"<<"\n乘以数"<\n"<cout<<"1.退出\n2.继续\n";
cin>>choice;
if(choice==
|
|
|