把这个程序改成行优先顺序存储就行。。。。
#include<iostream>
using namespace std;
typedef double Datatype;
class Matrix{
public:
Matrix();//无参构造函数
friend void In(Matrix &);//设置为友元的输入函数
friend void Out(Matrix &);//设置为友元的输出函数
Matrix operator +(Matrix &);//加法重载函数
Matrix operator -(Matrix &);//减法重载函数
Matrix operator *(Matrix &);//乘法重载函数
friend Matrix change(Matrix &);//转置函数
friend Matrix Adjunct(Matrix &, int , int );//设置成友元的求代数余子式
friend double Det(Matrix &); //设置成友元的递归求行列式
friend Matrix Inv(Matrix &); //设置成友元的求矩阵的逆
private:
int m,n;//矩阵的行数和列数
Datatype *p;//矩阵的基址
};
Matrix::Matrix(){
m=0;
n=0;
}
void In(Matrix &a){ //输入函数
cout<<"请输入行、列数";
cin>>a.m>>a.n;
int i,j;
a.p=new Datatype[a.m*a.n];
Datatype *q;
cout<<"请按列优先输入矩阵"<<endl;
for(j=0;j<a.n;j++)
for(q=a.p+j,i=0;i<a.m;i++,q+=a.n)
cin>>*q;
}
void Out(Matrix &b){ //输出函数
int i,j;
Datatype *p=b.p;
double *q;
for(i=0;i<b.m;i++)
for(q=p+i*b.n,j=0;j<b.n;j++,q++){
cout<<*q<<'\t';
if(j==b.n-1)
cout<<endl;
}
}
Matrix Matrix::operator+(Matrix &b){ //加法重载函数
if(m!=b.m||n!=b.n)
{cout<<"\n行或列不匹配";
exit(0);
}
Matrix c;
c.m=m;
c.n=n;
c.p=new double[m*n];
int i,j;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
c.p[i*c.n+j]=p[i*c.n+j]+b.p[i*c.n+j];
Out(c);
return c;
}
Matrix Matrix::operator -(Matrix &b){ //减法重载函数
if(m!=b.m||n!=b.n)
{cout<<"\n行或列不匹配";
exit(0);
}
Matrix c;
c.m=m;
c.n=n;
c.p=new double[m*n];
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
c.p[i*c.n+j]=p[i*c.n+j]-b.p[i*c.n+j];
Out(c);
return c;
}
Matrix Matrix::operator *(Matrix &b){ //乘法重载函数
Matrix c;
c.m=m;
c.n=n;
c.p=new double[m*n];
if(m!=b.n)
{
cout<<"\n行列不匹配";
exit(0);
}
int i,j,k;
for(i=0;i<m;i++)
for(j=0;j<b.n;j++)
for(c.p[i*b.n+j]=0,k=0;k<b.n;k++)
c.p[i*b.n+j]+=p[i*b.n+k]*b.p[k*b.n+j];
Out(c);
return c;
}
Matrix change(Matrix &a){//转置函数
Matrix b;
b.m=a.n;
b.n=a.m;
b.p=new double[a.m*a.n];
for(int i=0;i<a.m;i++)
for(int j=0;j<a.n;j++)
b.p[j*a.m+i]= a.p[i*a.n+j];
cout<<endl;
return b;
}
Matrix Adjunct(Matrix &a, int indexm, int indexn){ //求第indexm行indexn列元素的代数余子式
Matrix adj ;
adj.m=a.m - 1;
adj.n=a.n - 1;
adj.p = new double[(a.n - 1) * (a.n - 1)];
for (int i = 0; i < indexm; i++)
{
for (int j = 0; j < indexn; j++)
{
adj.p[i * (a.n - 1) + j] = a.p[i * a.n + j];
}
for (int k = indexn + 1; k < a.n; k++)
{
adj.p[i *(a.n - 1) + k -1] = a.p[i * a.n + k];
}
}
for (int m = indexm + 1; m < a.n; m++)
{
for (int j = 0; j < a.n - 1; j++)
{
adj.p[(m - 1) * (a.n - 1) + j] = a.p[m * a.n + j];
}
for (int k = indexn + 1; k <a.n; k++)
{
adj.p[(m - 1) * (a.n - 1) + k - 1] = a.p[m * a.n + k];
}
}
return adj;
}
double Det(Matrix &a) //递归求行列式
{
double det = 0;
if (a.m != a.n)
{
cout<<"不是方阵,没有行列式!"<<endl;
cout<<"求行列式退出"<<endl;
}
if (a.n == 1)
{
det = a.p[0];
return det;
}
else
{
for (int i = 0; i <a.n; i++)
{
if (i % 2 == 0)
det += a.p[i * a.n] * Det(Adjunct(a, i, 0));
else
det -= a.p[i * a.n] * Det(Adjunct(a, i, 0));
}
}
return det;
}
Matrix Inv(Matrix &a) { //求矩阵的逆
Matrix temp ;
temp.m=a.n;
temp.n=a.m;
temp.p = new double[a.m * a.n];
//矩阵的逆 = 伴随矩阵 / 行列式
double det = Det(a);
if (det == 0) //如果行列式的值为0,则没有逆
{
cout<<"此矩阵没有逆!"<<endl;
cout<<"求矩阵逆退出!";
exit(0);
}
for (int i = 0; i < temp.m; i++)
{
for (int j = 0; j < temp.n; j++)
{
if ((i +j) % 2 == 0)
temp.p[i * temp.m + j] = Det(Adjunct(a, i, j)) / det;
else
temp.p[i * temp.m + j] = -Det(Adjunct(a, i, j)) / det;
}
}
return temp;
}
void main(){
Matrix a,b,c;
cout<<"对于a矩阵";In(a);cout<<"a="<<endl;Out(a);
cout<<"对于b矩阵";In(b);cout<<"b="<<endl;Out(b);
cout<<"a+b="<<endl;c=a+b;cout<<endl;
cout<<"a-b="<<endl;c=a-b;cout<<endl;
cout<<"a*b="<<endl;c=a*b;cout<<endl;
cout<<"a矩阵转置后如下:"<<endl;a=change(a);Out(a);cout<<endl;
cout<<"b矩阵转置后如下"<<endl;b=change(b);Out(b);cout<<endl;
cout<<"行列式为:"<<endl;cout<<Det(a)<<endl;
cout<<"a矩阵的逆矩阵为:"<<endl;c=Inv(a);Out(c);cout<<endl;
}