- /*******************************************
- 根据用户输入的数据申请一维动态数组,并进行相
- 关的运算,矩阵求和、矩阵相乘、矩阵的逆矩阵、
- 矩阵的伴随矩阵、矩阵行列式的值(建议采用visual c++6.0编译).
- *********************************************/
- #include<iostream>
- #include<cmath>
- using namespace std;
- #include"function.h"
- int main()
- {
- int n,m,n1,m1,count(1);
- int *p,*p1,i;
- system("color 1e");
- cout<<"请输入第1个矩阵的行数与列数:"<<endl;
- cin>>m>>n;//主函数输入矩阵1
- p=new int [m*n];
- for(i=0;i<m*n;i++)
- {
- if(i%n==0)
- cout<<"请输入第"<<i/n+count<<"行的"<<n<<"个元素"<<endl;
- cin>>p[i];
- }
- cout<<"请输入第2个矩阵的行数与列数:"<<endl;
- cin>>m1>>n1;//主函数输入矩阵2
- p1=new int [m1*n1];
- for(i=0;i<m1*n1;i++)
- {
- if(i%n1==0)
- cout<<"请输入第"<<i/n1+count<<"行的"<<n1<<"个元素"<<endl;
- cin>>p1[i];
- }
- cout<<"============"<<endl;
- cout<<"你输入的矩阵1是:"<<endl;
- print(p,m,n);
- cout<<"你输入的矩阵2是:"<<endl; //输出矩阵1
- print(p1,m1,n1); //输出矩阵2
- if(m==n||m1==n1)
- cout<<"============\\n\\n";
- if(m==n)
- zhuan_zhi(p,m); //矩阵1的转置矩阵
- if(m1==n1)
- zhuan_zhi(p1,m1); //矩阵2的转置矩阵
- if(m==n||m1==n1)
- cout<<"============\\n\\n";
- if(m==m1&&n==n1)
- {
- count_sum(p,p1,m,n);
- cout<<"===============\\n";
- }
- if(n==m1)
- {
- cheng_matrix(p,p1,m,n,n1);
- cout<<"====================\\n";
- }
- if(m==n)
- {
- cout<<"矩阵1的行列式的值为:"<<determinant(p,m)<<endl;
- cout<<"=======================\\n";
- }
- if(m1==n1)
- {
- cout<<"矩阵2的行列式的值为:"<<determinant(p1,m1)<<endl;
- cout<<"=======================\\n";
- }
- if(m==n)
- {
- cout<<"矩阵1的伴随矩阵为:\\n";
- companion_matrix(p,m);
- cout<<"====================\\n";
- }
- if(m1==n1)
- {
- cout<<"矩阵2的伴随矩阵为:\\n";
- cout.unsetf(ios::fixed);
- cout.precision(6);
- companion_matrix(p1,m1);
- cout<<"====================\\n";
- }
- system("pause");
- return 0;
- }
- /***********************************
- function.h头文件主要实现实现主函数当
- 中的功能模块,有输出函数、矩阵求和、求
- 矩阵的逆矩阵、矩阵的伴随矩阵、矩阵的
- 行列式的值.
- ************************************/
- /******输出用户输入的矩阵******/
- void print(int p[],int m,int n)
- {
- for(int i=0;i<m;i++)
- {
- for(int j=0;j<n;j++)
- cout<<'|'<<p[i*n+j];
- cout<<'|'<<endl;
- }
- }
- /******基于一维数组的转置矩阵******/
- void zhuan_zhi(int p[],int m)
- {
- static int j1=1;
- int *p1;
- p1=new int[m*m];
- for(int i=0;i<m;i++)
- for(int j=0;j<m;j++)
- {
- p1[j*m+i]=p[i*m+j];
- }
- if(j1<3)
- cout<<"矩阵"<<j1<<"的转置矩阵为:"<<endl;
- print(p1,m,m);
- j1++;
- }
- /******求矩阵的乘法******/
- void cheng_matrix(int p[],int p1[],int m,int n,int n1)
- {
- int *p2=new int[m*n1];
- for(int i=0;i<m;i++)
- {
- for(int j=0;j<n1;j++)
- {
- p2[i*n1+j]=0;
- for(int k=0;k<n;k++)
- p2[i*n1+j]+=p[i*n+k]*p1[k*n1+j];
- }
- }
- cout<<"这两个矩阵的乘积为:"<<endl;
- print(p2,m,n1);
- }
- /******计算两个矩阵的和******/
- void count_sum(int p[],int p1[],int m,int n)
- {
- int *p2;
- p2=new int[n*m];
- for(int i=0;i<n*m;i++)
- p2[i]=p[i]+p1[i];
- cout<<"这两个矩阵的和为:"<<endl;
- print(p2,m,n);
- }
- /******确定行列式中每个元素的符号******/
- int symble(int n)
- {
- if(n%2==0)
- return 1;
- return -1;
- }
- /******计算矩阵的行列式的值******/
- int determinant(const int p[],int m) //将参数p定义为只读参数是防止通过间接寻址方式对它进行改变
- {
- int *temp,*temp1,*temp2,*b,count(0);
- if(m==1)
- return p[0];
- if(m==2)
- return (p[0]*p[3]-p[1]*p[2]);
- if(m==3)
- return (p[0]*p[4]*p[8]+p[1]*p[5]*p[6]+p[2]*p[3]*p[7]-p[2]*p[4]*p[6]-p[1]*p[3]*p[8]-p[0]*p[7]*p[5]);
- if(m>3)
- {
- temp=new int[m*m];
- temp1=new int[(m-1)*(m-1)];
- temp2=new int[m*m];
- b=new int[m];
- for(int g=0;g<m;g++)
- b[g]=p[g*m]*symble(g); //将每行元素和符号的乘积记录在数组b中
- for(int i=0;i<m;i++)
- for(int t=0;t<m;t++) //拷贝一个原数组
- temp2[i*m+t]=p[i*m+t];
- for(int i1=0;i1<m;i1++)
- for(int t1=0;t1<m-1;t1++)
- temp2[i1*m+t1]=temp2[i1*m+t1+1]; //将第一列覆盖掉
- for(int y=0;y<m*m;y++)
- temp[y]=temp2[y]; //将副本中的元素存于一个临时数组中
- for(int j=0;j<m;j++)
- {
- for(int l=j;l<m-1;l++)
- for(int k=0;k<m;k++)
- temp2[l*m+k]=temp2[(l+1)*m+k]; //将第l行覆盖掉
- /******这步很重要******/
- for(int r=0;r<m-1;r++) //这步很重要,一维与二维的区别就在此处,主要的作用是将经过移位后的元素,集中
- for(int q=0;q<m-1;q++) //连续放在一维数组中,如果采用二维数组此处可省,当然一维数组在动态方面更具优势
- temp1[r*(m-1)+q]=temp2[r*m+q];
- /******important******/
- count+=determinant(temp1,m-1)*b[j]; //此处输出会遇到困难,不像二维数组可以格式输出,这是一维数组的缺点
- for(y=0;y<m*m;y++)
- temp2[y]=temp[y]; //此处将副本中的元素初始化
- }
- return count;
- }
- }
- /******计算矩阵的伴随矩阵及其逆矩阵******/
- void companion_matrix(const int p[],int m) //将参数p定义为只读参数是防止通过间接寻址方式对它进行改变
- {
- int *temp,*temp1,*temp2;
- double *t;
- static int w=1;
- t=new double[m*m];
- temp=new int[m*m];
- temp1=new int [m*m];
- temp2=new int[m*m];
- for(int q=0;q<m;q++) //创建一个最开始的副本,后面用来初始化
- for(int q1=0;q1<m;q1++)
- temp1[q*m+q1]=p[q*m+q1];
- for(q=0;q<m;q++)
- for(int q1=0;q1<m;q1++) //拷贝一个原数组
- temp[q*m+q1]=p[q*m+q1];
- for(int i=0;i<m;i++)
- {
- for(int j=i;j<m;j++) //将原数组元素第i列覆盖
- for(int k=0;k<m-1;k++)
- temp[j*m+k]=temp[j*m+k+1];
- for(j=0;j<m;j++) //创建一个副本后面要用
- for(int k=0;k<m;k++)
- temp2[j*m+k]=temp[j*m+k];
- for(int t1=0;t1<m;t1++)
- {
- for(int r=t1;r<m-1;r++)
- for(int r1=0;r1<m;r1++)
- temp[r*m+r1]=temp[(r+1)*m+r1]; // 将原数组的i行覆盖
- t[t1*m+i]=determinant(temp,m-1)*symble(i+r); //根据伴随矩阵定义此处一定要转置
- //t数组中存的是伴随矩阵
- for(j=0;j<m;j++)
- for(int k=0;k<m;k++)
- temp[j*m+k]=temp2[j*m+k]; //做行变换之前初始化
- }
- for(j=0;j<m;j++)
- for(int k=0;k<m;k++)
- temp[j*m+k]=temp1[j*m+k]; //做列变换之前初始化
- }
- for(q=0;q<m;q++)
- { //输出伴随矩阵
- for(int q1=0;q1<m;q1++)
- cout<<'|'<<t[q*m+q1];
- cout<<'|'<<endl;
- }
- for(q=0;q<m;q++)
- for(int q1=0;q1<m;q1++)
- t[q*m+q1]/=fabs(determinant(p,m));
- cout.precision(2);
- cout.setf(ios::fixed); //保留两位小数控制精度
- if(determinant(p,m)!=0)
- {
- cout<<"矩阵"<<w<<"的逆矩阵为"<<endl;
- for(int q2=0;q2<m;q2++)
- {
- for(int q3=0;q3<m;q3++)
- cout<<'|'<<t[q2*m+q3]; //输出逆矩阵
- cout<<'|'<<endl;
- }
- w++;
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/120320148991.html
来源: http://www.codesnippet.cn/detail/120320148991.html