?? 消元法.txt
字號:
/*GAUSS1.0*/
/*只允許處理實系數方陣方程*/
#include <math.h>
#include <stdio.h>
#define M 10 /*系數矩陣的規模*/
main()
{
/*******************************以下是全局變量定義區***************************************************/
float a[M][M]={{0}},b[M]={0},x[M]={0.0},y[M]={0.0},com,cen;/*com是主元素.為什么無法設為DOUBLE??*/
/*******************************以上是全局變量定義區***************************************************/
/*******************************以下是局部變量定義區***************************************************/
int i,j,k,m=100,n,key=0,p,q,xor[M],restart=1,min; /*i、m是方程數和行數,j、n是未知數數和列數,p,q是主元素的行、列下標,key是重新輸入開關和中間變量*/
for(i=0;i<M;i++) xor[i]=i;
/*********************************以上是局部變量定義區***************************************************/
/********************************************************************************************************/
/*******************************以下是變量輸入區*********************************************************/
while(restart==1)
{key=0;
while(key==0)
{
printf("\nProgram start!\n\n\nEnter the number of x n,take care that 1<=n<=%d:",M);
scanf("%d",&n);
printf("How many equations in the question?Enter it:");
scanf("%d",&m);
min=(m>n?n:m);
/*這里規定系數矩陣為方陣*/
for(i=0;i<m;i++)
{printf("Input %d quotions of x[i] in No.%2d equation:\n",n,i+1);
for(j=0;j<n;j++)
scanf("%f",&a[i][j]);
printf("Input b[%2d]:",i+1);
scanf("%f",&b[i]);
}
printf("Enter complete!\nYour equations' quotion are:\n****************************************************************\n");
for(j=0;j<n;j++)
printf(" x%2d ",j+1);
printf("= b \n");
for(i=0;i<m;i++)
{for(j=0;j<n;j++)
printf("%9.5f ",a[i][j]);
printf(" %9.5f\n",b[i]);
}
printf("****************************************************************\nAre they correct?Yes enter 1;No enter 0 to input again:");
scanf("%d",&key);
restart=0;
}
/*********************************以上是變量輸入區***************************************************/
/*********************************以下是高斯消去區***************************************************/
/*先找主元素,再消去*/
for(k=0;k<min;k++)
{com=a[k][k];p=k;q=k;
for(i=k;i<m;i++)
for(j=k;j<n;j++)
if(a[i][j]>com)
{com=a[i][j];p=i;q=j;}
/*主元素是com,它的行下標和列下標是p q*/
for(j=0;j<n;j++)
{cen=a[k][j];a[k][j]=a[p][j];a[p][j]=cen;}
cen=b[k];b[k]=b[p];b[p]=cen;
for(i=0;i<m;i++)
{cen=a[i][k];a[i][k]=a[i][q];a[i][q]=cen;}
key=xor[k];xor[k]=xor[q];xor[q]=key;
/*主元選取完畢。XOR已經交換。以下是高斯消去*/
for(i=k+1;(i<m&&com!=0);i++)
{cen=-a[i][k]/a[k][k];
for(j=k;j<n;j++)
a[i][j]=a[i][j]+cen*a[k][j];
b[i]=b[i]+cen*b[k];
}
/*高斯消去完成!*/
}
/**/printf("Enter complete!\nYour equations' quotion are:\n");
for(j=0;j<n;j++)
printf(" x%2d ",j+1);
printf(" b \n");
for(i=0;i<m;i++)
{for(j=0;j<n;j++)
printf("%9.5f ",a[i][j]);
printf(" %9.5f\n",b[i]);
}/*檢驗完成!*/
/*********************************以上是高斯消去區***************************************************/
/*********************************以下是結果檢驗區***************************************************/
key=1;
for(i=0;i<n;i++)
{cen=0.0;
for(j=0;j<n;j++)
cen=cen+fabs(a[i][j]);
if(fabs(cen)<0.00001&&fabs(b[i])<0.00001)
{key=0;
break;
}
else if(fabs(cen)<0.00001&&fabs(b[i])>0.00001)
{key=-1;
break;
}
}
if(key==0)
{printf("\nThe equations have infinit answer groups because some of the equations you entered are invalid.\nDo you want to enter the equations again?\nEnter 1 to enter again;enter 0 to exit:");
scanf("%d",&restart);
}
else if(key==-1)
{printf("\nThe equations have no answer group because some of the equations you entered are contradictory.\nDo you want to enter the equations again?\nEnter 1 to enter again;enter 0 to exit:");
scanf("%d",&restart);
}
}
if(key==1)
{
/*********************************以上是結果檢驗區***************************************************/
/*********************************以下是回代求解區***************************************************/
for(k=n-1;k>=0;k--)
{cen=0;
for(j=m-1;j>k;j--)
cen=cen+y[j]*a[k][j];
y[k]=(b[k]-cen)/a[k][k];
}
for(i=0;i<n;i++)
x[xor[i]]=y[i];
/*********************************以上是回代求解區***************************************************/
/*********************************以下是結果輸出區***************************************************/
for(i=0;i<n;i++)
printf("x%2d=%9.5f\n",i+1,x[i]);
restart=0;key=0;
/*********************************以上是結果輸出區***************************************************/
/*********************************以下是結果檢驗區***************************************************/
}
/*********************************以上是結果檢驗區***************************************************/
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -