标签:
#include<stdio.h> #define row 3 #define colomn 3 void swap(double *a,double *b); int main() { int i,j,m,k,ii,jj; double a[row][colomn]={{2,3,1},{4,2,3},{7,1,-1}}; double b[colomn]={4,17,1}; int index; char str[]="matrix.txt"; for(ii=0;ii<row;ii++) { for(jj=0;jj<colomn;jj++) { printf("%5.2f\t",a[ii][jj]); } printf("%5.2f\n",b[ii]); } printf("\n*******************************\n"); for(i=0;i<row;i++) { index=i; while(a[index][i]==0) { index++; } if(index!=i) { for(k=0;k<colomn;k++) { swap(*(a+i)+k,*(a+index)+k); } swap(b+index,b+i); //swap row i and index } for(m=i;m<row;m++) { if(m==i) { b[m]=b[m]/a[i][i]; } else { /////////***********////// b[m]=b[m]-b[i]*a[m][i]/a[i][i]; } for(j=colomn-1;j>=0;j--) { if(m==i) { a[m][j]=a[m][j]/a[i][i]; } else { /////////***************// a[m][j]=a[m][j]-a[i][j]*a[m][i]; } } } //print every step for(ii=0;ii<row;ii++) { for(jj=0;jj<colomn;jj++) { printf("%5.2f\t",a[ii][jj]); } printf("%5.2f\n",b[ii]); } printf("\n*******************************\n"); } index=0; while(a[index][index]!=0&&index<row) index++; for(i=index-1;i>0;i--) { for(j=i-1;j>=0;j--) { //can‘t change turn b[j]=b[j]-a[j][i]*b[i]; a[j][i]=a[j][i]-a[i][i]*a[j][i]; } //print a for(ii=0;ii<row;ii++) { for(jj=0;jj<colomn;jj++) { printf("%5.2f\t",a[ii][jj]); } printf("%5.2f\n",b[ii]); } printf("\n*******************************\n"); } printf("the answer is :\n"); for(i=0;i<colomn;i++) { printf("\t%6.2f\n",b[i]); } return 0; } void swap(double *a,double *b) { double temp; temp=*a; *a=*b; *b=temp; }
标签:
原文地址:http://www.cnblogs.com/Bingo711x/p/4495621.html