一元高次方程C語言實(shí)現(xiàn)最高五次_第1頁
一元高次方程C語言實(shí)現(xiàn)最高五次_第2頁
一元高次方程C語言實(shí)現(xiàn)最高五次_第3頁
一元高次方程C語言實(shí)現(xiàn)最高五次_第4頁
一元高次方程C語言實(shí)現(xiàn)最高五次_第5頁
已閱讀5頁,還剩4頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、元高次方程c語言實(shí)現(xiàn)(windows VC6.0R本)注意:如果在linux vine或者UNIX以及非windows的系統(tǒng)下運(yùn)行的話將代碼pow以及 sqrt全部替換為powf和sqrtf以下為代碼#include <stdio . h>#include <stdlib . h>#include <math. h>#include <string . h>#define MAX 1000/*最大遞歸次數(shù)為1000次*/#define ERROR"math errorn" /* 顯示錯誤信息*/char Fun310,fx51

2、0="" ; /* 美化輸出結(jié)果 */float a1,b1,c1,d1,e1,f1;/*宏定義高次方程各項(xiàng)系數(shù)最大五次*/int NUMjdg;/*判斷函數(shù)最高次數(shù)*/float FC(float a, float b); /* 解一元一次方程 */floatFC3(floata,floatb,floatc,float d); /* 解一元三次方程 */floatFC2(floata,floatb,floatc,int num); /* 解一元二次方程 */floatFC4(floata,floatb,floatc,float d, float e); /* 解一元四次方

3、程*/float FC5(); /*解一元五次方程*/float fun( float x); /*五次函數(shù)代值求解*/float dfun( float x); /*五次導(dǎo)函數(shù)代值求解*/float iterate( float x); /*牛頓迭代法猜值求解*/ int main()float x5; /*五次方程最后一個解*/FILE *fp; /*解的內(nèi)容以文件的形式保存*/char save;char buf256;/*從文件內(nèi)讀取內(nèi)容保存至buf*/buf0 ='0' ;/*防止亂碼,開頭定義為NULL*/fp =fopen( "fun.dat"

4、 , "w");strcpy(Fun3, "" );fclose(fp);printf( "Do you want to save the result? 'y' or 'n'"); /* 是否想保存結(jié)果數(shù)據(jù)? */printf("n->" );scanf( "%c", &save);redo:printf( "please input degree numbern->" ); /* 請輸入最高次數(shù) */ scanf( &q

5、uot;%d", &NUMjdg););/*輸入最高次項(xiàng)系數(shù)*/if (NUMjdg<1| NUMjdg>5) goto redo;printf( "the number of a?->" scanf( "%f",&a1);if (a1 =0)printf(ERROR); return 1; printf( "the number of b?->");/* 輸入第二高次項(xiàng)系數(shù) */scanf( "%f, &b1);if (NUMjdg=1) printf( "

6、;the result is %gn",FC(a1,b1); goto end1; printf( "the number of c?->");/* 輸入第三高次項(xiàng)系數(shù) */ scanf( "%f" , &c1);if (NUMjdg=2)FC2(a1,b1,c1,1); goto end2; printf( "the number of d?->");/* 輸入第四高次項(xiàng)系數(shù) */scanf( "%f" , &d1);if (NUMjdg=3)FC3(a1,b1,c1,d1)

7、; goto end3; printf( "the number of e?->");/* 輸入第五高次項(xiàng)系數(shù) */scanf( "%f" , &e1);if (NUMjdg=4)FC4(a1,b1,c1,d1,e1); goto end4; printf( "the number of f?->"); /* 輸入常數(shù)項(xiàng)系數(shù) */scanf( "%f",&f1);x5 =FC5();end2:end4:if (a1 != 0) printf( "n*nThere is foll

8、owing result:n");fp =fopen( "fun.dat" , "r" );while (fgets(buf,256,fp) != NULL)printf( "%s",buf); fclose(fp); fp =fopen( "fun.dat" , "a" );if (NUMjdg=5) fprintf(fp, "x%s=%gn",fx5,x5);printf( "x%s=%gn",fx5,x5);fclose(fp);end1:

9、end3:if (save =-n') unlink( "fun.dat" ); getch();return 0;)/*以下為各項(xiàng)函數(shù)的定義*/float fun(float x)return(x*x*x*x*x*al+x*x*x*x*bl+x*x*x*cl+x*x*dl+x*el+fl);)float dfun(float x)return(5*al*x*x*x*x+4*bl*x*x*x+3*cl*x*x+2*dl*x+el);)float iterate(float x)float xl;xl=x-fun(x)/dfun(x);return(xl);)floa

10、t FC5()(int k=0, i;float x0=-2. 0, xl, de;float b, c, d, e, temp5;bl/'=al; cl/=al; dl/=al; el/=al; fl/=al; al=l. 0;printf("FC5 a=%g b=%g c=%g d=%g e=%g f=%gn,z, al, bl, cl, dl, el, fl);if (bl=二0. 0&&cl=0. 0&&dl=0. 0&&el=0. 0&&fl=0. 0)(return 0. 0;temp0=-b1/5;

11、 temptl=-cl/(2*bl); temp2=-dl/cl; temp3=2*el/(-dl); temp4=-5*fl/el;for(i=0; i<5; i+) printf (,*temp%d: %gz,, i, tempi);i f (temp 0 =temp 1 &&temp 0 =temp 2 &&temp 0 =temp 3&&temp0 =temp4 )(return temp0;dok+;xl=iterate(xO);/ printf (z,%d %gn”,k, xl);de=fabs(xl-xO);x0=xl;)wh

12、ile (de>=0.00001&&k<MAX);strcpy(fx5,"”);b =b1+x1; c =c1+b*x1; d =d1+c*x1; e =e1+d*x1;FC4(1.0,b,c,d,e);printf( "one of the result is %gn",x1);return x1;/*解法請參照盛金公式,費(fèi)拉里公式,牛頓迭代法,一元二次求根公式*/float FC4(float a, float b, float c, float d, float e)float y;float M,N,P;float y04;in

13、t i;b /= a; c /= a; d /= a; e /= a; a =1.0;printf( "FC4 a:%g b:%g c:%g d:%g e:%gn" ,a,b,c,d,e);y00 =8.0;y01 =-4.0 *c;y02 =-1*(8.0 *e-2.0*b*d);y03 =-e*(b*b-4.0*c)-d*d;for (i =1;i <=3;i +)printf( "y%d:%g",i,y0i);printf( "n");y =FC3(y00,y01,y02,y03); printf( "y:%g

14、" ,y);M =sqrt(8.0 *y+b*b- 4.0 *c);N =b*y- d;printf( "M:%g N:%gn" ,M,N);if (M=0)P =sqrt(y *y-e);printf( "P:%gn" ,P);FC2(2.0,b,2.0*(y +P),1);FC2(2.0,b,2.0*(y-P),3);else (FC2(2.0,b+M,2.0*(y +N M),1);FC2(2.0,b-M,2.0*(y-NM),3);)return 0.0;) float FC3(float a, float b, float c, fl

15、oat d)(float A,B,C,delta,x3,ni,fun3;float y1,y2,k,T,theta;float temp;FILE *fp;int i,y_1 =1,y_2 =1;printf( "FC3->a=%g b=%g c=%g d=%gn"a,b,c,d);A =b* b- 3*a*c;B =b*c- 9*a*d;C =c*c- 3*b*d;delta =B*B-4*A*C;printf( "fc3A:%g B:%g C:%gn" ,A,B,C);printf( "fc3 delta:%gn" ,del

16、ta);if (NUMjdg=3)printf( "There is following result:nn" ); fp =fopen( "fun.dat" , "w");)elsestrcpy(Fun3, "fun3:" );)if (delta =0)if (A=B)if (b =0)printf( "%sx1=x2=x3=%dn",Fun3,0); elseprintf( "%sx1=x2=x3=%gn" ,Fun3, - b/(3*a); if (b =0)fpri

17、ntf(fp,"%sx1=x2=x3=%dn",Fun3,0); elsefprintf(fp,"%sx1=x2=x3=%gn",Fun3, -b/(3*a); fun0 =-b/(3*a);fun1 =fun0;fun2 =fun1;if (NUMjdg=3)printf( "n" ); fclose(fp); return fun0;elsek =B/A;printf("%sx1=%gn" ,Fun3,fun0 =-b/a+k);printf("%sx2=%gn" ,Fun3,fun1 =-

18、k/2);fprintf(fp,"%sx1=%gn" ,Fun3,fun0 =-b/a+k);fprintf(fp,"%sx2=%gn" ,Fun3,fun1 =-k/ 2);if (NUMjdg=3)printf( "n" ); fclose(fp); if (fun0 >fun1)return fun0;elsereturn fun1;else if (delta >0)y1 =A*b+1.5*a*(- B+pow(delta,0.5);if (y1 <0.0)y_1=-1;y2 =A*b+1.5*a*(- B-

19、pow(delta,0.5);if (y2 <0)y_2=-1;if (NUMjdJ= 3)printf("y1: %gny2: %gn" ,y1,y2);x0=(-b-y_1*pow(fabs(y1),1.0 / 3.0)- y_2*pow(fabs(y2),1.0 / 3.0) /(3*a);x1=(-2*b+y_1*pow(fabs(y1),1.0 /3.0)+y_2*pow(fabs(y2),1.0 /3.0) /(6*a); ni =pow(3.0,0.5) *(y_1 *pow(fabs(y1),1.0 /3.0)-y_2*pow(fabs(y2),1.0

20、 / 3.0) /(6*a);printf("%sx1=%gn" ,Fun3,fun0 =x0);printf("%sx2=%g+%gin" ,Fun3,x1,fabs(ni);printf("%sx3=%g-%gin" ,Fun3,x1,fabs(ni);fprintf(fp,"%sx1=%gn" ,Fun3,fun0 =x0);fprintf(fp,"%sx2=%g+%gin" ,Fun3,x1,fabs(ni);fprintf(fp,"%sx3=%g-%gin" ,Fu

21、n3,x1,fabs(ni);fun1=fun0;fun2=fun1;if (NUMjdg=3) printf( "n" ); fclose(fp); return fun0; else T =(2*A* b- 3*a* B)/ (2 * pow(A,1.5); theta=acos( T);theta/= 3;if (NUMjdJ=3) printf("T:%gn" ,T);printf("theta: %gn",theta); /cs = theta+2/3*i*acos(-1);x0=(- b-2*pow(A,0.5) *cos

22、(theta) / (3*a);x1=(- b+pow(A,0.5) *(cos(theta) +pow(3,0.5) *sin(theta) / (3*a);x2=(- b+pow(A,0.5) *(cos(theta) -pow(3,0.5) *sin(theta) / (3*a);for (i =0;i <3;i +) printf( "%sx%d=%gn",Fun3,i +1,funi =xi); if (NUMjdg=3) for (i =0;i <3;i +)fprintf(fp, "%sx%d=%gn",Fun3,i +1,funi =xi); if (fun0 >fun1) temp=fun0; else temp=fun1; if (NUMjdg=3) printf( "n" ); fclose(fp); if (temp>fun2)return temp; elsereturn fun2; float FC2(float a, f

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論