#include<stdio.h> #define N 3 int a[N][N],b[N],choice; float x[N];
int qd() { int i,flag=1; for(i=0;i<N;i++) if(a[i][i]==0) { flag=0; break; } return flag; }
void main( ) { int i,j; float sum; printf("make a choice:ub(1) or lb(2):"); scanf("%d",&choice);
if(choice==1) { printf("input A:\n"); for(i=0;i<N;i++) for(j=i;j<N;j++) scanf("%d",&a[i][j]);
printf("input b:\n"); for(i=0;i<N;i++) scanf("%d",&b[i]);
if(qd( )) { x[N-1]=b[N-1]/a[N-1][N-1]; for(i=N-2;i>=0;i--) { sum=0; for(j=i+1;j<N;j++) sum+=a[i][j]*x[j]; x[i]=(b[i]-sum)/a[i][i]; } } else { printf("Error,the main stream must be positive or negative.\n"); return; } } if(choice==2) { printf("input A:\n"); for(i=0;i<N;i++) for(j=0;j<=i;j++) scanf("%d",&a[i][j]);
printf("input b:\n"); for(i=0;i<N;i++) scanf("%d",&b[i]);
if(qd( )) { x[0]=b[0]/a[0][0]; for(i=1;i<N;i++) { sum=0; for(j=0;j<i;j++) sum+=a[i][j]*x[j]; x[i]=(b[i]-sum)/a[i][i]; } } else { printf("Error,the main stream must be positive or negative.\n"); return; } }
printf("throuh calculating,the result is:\n"); for(i=0;i<N;i++) printf("x[%d]=%.2f\n",i+1,x[i]); }
----------------解决方案--------------------------------------------------------