麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > C > 正文

關于C語言解線性方程的方法

2020-02-24 14:24:45
字體:
來源:轉載
供稿:網友

關于C語言解線性方程的方法是不是有很多小伙伴們不了解呢?那么我們今天就帶著疑問一起跟小編去學習C語言解線性方程的四種方法,希望本文對你有一定的幫助。

發了好幾天編了個解線性方程組的小程序,可第一次實戰就大敗而歸。經過半天的調試,仍找不出糾正的方法。因為并不是算法的問題,而是因為自己對編譯器處理 浮點函數的方法不是很理解。明明D=0的方陣解出來不等于0了,跟蹤調試發現,計算過程程序對數據進行了舍去處理,導致最終結果不對。不過如果沒有浮點型 的話,這個程序應該算不錯了 。
?

?

#include<stdio.h>
#include<math.h>
#include<mem.h>
#define NUM 100
void print(void)???? /* 使用說明 */
????? { clrscr();
??????? printf("/n/n/n/n/n/t/t/t/t 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("/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)??? /*數據輸入*/
???? { 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;}
???? /* printf("??? %d??? ",temp); */
????????? 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]; /*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) /*高斯消元法計算*/
???? {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;}}
/* 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) /*高斯_約當法計算*/
???? {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++;??? }}
/* 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) /*計算系數行列式D值*/
?????? { 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; 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 /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<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(" /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; i<NUM; i++) free(a[i]);
free(a);
free(c);}

以上就是關于C語言解線性方程的方法,文章中小編主要介紹了C語言解線性方程的四種方法,大家參考使用,學習線性代數的同學一定能用到。?

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 哪里可以看免费的av | 久久久久久亚洲综合影院红桃 | 国产午夜精品久久久久久久蜜臀 | 91久久久久久久久久久久久 | 欧美成人一区二区三区电影 | 渔夫荒淫艳史 | 欧美日韩大片在线观看 | 中文字幕亚洲一区二区三区 | 超碰人人做人人爱 | 91精品国产日韩91久久久久久360 | 国产日韩线路一线路二 | 久久综合久久美利坚合众国 | 久久第四色 | 精品国产一区二区三区四区在线 | 久久伊人国产精品 | 香蕉视频99 | 日本免费一区二区三区四区 | 久久国产精品久久久久久电车 | 免费国产在线视频 | 新久草在线视频 | 男人天堂免费 | 中国av一级片 | 性生活视频软件 | 天堂二区 | 国内精品久久久久久久久久 | 成人视屏在线 | av在线官网 | 黄色片视频观看 | www.99tv| 亚洲男人一区 | 97人人草 | 色交视频| 中国大陆高清aⅴ毛片 | a级毛片免费观看在线播放 日本aaa一级片 | 欧美精品一区二区三区在线 | 一级免费| 国产一区二区三区精品在线观看 | 国产精品一区二av18款 | 久久91亚洲精品久久91综合 | 久久精品国产一区二区电影 | 久久国产秒 |