Hair for several days compiled a solution to a linear equation group of small procedures, can be the first actual combat on the defeat and return. After half a day of debugging, still can not find a way to correct. Because it's not an algorithmic problem, it's because you don't understand the way the compiler handles floating-point functions. Clearly d=0 square matrix is not equal to 0, tracking debugging found that the calculation process procedures to the data to deal with, resulting in the end result is wrong. But if there is no floating-point type, this program should be pretty good.
Copy Code code as follows:
#include <stdio.h>
#include <math.h>
#include <mem.h>
#define NUM 100
void print (void)/* Usage Instructions * *
{CLRSCR ();
printf ("\n\n\n\n\n\t\t\t\t Introduction \ n");
printf ("\t*--------------------------------------------------------------*\n");
printf ("\t* This program is", "compute linear equations. *\n ");
printf ("\t* the" way of use it's very simple. *\n ");
printf ("\t* first:input the number of the equation; (Input 0 to exit) *\n");
printf ("\t* second:input the coefficient of every eqution; *\n ");
printf ("\t* third:input the constant of every eqution; *\n ");
printf ("\t* last:chose the way you want use to solve the equtions;" *\n ");
printf ("\t*," "All, input any key to run it ... *\n");
printf ("\t*-------------------------BY__TJX------------------------------*\n");
Getch (); }
void chose (void)/* Select method of calculation * *
{CLRSCR ();
Fflush (stdin);
printf ("\n\n\n\n\n\t\t**********introduction********** \ n");
printf ("\t\t* chose the way,please." * \ n ");
printf ("\t\t* A:gauss eliminant. * \ n ");
printf ("\t\t* b:gauss_yd eliminant. * \ n ");
printf ("\t\t* c:iterative way. * \ n ");
printf ("\t\t* d:cramer way. * \ n ");
printf ("\t\t* e:exit. * \ n ");
printf ("\t\t*************by__tjx************ \ n");
printf ("\t\tplease Choose number: \ n");}
void input (double **a1,double b1[],int num)/* Data input/*
{int i,j,t;
Double *p;
Char De1,de2;
do{
printf ("Please input array a[%d][%d]: \ n", num,num);
printf ("Warn:the the" of the "array mustn ' t contain zero! \ n ");
for (i=1;i<=num;i++)
{printf ("Please input array a[%d][]: \ n", i);
for (j=1;j<=num;j++)
{t=0;
if (i==1&&j==1)
{do{
if (t==0) {scanf ("%lf", &a1[i][j); t++;}
else {printf ("The input is Invalid,please input again:\n"); scanf ("%f", &a1[i][j);}
}while (a1[i][j]==0);}
else scanf ("%lf", &a1[i][j]);}
printf ("\nplease check the value of array a[%d][%d],press Y to input again.\n", num,num);
do{
De1=getch ();
}while (de1!= ' y ' &&de1!= ' y ' &&de1!= ' n ' &&de1!= ' n ');
}while (de1== ' y ') | | de1== ' Y ');
do{
printf ("Please input array b[%d]: \ n", num);
p=b1+1;
for (i=1;i<=num;i++)
scanf ("%lf", p++);
printf ("\nplease check the value of array b[%d],press Y to input again.\n", num);
do{
De2=getch ();
}while (de2!= ' y ' &&de2!= ' y ' &&de2!= ' n ' &&de2!= ' n ');
}while (de2== ' y ') | | de2== ' Y ');}
int Max (double *t1, double x1[],int n)/* Iteration Sub function * *
{int i,temp=0;
for (i=1;i<=n;i++)
if (Fabs (X1[i]-t1[i]) >1e-2) {temp=1;break;}
/* printf ("%d", temp); */
return temp;
}
int Ddcompute (double **a1,double b1[],double x1[],int N)/* Iterative Method Calculation * *
{Double *t;
int i,j,k=0;
Double sum1=0.0,sum2=0.0;
t= (double*) malloc (n*sizeof (double));
printf ("\nplease Input the Initial Value of x:\n");
for (i=1;i<=n;i++)
scanf ("%lf", &x1[i]);
do{k++;
for (i=1;i<=n;i++)
T[i]=x1[i];
for (i=1;i<=n;i++)
{sum1=0.0;sum2=0.0;
for (j=1;j<=i-1;j++) sum1=sum1+a1[i][j]*x1[j]; /*printf ("sum1=%0.4f", sum1);
for (j=i+1;j<=n;j++) sum2=sum2+a1[i][j]*t[j]; /* printf ("sum2=%0.4f", sum2);} */
if (a1[i][i]==0| | Fabs (sum1) >1e+12| | Fabs (sum2) >1e+12)
{printf ("\nwarning:these equtions can ' t be solve by this way!\n press any Key to continue ...");
Getch ();
Free (t);
return 0;}
x1[i]= (b1[i]-sum1-sum2)/a1[i][i];}
}while (Max (t,x1,n));
/* for (i=1;i<=n;i++)
{if (i%3==0) printf ("\ n");
printf ("%.4f", X1[i]); */
Free (t);
return 1; }
int Gscompute (double **a1,double b1[],double x1[],int N)/* Gaussian elimination method calculation * *
{int i,j,k;
Double m,sum;
for (k=1;k<=n-1;k++)
for (i=k+1;i<=n;i++)
{if (a1[k][k]==0) {printf ("\nthese equtions can ' t be solve are this way.\n press any Key to continue ...");
Getch ();
return 0; }
if ((m=0-a1[i][k]/a1[k][k)) ==0) {i++; continue;}
else {for (j=k+1;j<=n;j++)
A1[i][j]=a1[i][j]+a1[k][j]*m;
B1[I]=B1[I]+B1[K]*M}}
* Yi Xia ji suan x Zhi * *
X1[n]=b1[n]/a1[n][n];
for (i=n-1;i>=1;i--)
{sum=0.0;
for (j=n;j>=i+1;j--)
SUM=SUM+A1[I][J]*X1[J];
x1[i]= (b1[i]-sum)/a1[i][i];}
return 1; }
int Gs_ydcompute (double **a1,double b1[],double x1[],int N)/* Gauss _ Approximate Method calculation * *
{int i,j,k;
Double m,sum;
for (k=1;k<=n;k++)
{i=1;
while (i<=n)
{if (a1[k][k]==0) {printf ("\nthese equtions can ' t be solve are this way.\n press any Key to continue ...");
Getch (); return 0;}
if (i!=k)
{if ((m=0-a1[i][k]/a1[k][k)) ==0) {i++; continue;}
else {for (j=k+1;j<=n;j++)
A1[i][j]=a1[i][j]+a1[k][j]*m;
B1[i]=b1[i]+b1[k]*m;}
i++;}
else i++; }}
* Yi Xia ji suan x Zhi * *
for (i=n;i>=1;i--)
X1[i]=b1[i]/a1[i][i];
return 1;}
Double computed (double **a,int h,int l, int *c1,int n)/* COMPUTE coefficient determinant d value * *
{int I, j,p=1;
Double sum=0.0;
if (h==n)
sum=1.0;
else {
I=++h;
c1[l]=0;
for (j=1;j<=n;j++)
if (C1[j])
if (a[i][j]==0) p++;
else {sum=sum+a[i][j]*computed (a,i,j,c1,n) *pow ( -1,1+p); p++;} C1[l]=1; }
return sum; }
void Ncompute (double **a,double b[],double x[],int n,int *c,double h) * g-Lyme Method Calculation * *
{int i,j;
Double T[num];
for (j=1;j<=n;j++)
{for (i=1;i<=n;i++)
{t[i]=a[i][j];a[i][j]=b[i];}
X[j]=computed (a,0,0,c,n)/h;
for (i=1;i<=n;i++)
A[i][j]=t[i]; }
}
Main ()
{double X[num];
Double B[num];
int i,j=2,n=0;
int *c;
Double he;
Char m,decision;
Double **a;
A= (double**) malloc (num*sizeof (double*));
for (i=0; i<num; i++)
a[i]= (double*) malloc (num*sizeof (double));
Print ();
do{
CLRSCR ();
do{
if (n>=num) printf ("N is too large,please input again:\n");
Else
printf ("Please input the total number of the equations N (n<num): \ n");
scanf ("%d", &n);
}while (N>num);
if (n==0) {for (I=1, i<num; i++) free (a[i));
Free (a); Exit (1);}
Input (a,b,n);
c= (int *) malloc ((n+1) *sizeof (int));
Memset (c,1, (n+1) *sizeof (int));
He=computed (A,0,0,c,n);
if (Fabs (he) >1e-4)
{
Other:chose ();
do{
M=getche ();
}while (m!= ' A ' &&m!= ' B ' &&m!= ' A ' &&m!= ' B ' &&m!= ' C ' &&m!= ' C ' &&m!= ' d ') &&m!= ' D ' &&m!= ' e ' &&m!= ' e ');
Switch (m)
{case ' a ':;
Case ' A ': J=gscompute (a,b,x,n); Break
Case ' B ':;
Case ' B ': J=gs_ydcompute (a,b,x,n); Break
Case ' C ':;
Case ' C ': J=ddcompute (a,b,x,n); Break
Case ' d ':;
Case ' D ': j=1; Ncompute (A,b,x,n,c,he); Break
Case ' E ':;
Case ' E ': j=2; Break
default:j=2; Break
}
if (j==1)
{CLRSCR ();
printf ("\n\n\n\n");
printf ("d=%.4f \", he);
for (i=1;i<=n;i++)
{if (i%5==0) printf ("\ n");
printf ("%.4f", X[i]);
}
else if (j==0)
{printf ("\nthese equtions can ' t be solve it way.\nplease chose the other way.");
else {for (i=1; i<num; i++) Free (a[i));
Free (a);
Free (c);
Exit (1);}
}
else printf ("\n\n\td=%.4f\n This linear equations hasn ' t accurate answer!", he);
printf ("\ Does you want to continue?" ( y/n) \ n ");
do{
Decision=getchar ();} while (decision!= ' y ' &&decision!= ' y ' &&decision!= ' n ' &&decision!= ' n ');
}while (decision== ' y ') | | decision== ' Y ');
For (I=1, i<num; i++) free (a[i));
Free (a);
Free (c);}