}
三、实验成果
四、实验体会
此次上机实践主要是实现快速排序算法,这个实验相对于其他实验,比较简单,做起来问题不是很多,经过几次调试,基本上就能完成。
实验三矩阵类的设计与实现
一、实验目的
通过上机实践,巩固课堂老师所讲关于矩阵设计的知识,通过反复的调试,找错,设计出矩阵。
二、实验过程
#include
#include
#include
#include
classXXXMatrix
{
private:
introws,columns;
double**element;
public:
XXXMatrix(introws=4,intcolumns=4);
XXXMatrix(int*mat,introws,intcolumns);
XXXMatrix(constXXXMatrix&m);
~XXXMatrix();
intrintv(intn,doublea[][4]);
XXXMatrix&inverse(XXXMatrix&mat);
XXXMatrix&transpose(doublea[][4]);
voidinit(introws,intcolumns);
doubleget(inti,intj);
boolset(inti,intj,intvalue);
friendostream&operator<<(ostream&out,XXXMatrix&mat);
XXXMatrix&operator+=(XXXMatrix&mat);
XXXMatrix&operator=(constXXXMatrix&mat);
booloperator==(constXXXMatrix&mat);
booloperator!
=(constXXXMatrix&mat);
XXXMatrix&operator-(XXXMatrix&mat);
XXXMatrix&operator-();
XXXMatrix&operator*(XXXMatrix&mat);
};
voidXXXMatrix:
:
init(introws,intcolumns)
{
if(rows<=0||columns<=0)
throw"矩阵行或列数非正数异常";
this->rows=rows;
this->columns=columns;
element=newdouble*[rows];
for(inti=0;i{
element[i]=newdouble[columns];
for(intj=0;jelement[i][j]=0;
}
}
XXXMatrix:
:
XXXMatrix(introws,intcolumns)
{
init(rows,columns);
}
XXXMatrix:
:
XXXMatrix(int*mat,introws,intcolumns)
{
init(rows,columns);
for(inti=0;ifor(intj=0;jelement[i][j]=*(mat+i*columns+j);
}
XXXMatrix:
:
XXXMatrix(constXXXMatrix&m)
{
init(m.rows,m.columns);
for(inti=0;ifor(intj;jelement[i][j]=m.element[i][j];
}
XXXMatrix:
:
~XXXMatrix()
{
for(inti=0;idelete(element[i]);
delete(element);
}
doubleXXXMatrix:
:
get(inti,intj)
{
if(i>=0&&i=0&&jreturnelement[i][j];
}
boolXXXMatrix:
:
set(inti,intj,intvalue)
{
if(i>=0&&i=0&&j{
element[i][j]=value;
returntrue;
}
returnfalse;
}
ostream&operator<<(ostream&out,XXXMatrix&mat)
{
out<<"矩阵XXXMatrix("<for(inti=0;i{
for(intj=0;jout<out<<"\n";
}
returnout;
}
XXXMatrix&XXXMatrix:
:
operator=(constXXXMatrix&mat)
{
if(this->rows==mat.rows&&this->columns==mat.columns)
{
for(inti=0;ifor(intj=0;jelement[i][j]=mat.element[i][j];
return*this;
}
throw"两个矩阵阶数不同,不能相加";
}
boolXXXMatrix:
:
operator==(constXXXMatrix&mat)
{
if(this->rows==mat.rows&&this->columns==mat.columns)
{
for(inti=0;i{
for(intj=0;jif(this->element[i][j]!
=mat.element[i][j])
{
break;
returnfalse;
}
else
returntrue;
}
}
}
boolXXXMatrix:
:
operator!
=(constXXXMatrix&mat)
{if(this->rows==mat.rows&&this->columns==mat.columns)
{
for(inti=0;i{
for(intj=0;jif(this->element[i][j]==mat.element[i][j])
{
break;
returnfalse;
}
else
returntrue;
}
}
}
XXXMatrix&XXXMatrix:
:
operator-(XX