欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

实现矩阵的加、减、乘、除和逆运算

最编程 2024-05-22 15:46:24
...
1 #include<stdio.h> 2 #include<stdlib.h> 3 #define col 3 4 #define row 3 5 class matrix//类的定义 6 { 7 private: 8 double m[col][row];//矩阵设置为私有的, 9 public: 10 matrix(){}//无参数的构造函数 11 matrix(double a[col][row]);//有参数的构造函数 12 matrix Add(matrix &b);//加法运算声明 13 matrix Sub(matrix &b);//减法运算声明 14 matrix Mul(matrix &b);//乘法运算声明 15 matrix Div(matrix &b);//除法运算声明 16 matrix Inverse();//求逆运算声明 17 ~matrix();//析构函数声明 18 void display();//显示函数声明 19 }; 20 matrix::matrix(double a[col][row])//构造函数的定义 21 { 22 int i,j; 23 for(i=0;i<col;i++) 24 for(j=0;j<row;j++) 25 m[i][j]=a[i][j]; 26 } 27 matrix matrix::Add(matrix &b)//加法运算 28 { 29 int i,j; 30 matrix*c=(matrix*)malloc(sizeof(matrix)); 31 for(i=0;i<col;i++) 32 for(j=0;j<row;j++) 33 c->m[i][j]=m[i][j]+b.m[i][j]; 34 return(*c); 35 } 36 matrix matrix::Sub(matrix &b)//减法运算 37 { 38 int i,j; 39 matrix*c=(matrix*)malloc(sizeof(matrix)); 40 for(i=0;i<col;i++) 41 for(j=0;j<row;j++) 42 c->m[i][j]=m[i][j]-b.m[i][j]; 43 return *c; 44 } 45 matrix matrix::Mul(matrix &b)//乘法运算 46 { 47 int i,j,k; 48 double sum=0; 49 matrix*c=(matrix*)malloc(sizeof(matrix)); 50 for(i=0;i<col;i++) 51 { 52 for(j=0;j<row;j++) 53 { 54 for(k=0;k<row;k++) 55 sum+=m[i][k]*(b.m[k][j]); 56 c->m[i][j]=sum; 57 sum=0; 58 } 59 } 60 return(*c); 61 } 62 matrix matrix::Div(matrix &b)//除法运算 63 { 64 //除法直接求解,参见主函数 65 matrix c; 66 return(c); 67 } 68 matrix matrix::Inverse()//求逆运算 69 { //参考博客:http://www.cnblogs.com/rollenholt/articles/2050662.html 70 int i,j,k,M=col,N=2*col; 71 double b[col][col*2]; 72 matrix*c=(matrix*)malloc(sizeof(matrix)); 73 for(i=0;i<M;i++) //赋值 74 for(j=0;j<M;j++) 75 b[i][j]=m[i][j]; 76 for(i=0;i<M;i++) //扩展 77 for(j=M;j<N;j++) 78 { 79 if(i==(j-M)) 80 b[i][j]=1; 81 else 82 b[i][j]=0; 83 } 84 /***************下面进行求逆运算*********/ 85 for(i=0;i<M;i++) 86 { 87 if(b[i][i]==0) 88 { 89 for(k=i;k<M;k++) 90 { 91 if(b[k][i]!=0) //作者的博客里面此处为b[k][k],貌似是不正确的, 92 //因为这对比如说是{0,0,1,1,0,1,0,1,1}的矩阵就会判断为不可逆, 93 { //而实际上该矩阵是可逆的,这里应该是作者笔误,待进一步求证 94 for(int j=0;j<N;j++) 95 { 96 double temp; 97 temp=b[i][j]; 98 b[i][j]=b[k][j]; 99 b[k][j]=temp; 100 } 101 break; 102 } 103 } 104 if(k==M) 105 { 106 printf("该矩阵不可逆!\n"); 107 exit(0); 108 } 109 } 110 for(j=N-1;j>=i;j--) 111 b[i][j]/=b[i][i]; 112 113 for(k=0;k<M;k++) 114 { 115 if(k!=i) 116 { 117 double temp=b[k][i]; 118 for(j=0;j<N;j++) 119 b[k][j]-=temp*b[i][j]; 120 } 121 } 122 } 123 /**********************导出结果******************/ 124 for(i=0;i<M;i++) 125 for(j=3;j<N;j++) 126 c->m[i][j-3]=b[i][j]; 127 return (*c); 128 } 129 130 matrix::~matrix() 131 {} 132 void matrix::display() 133 { 134 int i,j; 135 for(i=0;i<col;i++) 136 { 137 for(j=0;j<row;j++) 138 printf("%f ",m[i][j]); 139 printf("\n"); 140 } 141 } 142 void main() 143 { 144 double a[3][3]={{1,0,1},{0,1,1},{0,3,1}}; 145 double b[3][3]={{0,0,1},{1,0,1},{0,1,0}}; 146 matrix ma(a),mb(b),mc; 147 int flag; 148 printf("----------------------------------------------------\n请选择要进行的操作:\n1、打印\t2、加法"); 149 printf("\t3、减法\n4、乘法\t5、除法\t6、求逆\n7、退出\n"); 150 printf("-----------------------------------------------------\n"); 151 scanf("%d",&flag); 152 while((flag==1)||(flag==2)||(flag==3)||(flag==4)||(flag==5)||(flag==6)||(flag==7)) 153 { 154 if(flag==1) 155 { 156 printf("矩阵a为:\n"); 157 ma.display(); 158 printf("矩阵b为:\n"); 159 mb.display(); 160 } 161 if(flag==2)//矩阵加法运算 162 { 163 printf("矩阵加法运算结果:\n"); 164 mc=ma.Add(mb); 165 mc.display(); 166 } 167 else if(flag==3)//矩阵减法运算 168 { 169 printf("矩阵减法运算结果:\n"); 170 mc=ma.Sub(mb); 171 mc.display(); 172 } 173 else if(flag==4)//矩阵乘法运算 174 { 175 printf("矩阵乘法运算结果:\n"); 176 mc=ma.Mul(mb); 177 mc.display(); 178 } 179 else if(flag==5)//矩阵除法运算 180 { 181 printf("矩阵除法运算结果:\n"); 182 printf("矩阵的除法分成两类:\n 1、A\\B=inverse(A)*B \n 2、B/A=B*inverse(A)\n"); 183 printf("采用第1类,则a\\b的结果为:\n"); 184 mc=ma.Inverse(); 185 mc=mc.Mul(mb); 186 mc.display(); 187 printf("采用第2类,则a/b的结果为:\n"); 188 mc=mb.Inverse(); 189 mc=ma.Mul(mc); 190 mc.display(); 191 } 192 else if (flag==6)//矩阵求逆运算 193 { 194 printf("矩阵a求逆运算结果为:\n"); 195 mc=ma.Inverse(); 196 mc.display(); 197 198 printf("矩阵b求逆运算结果为:\n"); 199 mc=mb.Inverse(); 200 mc.display(); 201 } 202 else {exit(0);} 203 printf("----------------------------------------------------\n请选择要进行的操作:\n1、打印\t2、加法"); 204 printf("\t3、减法\n4、乘法\t5、除法\t6、求逆\n7、退出\n"); 205 printf("-----------------------------------------------------\n"); 206 scanf("%d",&flag); 207 } 208 }

推荐阅读