矩阵求逆的c语言程序

//源程序如下# includesdio.h

#includeconio.h

#includestring.h

#includeiostream.h

#includestdlib.h

#包含math.h

# define max 100 void input style(int *);//输入功能

void input(int **,int);//输入功能

long danx(int **,int);

int sgnx(int);

void martx(int **,int);int main(void)

{

int style=0,I = 0;

Int矩阵用C语言编程求解逆矩阵。 # include stdio . h # include stdlib . h # include malloc . h oid matrix opp(double * A,int m,int n,double * inv mat);void MatrixInver(double *A,int m,int n,double * invmat);双盈(双A[],int m,int n);int matrix_inv(double* p,int num,double * inv mat);void MatrixOpp(double A[],int m,int n,double * inv mat){ int I,j,x,y,k;double *SP = NULL,*AB = NULL,*B = NULL,X;SP =(double *)malloc(m * n * sizeof(double));AB =(double *)malloc(m * n * sizeof(double));b =(double *)malloc(m * n * sizeof(double));X =剩余(A,m,n);X = 1/X;for(I = 0;我是m;I)for(j = 0;j n;j){ for(k = 0;k m * n;k)B[k]= A[k];{ for(x = 0;x n;x)B[I * n x]= 0;for(y = 0;y m;y)B[m * y j]= 0;b[I * n j]= 1;SP[i * n j] =盈余(B,m,n);AB[I * n j]= X * SP[I * n j];} } matrix inver(AB,m,n,inv mat);免费(SP);免费(AB);免费(B);}void MatrixInver(double A[],int m,int n,double * invmat){ int I,j;double * B = invmatfor(I = 0;I n;I)for(j = 0;j m;j)B[I * m j]= A[j * n I];}double盈余(double A[],int m,int n){ int I,j,k,p,r;double X,temp = 1,temp1 = 1,s = 0,S1 = 0;if(n = = 2){ for(I = 0;我是m;I)for(j = 0;j n;j)if((I j)% 2)temp 1 * = A[I * n j];else temp * = A[I * n j];X =温度-温度1;} else { for(k = 0;k n;k){ for(I = 0,j = k;i m,j n;I,j)temp * = A[I * n j];if(m-I){ for(p = m-I,r = m-1;p 0;p -,r-)temp * = A[r * n p-1];} s = temptemp = 1;} for(k = n-1;k = 0;k-){ for(I = 0,j = k;i m,j = 0;I,j-)temp 1 * = A[I * n j];if(m-I){ for(p = m-1,r = I;r m;p -,r)temp 1 * = A[r * n p];} s1 = temp1temp1 = 1} X = s-S1;}返回X;}int matrix_inv(double* p,int num,double * invmat){ if(p = = NULL | | invmat = = NULL){ return 1;} if(num 10){ return 2;} matrix opp(p,num,num,inv mat);返回0;} int main(){ int I,j;int numdouble * arr = NULLdouble * result = NULLint标志;Printf("请输入矩阵维度:\ n ");scanf("%d ",数字);arr =(double *)malloc(sizeof(double)* num * num);result =(double *)malloc(sizeof(double)* num * num);Printf("请输入%d*%d矩阵:\n ",num,num);for(I = 0;i numI){ for(j = 0;j numj){ scanf(" % lf ",arr[I * num j]);} } flag = matrix _ inv(arr,num,result);If (flag = = 0) {printf("逆矩阵为:\ n ");for(I = 0;i num * numI){ printf(" % lf \ t " ,*(结果I));if(I % num = =(num-1))printf(" \ n ");}} else if (flag = = 1) {printf ("p/q为空\ n ");} else { printf("超过最大尺寸\ n ");}系统(“暂停”);免费(arr);免费(结果);返回0;}

相关文章

发表新评论