如大家有兴趣运行看看.结果很好。 #include<stdio.h> #include<conio.h> #include<math.h> #define N 100 #define epsilon 1e-6 float a[N][N+1]; int z[N];
void main( ) { int i,j,k,l,n,m,p,q; float t,s=0; clrscr( ); printf("It is strongly recommended that you read this before you run the programming:\n"); printf("1.This is all main element elimination method to solve linear equations.\n"); printf("2.Please review the principles of all main element elimination method.\n"); printf("3.Be careful,don't just depend on computers,you have mind!\n"); printf("4.Probably there are some defects in this programme.\n"); printf(" If any,please send messages to the creator.\n"); printf("5.Copyright 2005-5-18 Zhang Zujin\n"); printf(" QQ:283421560 E-mail:zhangzujin360732@163.com\n\n"); printf("Input juzhen jieshu:"); scanf("%d",&n); printf("\n"); printf("Input Extended Matrix:\n"); for(i=0;i<n;i++) for(j=0;j<n+1;j++) scanf("%f",&a[i][j]); for(i=0;i<n;i++) z[i]=i;
for(k=0;k<n-1;k++) { p=q=k;
for(i=k+1;i<n;i++) for(j=k+1;j<n;j++) if( abs(a[i][j]) > abs(a[k][k]) ) { p=i; q=j; } m=z[k]; z[k]=z[q]; z[q]=m;
if(p!=k) for(j=0;j<n+1;j++) { t=a[k][j]; a[k][j]=a[p][j]; a[p][j]=t; }
if(q!=k) for(i=0;i<n;i++) { t=a[i][k]; a[i][k]=a[i][q]; a[i][q]=t; }
if( abs(a[k][k]) < epsilon) { printf("\nError,quanzhuyuan xiaoqufa cann't be durable,break at %d!\n",k+1); return; } for(i=k+1;i<n;i++) { a[i][k]=a[i][k] / a[k][k]; for(j=k+1;j<n+1;j++) a[i][j]=a[i][j]-a[i][k] * a[k][j]; } } if( abs(a[n-1][n-1])<epsilon) { printf("Error,divide by 0! At last %d.\n",n); return; } for(k=n-1;k>=0;k--) { s=0; for(j=k+1;j<n;j++) s+=a[k][j]*a[j][n]; a[k][n]=( a[k][n]-s ) / a[k][k]; } printf("\n*****The Result*****\n"); for(i=0;i<n-1;i++) for(j=0;j<n;j++) if(z[i]>z[j]) { m=z[i]; z[i]=z[j]; z[j]=m;
t=a[i][n]; a[i][n]=a[j][n]; a[j][n]=t; }
for(i=0;i<n;i++) printf(" x[%d]=%.4f\n",z[i]+1,a[i][n]); getch( ); }