码迷,mamicode.com
首页 > 其他好文 > 详细

高斯消元法

时间:2017-11-28 20:41:51      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:结果   coding   printf   tput   线性   ++   sel   lag   for   

1.已经实现的功能:

  1.线性方程组的输入

  2.高斯消元过程

  3.回代过程

  4.还差方程组解的判断

coding:

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<string>
  4 
  5 #define MAX(a,b) a>b?a:b
  6 
  7 bool gaussElin();//高斯消元函数
  8 bool intputA_B();//输入矩阵A,B
  9 int  solvLinearEqu();//线性方程组解的情况
 10 /*
 11     声明全局静态变量
 12 */
 13 static int row;//系数矩阵列
 14 static int col;//系数矩阵行
 15 static float **A;//系数矩阵
 16 static float *B;//常数项
 17 static float *X;//方程组的解
 18 static bool flag;//是否为齐次线性组
 19 /*
 20     main函数
 21 */
 22 int main(){
 23     bool con = true;
 24         while(con){
 25             bool is = intputA_B();
 26                 if(is){
 27                     if(gaussElin()){
 28                         printf("最终结果:\n");
 29                         for(int i = 0;i<row;i++)
 30                            printf("%f ",x[i]);
 31                         printf("\n");
 32                     }
 33                     else{
 34                         printf("不能进行高斯消\n");
 35                     }
 36               }
 37               else{
 38                      printf("是否重新输入是则输入:yes,否则输入:\n");
 39                      char a[5];
 40                      scanf("%s",a);
 41                      if(strcmp("yes",a)){
 42                             continue;
 43                      }
 44                      if(strcmp("no",a)){
 45                             con =false;
 46                      }
 47               }
 48        }
 49               return ;
 50 }
 51 bool intputA_B()
 52 {
 53        int i = 0,j = 0;
 54        printf("输入矩阵的行列,用空格隔开\n");
 55        
 56        float n,m;
 57        scanf("%f%f",&n,&m);
 58        row = (int)n;
 59        col=     (int)m;
 60        
 61        //检查输入是否正确
 62        if((row!=n)||(col!=m)){
 63               printf("输入错误!!!!\n");
 64               return false;
 65        }
 66        //分配内存空间
 67        A = (float **)malloc(sizeof(float *)*row);
 68        for(i = 0;i<row;i++)
 69        {
 70               A[i] = (float *)malloc(sizeof(float)*col);
 71        }
 72        B = (float *)malloc(sizeof(float)*row);
 73        //输入A矩阵
 74        printf("依次输入系数矩阵\n");
 75        char c ;
 76        for(i = 0;i<row;i++)
 77         {
 78             for(j = 0;j<col;j++)
 79             {
 80                 scanf("%f",&A[i][j]);
 81             }
 82             c = getchar();//换行
 83         }
 84        printf("依次输入常数\n");
 85 
 86        for(i= 0;i<row;i++){
 87             scanf("%f",&B[i]);
 88             if(B[i]==0){
 89                 flag = 1;
 90             }
 91        }
 92        //换行
 93        c = getchar();
 94        return true;
 95 }       
 96 /*
 97     return:bool
 98     进行高斯消元
 99     如果主元素为0,则返回false
100 */
101 bool xiaoyuan(){
102     for(int i = 0;i<row-1;i++){
103     if(A[i][i]==0){
104         return false;
105     }
106     for(int j = i+1;j<row;j++)
107     {
108         float step = A[j][i]/A[i][i];//消元的倍数
109             for(int k = i;k<col;k++){
110                 A[j][k] = A[j][k]-A[i][k]*step;
111             }
112             B[j] = B[j]-B[i]*step;
113         }
114      }
115     return true;
116 }
117 /*
118     回代的过程
119 */
120 void huidai(){
121     int i;
122     for( i = row-1;i>=0;i--){
123         float sum = 0;
124         for(int j = i+1;j<row;j++){
125             sum = sum+x[j]*A[i][j];
126         }
127         x[i] = (B[i]-sum)/A[i][j];
128     }
129 }
130 /*
131     高斯消元过程
132 */
133 bool gaussElin( ){
134     //存储结果
135      x = (float*)malloc(row*sizeof(float));
136      //回带过程
137      if(xiaoyuan())
138     {
139         return false;
140     }
141     else
142         return true;
143     huidai();
144     return true;
145 }
146 /*
147     return 方程组解的情况
148     
149 */
150 int solvLinearEqu(){
151     if(gaussElin()==false){
152         //高斯消元不成功
153         return 0;
154     }
155     //齐次方程组
156     if(flag==1){
157         if(row>=col){
158             if(A[col-1][col-1]!=0){
159                 memset(X,0,sizeof(X));
160                 return 1;//维一解
161             }
162             else{
163                 return 2;//无穷多解
164             }
165         }
166         else{
167             return 2;//无穷多解
168         }
169     else{
170         
171     }
172             
173 }
174     

 

高斯消元法

标签:结果   coding   printf   tput   线性   ++   sel   lag   for   

原文地址:http://www.cnblogs.com/shiyiandchuixue/p/7911814.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!