古詩詞大全網 - 成語故事 - 求壹個 計算矩陣行列式 的C或C++程序!

求壹個 計算矩陣行列式 的C或C++程序!

以下是我原來寫過的壹個關於矩陣的類,裏面實現了求方陣的行列式函數:int det(matrix);

//matrix.h

//*******************************

# ifndef matrix_h

# define matrix_h

class matrix

{

private:

int m;

int n;

double arr[8][8];

public:

matrix();

matrix(double);

void set();// 對矩陣的賦值;

void show();//在屏幕上顯示矩陣;

friend int homotype(matrix,matrix);//是否同型;

friend int multipliable(matrix,matrix);

//判斷矩陣是否可相乘

friend matrix operator +(matrix,matrix);

//矩陣相加,對加號重載;

friend matrix operator -(matrix,matrix);

//矩陣機減,對減號重載;

friend matrix add(matrix,matrix);

//矩陣相加;

friend matrix minus(matrix,matrix);

//矩陣相減

friend matrix multiply(matrix,matrix);

//矩陣相乘

friend matrix operator *(matrix,matrix);

//矩陣相乘,對乘號重載;

friend double det(matrix);

//求方陣的行列式;

int issquare()

{

return m==n;

}

//判斷是否為方陣;

matrix left(int x,int y);

//求元素arr[x][y](x,y從0開始)的余子式;

};

# endif

//*******************************

//matrix.cpp

//下面是對類中成員及友元函數的實現

//*******************************

# include <iostream.h>

# include "matrix.h"

matrix::matrix()

{

m = 8;

n = 8;

for(int i=0;i<m;i++)

{

for(int j=0;j<n;j++)

arr[i][j] = 0;

}

}

matrix::matrix(double x)

{

m = 1;

n = 1;

arr[0][0] = x;

}

void matrix::set()

{

cout<<"Set the type of the matrix(?*?)"<<endl;

cin>>m>>n;

cout<<"Now input the elements of the matrix:"<<endl;

for(int i=0;i<m;i++)

{

for(int j=0;j<n;j++)

cin>>arr[i][j];

}

}

void matrix::show()

{

cout<<"This is the matrix:"<<endl;

for(int i=0;i<m;i++)

{

for(int j=0;j<n;j++)

{

cout<<arr[i][j]<<ends;

}

cout<<endl;

}

}

int homotype(matrix x,matrix y)

{

if(x.m==y.m&&x.n==y.n)

return 1;

else

return 0;

}

int multipliable(matrix x,matrix y)

{

if(x.n==y.m)

return 1;

else

return 0;

}

matrix add(matrix x,matrix y)

{

matrix z;

if(homotype(x,y))

{

z.m = x.m;

z.n = x.n;

for(int i=0;i<z.m;i++)

for(int j=0;j<z.n;j++)

z.arr[i][j] = x.arr[i][j] + y.arr[i][j];

return z;

}

else

{

cout<<"cannot be added!"<<endl;

return z;

}

}

matrix operator +(matrix x,matrix y)

{

return add(x,y);

}

matrix minus(matrix x,matrix y)

{

matrix z;

if(homotype(x,y))

{

z.m = x.m;

z.n = x.n;

for(int i=0;i<z.m;i++)

for(int j=0;j<z.n;j++)

z.arr[i][j] = x.arr[i][j] - y.arr[i][j];

return z;

}

else

{

cout<<"cannot be added!"<<endl;

return z;

}

}

matrix operator -(matrix x,matrix y)

{

return minus(x,y);

}

matrix multiply(matrix x,matrix y)

{

matrix z;

if(x.n!=y.m)

{

cout<<"The two matrixes cannot be multiplied."<<endl;

return z;

}

for(int i=0;i<x.m;i++)

{

for(int j=0;j<y.n;j++)

{

for(int k=0;k<x.n;k++)

z.arr[i][j] += x.arr[i][k]*y.arr[k][j];

}

}

z.m = x.m;

z.n = y.n;

return z;

}

matrix operator *(matrix x,matrix y)

{

matrix z;

z = multiply(x,y);

return z;

}

matrix matrix::left(int x,int y)

{

matrix leftmatrix;

if((x>=m)||(y>=n))

{

cout<<"errer"<<endl;

return leftmatrix;

}

/* if(!issquare())

{

cout<<"不是方陣!"<<endl;

return *this;

}//*/

leftmatrix.m = m - 1;

leftmatrix.n = n - 1;

int testx = 0;

int testy = 0;

for(int i=0;i<leftmatrix.m;i++)

{

testy = 0;

for(int j=0;j<leftmatrix.n;j++)

{

if(i==x)

testx = 1;

if(j==y)

testy = 1;

if((!testx)&&(!testy))

leftmatrix.arr[i][j] = this->arr[i][j];

else if(testx&&(!testy))

leftmatrix.arr[i][j] = this->arr[i+1][j];

else if((!testx)&&testy)

leftmatrix.arr[i][j] = this->arr[i][j+1];

else

leftmatrix.arr[i][j] = this->arr[i+1][j+1];

}

}

return leftmatrix;

}

double det(matrix x)//遞歸算法

{

if(!x.issquare())

{

cout<<"不是方陣!"<<endl;

return 9.99;

}

if(x.m==0)

return 0;

else if(x.m==1)

return x.arr[0][0];

else if(x.m==2)

return (x.arr[0][0]*x.arr[1][1] - x.arr[0][1]*x.arr[1][0]);

else

{

double num = 0;

int a = 1;

for(int i=0;i<x.m;i++)

{

num = num + a*x.arr[0][i]*det(x.left(0,i));

//按第0行展開

a = -a;

}

return num;

}

}

//**************************************