栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C语言解线性方程的四种方法

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C语言解线性方程的四种方法

发了好几天编了个解线性方程组的小程序,可第一次实战就大败而归。经过半天的调试,仍找不出纠正的方法。因为并不是算法的问题,而是因为自己对编译器处理 浮点函数的方法不是很理解。明明D=0的方阵解出来不等于0了,跟踪调试发现,计算过程程序对数据进行了舍去处理,导致最终结果不对。不过如果没有浮点型 的话,这个程序应该算不错了 。

复制代码 代码如下:
#include
#include
#include
#define NUM 100
void print(void)    
      { clrscr();
        printf("nnnnntttt Introduction n");
        printf("t*--------------------------------------------------------------*n");
        printf("t*    This program was design for compute linear equations.       *n");
        printf("t*    The way of use it is 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*    That's all, input any key to run it . . .                   *n");
        printf("t*-------------------------By__TJX------------------------------*n");
        getch(); }

void chose(void)   
    { clrscr();
       fflush(stdin);
        printf("nnnnntt**********Introduction********** n");
                  printf("tt* Chose the way,please.        * n");
                  printf("tt* a : Gauss eliminant.         * n");
                  printf("tt* b : Gauss_yd eliminant.      * n");
          printf("tt* c : Iterative way.           * n");
                  printf("tt* d : Cramer way.              * n");
                  printf("tt* e : exit.                    * n");              
                  printf("tt*************By__TJX************ n");
                  printf("ttPlease choose number :n");}

void input(double **a1,double b1[],int num)   
     { int i,j,t;
       double *p;
       char de1,de2;
do{
     printf("Please input array a[%d][%d]: n",num,num);
     printf("Warn: The first number 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)   
     { int i,temp=0;
           for(i=1;i<=n;i++)
          if(fabs(x1[i]-t1[i])>1e-2) {temp=1;break;}
    
          return temp;
     }

int ddcompute(double **a1,double b1[],double x1[],int n)
      {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];

               for(j=i+1;j<=n;j++) sum2=sum2+a1[i][j]*t[j];
           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));
    
     free(t);
           return 1; }

int gscompute(double **a1,double b1[],double x1[],int n)
     {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 is 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;}}

    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)
     {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 is 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++;    }}
   
    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)
       { 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)     
      {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; ia[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          scanf(" %d",&n);
       }while(n>NUM);
    if(n==0) {for(i=1; 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("nnnn");
                printf("     D=%.4f n",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 is this way.nPlease chose the other way."); goto Other;}
          else {for(i=1; i          free(a);
          free(c);
          exit(1);}
         }
       else printf(" nntD=%.4fn This linear equations hasn't accurate answer!",he);
       printf(" n Do 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; ifree(a);
free(c);}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/66020.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号