版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、c语言解线性方程的四种方法发了好几天编了个解线性方程组的小程序,可第一次实战就大败而归。经过半天的调试,仍 找不出纠正的方法。因为并不是算法的问题,而是因为自己对编译器处理浮点函数的方法 不是很理解。明明d=0的方阵解出来不等于0 了,跟踪调试发现,计算过程程序对数据进 行了舍去处理,导致最终结果不对。不过如果没有浮点型的话,这个程序应该算不错了。 复制代码代码如下:# include<stdio.h>#in clude<math.h>#include<mem.h>#define num 100void print(void) /* 使用说明 */clrsc
2、rf);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;(lnput 0 to exit) *n”);prin tf("t*sec on
3、d: in put the coefficie nt of every equti on;*n“);printf("t*third : input the constant of every equ廿on;*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*nu);getchf);void
4、 chose(void)/*选择计算方法*/clrscrf); fflush(stdin);printf("nnnnntt*lntroduction* 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*
5、 d : cramer way.* n");printf("tt* e : exit.* n");prin tf(mtt * * * by -pj* *、门11”printf(uttplease choose number:n");void inputfdouble *al,double bl,int num)/*数据输入*/int i jt;double *p;char del,de2;do printf("please input array a%d%d: n",num,num);prin tf("warn: the f
6、irst nu mber of the array must n't con tain zero! n”); for(i=l;i< 二 nu m;i+)printf("please input array a%d: n"j);for(j=l;j<=nu m;j+)t=o;if(i=l&&j=l)doif(t=o) scanf(”lf”,&alij); t+;else printf("the input is invalid,please input again:n"); scanf(”f",&
7、alij); while(alij=o);else scanf(,%lf,/&alij);prin tf(" n please check the value of array a%d%d,press y to in put agai n.n”,num, num); dodel=getch();while(del!='y'&&del!='y'&&del!='n'&&del!='n");while(del='y,| |del='y');dop
8、rin tf("please in put array b%d: n",num);p=bl+l;for(i=l;i<=nu m;i+)scanf(”lf”,p+);prin tf("n pleasecheck the value of array b%d,press y to inputagaiwww.h un n”,n um);dode2=getch();while(de2!=y&&de2uy'&&de2un'&&de2!»n');while(de2='y'|
9、 |de2='y');int max(double *tl, double xl,int n) /*迭代子甫数*/int i,temp=0;for(i=l;i< 二 n ;i+)if(fabs(xli-tli)>le-2) temp=l;break;/* printf(" %d temp); */return temp;int ddcompute(double *al,double bl,double xl,int n) /*迭代法计算*/double *t;int i,j,k=o;double suml=0.0,sum2=0.0;t=(double*)m
10、alloc(n*sizeof(double);printf("nplease input the initial value of x:n");for(i=l;i<=n;i+)scanf("%lf"z&xli);do k+;for(i=l;i<=n;j+)ti=xli;for(i=l;i< 二 n ;i+)suml=0.0;sum2=0.0;for(j=l;j<=i-l;j+) suml=suml+alij*xlj; /*printf(" suml= %0.4f "suml);*/for(j=i+l;j
11、<=n;j 卄)sum2=sum2+alij*tj; /* printf(" sum2= %0.4fn,sum2);*/if(alii=o| |fabs(suml)>le+12| |fabs(sum2)>le+12)printf(" nwarning: these equtions can't be solve by this way!n press any key tocontinu e.");getch();free(t);return 0;xli=(bli-suml-sum2)/alii;while(max(tzxl,n);/* f
12、or(i=l;i<=n;i+)if(i%3=0) printf("nk);printf(n %.4f>11);*/free(t);return 1;int gscompute(double *al,double bl,double xl,int n) /*高斯消元法计算*/int ij,k;double m,sum;for(k=l;k<=n-l;k+)for(i=k+l;i<=n;i+) if(alkk=o) printf(” nthese equtions can't be solve is this n press any key to conti
13、nugetch();return 0;if(m=0-alik/alkk)=0) i+; continue;else for(j=k+l;j<=n;j+)alij=alij+alkj*m;bli=bli+blk*m;/* yi xia ji suan x zhi */xln=bln/alnn;for(i=n-l;i>=l;i-)sum=0.0;for(j=n;j>=i+l;j-)sum=sum+alij*xlj;xli=(bli-sum)/alii;return 1;int gs_ydcompute(double *al,double bl,double xl,int n)/*
14、高斯纟勺当法计算*/int ij,k;double m,sum;for(k=l;k<=n;k+)i=l;while(i< 二 n) if(alkk=o) printf nthese equtions can't be solve is this way.n press anykey to continue.n);getch();return 0;if(i!=k)if(m=0-alik/alkk)=0) i+; continue;else for(j=k+l;j<=n;j+)alij=alij+alkj*m;bli=bli+blk*m;i+;else i+;/* yi
15、xia ji suan x zhi */for(i=xli=bli/alii;return 1;double computed(double *a,int h,int i, int *cl,int n) /*计算系数行列式 d 值*/int i, j,p=l;double sum=0.0;讦(h二二n)sum=1.0;else i=+h;cll=0;for(j=l;j<=n;j+)if(clj)if(aij=o) p+;else sum=sum+aij*computed(aj,j,cl, n)*pow(l,l+p);p+;cll=l;return sum;void ncompute(do
16、uble *a,double b,double x,int n,int *c,double h)/*克莱姆法计算*/int ij;double tnum;for(j=l;j<=n;j+)for(i=l;i< 二 n ;i+)ti=aij;aij=bi;xj=computed(a,0,0,c, n)/h;for(i=l;i< 二 n ;i+)aij=ti;main()double xnum;double bnum;int i,j二2,n=0;int *c;double he;char m,decision;double *a;a=(double*)malloc(num*size
17、of(double*);for (i=0; i<num; i+)ai=(double*)malloc(num*sizeof(double);print();doclrscr();doif(n>=num) printfn is too large, please in put agai n:rt);elseprin tf(hplease in put the total nu mber of the equations n(n<n um): nh);scanf(" %d",&n);while(n>num);if(n=o) for(i=l; i&
18、lt;num; i+) free(ai);free(a); exit(l);input(a,b,n);c=(int *)malloc(n+l)*sizeof(int);memset(c,l,(n+l)*sizeof(int);he=computed(a,0,0,c, n);if(fabs(he)>le-4)other:chose();dom=getche();while(m!='a'&&m!二&&m!='a'&&m!='b'&&m!='c'&&m
19、!='c'&&m!='d'&&m!='d'&&m'e'&&m!二'e');switch(m)case 0:;case 'a1: j=gscompute(a,b,x,n); break;case 'b1:;case 'b': j=gs_ydcompute(a,b,x,n); break;case 'c1:;case c: j=ddcompute(a,b,x,n); break;case "d1:;case
20、 d: j=l; ncompute(abx,nche); break;case lel:;case e: j=2; break;default: j=2; break;if(j=d clrscr();printf(”nnnn“);printff' d=%.4f n",he);for(i=l;i<=n ;i+)if(i%5=0) printfcxn");printf(" %.4f“,xi);else if(j=o)printf(" nthese equtions can't be solve is this way.nplease chose the other way."); goto other;el
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 生物标志物在药物临床试验中的医学转化实践
- 生物材料与血管化策略研究
- 生物可吸收支架术后双抗治疗时长新进展
- 生物制剂临床试验中受试者退出干预机制
- 林业集团总会计师考试题库
- 运动康复师面试题及专业知识梳理含答案
- 交互设计考试题及答案解析
- 深度解析(2026)《GBT 19486-2004电子政务主题词表编制规则》
- 生命末期医疗决策中的知情同意替代方案
- 土壤环境测试技术规范
- 心肌病健康宣教
- 2025-2030中国泥浆刀闸阀行业需求状况及应用前景预测报告
- 选矿厂岗位安全操作规程
- 成人床旁心电监护护理规程
- T/CEPPEA 5028-2023陆上风力发电机组预应力预制混凝土塔筒施工与质量验收规范
- DB3308173-2025化工企业消防与工艺应急处置队建设规范
- 2025股权质押借款合同范本
- 晚会聘请导演协议书
- 电迁改监理实施细则
- 促脉证中医护理方案
- 排污许可合同模板
评论
0/150
提交评论