n维矩阵乘法:A B-1

数据结构课程设计 这个比较麻烦
功能:
设计一个矩阵相乘的程序,首先从键盘输入两个矩阵a,b的内容,并输出两个矩阵,输出ab-1结果。
分步实施:
1.初步完成总体设计,搭好框架,确定人机对话的界面,确定函数个数;
2.完成最低要求:建立一个文件,可完成2维矩阵的情况;
3.进一步要求:通过键盘输入维数n。有兴趣的同学可以自己扩充系统功能。
要求:
1.界面友好,函数功能要划分好
2.总体设计应画一流程图
3.程序要加必要的注释
4.要提供程序测试方案
5.程序一定要经得起测试,宁可功能少一些,也要能运行起来,不能运行的程序是没有价值的。

声名一点。不要现在网上已经有的答案~流程图也要~~可以追加分数~谢谢~
B-1就是B的逆矩阵的意思,用 C语言~~谢谢

求逆矩阵很费时间,我不知道太大的矩阵会有多慢。你自己试试吧。以下是代码:

#include <stdio.h>
#include <stdlib.h>

struct matrix{
int row;
int column;
double ***m;
};

typedef struct matrix Matrix;

double determinant(Matrix ma){
if(ma.row==1&&ma.column==1) return ***(ma.m);
else{
int i,j,k,l;
double sum=0;
for(i=0;i<ma.column;i++){
Matrix t;
t.row=ma.row-1;
t.column=ma.column-1;
t.m=(double ***)calloc(t.row,sizeof(double **));
for(j=0;j<t.row;j++){
*(t.m+j)=(double **)calloc(t.column,sizeof(double *));
k=0;l=0;
while(k<ma.column){
if(k!=i){
*(*(t.m+j)+l)=*(*(ma.m+j+1)+k);
l++;
}
k++;
}
}
double sign=i%2==0?1.0:-1.0;
sum=sum+(*(*(*ma.m+i)))*sign*determinant(t);

for(j=0;j<t.row;j++)
free(*(t.m+j));
free(t.m);
}
return sum;
}
}

double cofactor(Matrix ma,int r,int c){
Matrix t;
t.row=ma.row-1;
t.column=ma.column-1;
t.m=(double ***)calloc(t.row,sizeof(double **));
int i,j,k,l;
for(j=0;j<t.row;j++)
*(t.m+j)=(double **)calloc(t.column,sizeof(double *));
i=0;j=0;
while(i<ma.row){
if(i!=r){
k=0;l=0;
while(k<ma.column){
if(k!=c){
*(*(t.m+j)+l)=*(*(ma.m+i)+k);
l++;
}
k++;
}
j++;
}
i++;
}
double cof=determinant(t);
for(j=0;j<t.row;j++)
free(*(t.m+j));
free(t.m);
return cof;
}

void transpose(Matrix **ma){
int i,j;
double temp;
for(i=0;i<(*ma)->row;i++)
for(j=0;j<i;j++){
temp=*(*(*((*ma)->m+i)+j));
*(*(*((*ma)->m+i)+j))=*(*(*((*ma)->m+j)+i));
*(*(*((*ma)->m+j)+i))=temp;
}
}

Matrix *inverse(Matrix ma){
if(ma.row!=ma.column){
printf("This matrix cannot be inversed.\n");
return 0;
}
double det=determinant(ma);
if(det==0){
printf("This matrix cannot be inversed.\n");
return 0;
}
Matrix *t=(Matrix *)malloc(sizeof(Matrix));
t->row=ma.row;
t->column=ma.column;
t->m=(double ***)calloc(t->row,sizeof(double **));
int i,j;
for(j=0;j<t->row;j++)
*(t->m+j)=(double **)calloc(t->column,sizeof(double *));
for(i=0;i<t->row;i++)
for(j=0;j<t->column;j++)
*(*(t->m+i)+j)=(double *)malloc(sizeof(double));
double sign;
for(i=0;i<t->row;i++)
for(j=0;j<t->column;j++){
sign=(i+j)%2==0?1.0:-1.0;
*(*(*(t->m+i)+j))=sign*cofactor(ma,i,j)/det;
}
Matrix **s;
s=&t;
transpose(s);
return t;
}

Matrix *multiply(Matrix *m1,Matrix *m2){
if(m1->column!=m2->row){
printf("Multiply is not permitted for these tow matrix.\n");
return 0;
}
else{
Matrix *t=(Matrix *)malloc(sizeof(Matrix));
t->row=m1->row;
t->column=m2->column;
t->m=(double ***)calloc(t->row,sizeof(double **));
int i,j,k;
for(j=0;j<t->row;j++)
*(t->m+j)=(double **)calloc(t->column,sizeof(double *));
for(i=0;i<t->row;i++)
for(j=0;j<t->column;j++)
*(*(t->m+i)+j)=(double *)malloc(sizeof(double));
for(i=0;i<t->row;i++)
for(j=0;j<t->column;j++){
double entry=0;
for(k=0;k<m1->column;k++)
entry=entry+(*(*(*(m1->m+i)+k)))*(*(*(*(m2->m+k)+j)));
*(*(*(t->m+i)+j))=entry;
}
return t;
}
}

void release_memory(Matrix *ma){
int i,j;
for(i=0;i<ma->row;i++)
for(j=0;j<ma->column;j++)
free(*(*(ma->m+i)+j));
for(i=0;i<ma->row;i++)
free(*(ma->m+i));
free(ma->m);
free(ma);
}

int main(){
int i,j;
double entry;
Matrix *m1=(Matrix *)malloc(sizeof(Matrix));
Matrix *m2=(Matrix *)malloc(sizeof(Matrix));
printf("Enter the number of rows in the first matrix: ");
scanf("%d",&i);
m1->row=i;
printf("Enter the number of columns in the first matrix: ");
scanf("%d",&i);
m1->column=i;
m1->m=(double ***)calloc(m1->row,sizeof(double **));
for(j=0;j<m1->row;j++)
*(m1->m+j)=(double **)calloc(m1->column,sizeof(double *));
for(i=0;i<m1->row;i++)
for(j=0;j<m1->column;j++)
*(*(m1->m+i)+j)=(double *)malloc(sizeof(double));
printf("Enter the elements of the first matrix, from top to bottom and from left to right: \n");
for(i=0;i<m1->row;i++)
for(j=0;j<m1->column;j++){
scanf("%lf",&entry);
*(*(*(m1->m+i)+j))=entry;
}
printf("Enter the number of rows in the second matrix: ");
scanf("%d",&i);
m2->row=i;
printf("Enter the number of columns in the second matrix: ");
scanf("%d",&i);
m2->column=i;
m2->m=(double ***)calloc(m2->row,sizeof(double **));
for(j=0;j<m2->row;j++)
*(m2->m+j)=(double **)calloc(m2->column,sizeof(double *));
for(i=0;i<m2->row;i++)
for(j=0;j<m2->column;j++)
*(*(m2->m+i)+j)=(double *)malloc(sizeof(double));
printf("Enter the elements of the second matrix, from top to bottom and from left to right: \n");
for(i=0;i<m2->row;i++)
for(j=0;j<m2->column;j++){
scanf("%lf",&entry);
*(*(*(m2->m+i)+j))=entry;
}
Matrix *m2i=inverse(*m2);
if(m2i==0){
printf("Operation terminated, since no inverse matrix exists.\n");
return 1;
}
Matrix *m3=multiply(m1,m2i);
if(m3==0){
printf("Operation terminated, since the dimensions do not match.\n");
return 2;
}
printf("The result matrix is:\n");
for(i=0;i<m3->row;i++){
for(j=0;j<m3->column;j++)
printf("%5.2f",*(*(*(m3->m+i)+j)));
printf("\n");
}
release_memory(m1);
release_memory(m2);
release_memory(m2i);
release_memory(m3);
return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-06-13
1.初步完成总体设计,搭好框架,确定人机对话的界面,确定函数个数;
2.完成最低要求:建立一个文件,可完成2维矩阵的情况;
3.进一步要求:通过键盘输入维数n。有兴趣的同学可以自己扩充系统功能。
相似回答