一元高次方程C语言实现(最高
五次)
一元高次方程c语言实现(windowsVC6.0版本)
注意:如果在linuxvine或者UNIX以及非windows的系统下运行的话将代码pow以及
sqrt全部替换为powf和sqrtf
以下为代码
#include
#include
#include
#include
#defineMAX1000/*最大递归次数为1000次*/
#defineERROR"matherrorn"/*显示错误信息*/
c过河小卒 harFun3[10],fx5[10]="";/*美化输出结果*/
floata1,b1,c1,d1,e1,f1;/*宏定义高次方程各项系数最大五次*/
intNUMjdg;/*判断函数最高次数*/
floatFC(floata,floatb);/*解一元一次方程*/
floatFC3(floata,floatb,floatc,floatd);/*解一元三次方程*/
floatFC2(floata,floatb,floatc,intnum);/*解一元二次方程*/
floatFC4(floata,floatb,floatc,floatd,floate);/*解一元四次方程*/
floatFC5();/*解一元五次方程*/
floatfun(floatx);/*五次函数代值求解*/
floatdfun(floatx);/*五次导函数代值求解*/
floatiterate(floatx);/*牛顿迭代法猜值求解*/
intmain()
{
floatx5;/*五次方程最后一个解*/
FILE*fp;/*解的内容以文件的形式保存*/
charsave;
charbuf[256];/*从文件内读取内容保存至buf*/
buf[0]='0';/*防止乱码,开头定义为NULL*/
fp=fopen("","w");
strcpy(Fun3,"");
fclo(fp);
printf("Doyouwanttosavetheresult?'y'or'n'");/*是否想保存结果
数据?*/
prin烧平鱼 tf("n--->");
scanf("%c",&save);
redo:
printf("pleainputdegreenumbern--->");/*请输入最高次数*/
scanf("%d",&NUMjdg);
if(NUMjdg<1||NUMjdg>5)gotoredo;
printf("thenumberofa?--->");/*输入最高次项系数*/
scanf("%f",&a1);
if(a1==0)
{printf(ERROR);return1;}
printf("thenumberofb?--->");/*输入第二高次项系数*/
scanf("%f",&b1);
if(NUMjdg==1)
{printf("theresultis%gn",FC(a1,b1));gotoend1;}
printf("thenumberofc?--->");/*输入第三高次项系数*/
scanf("%f",&c1);
if(NUMjdg==2)
{FC2(a1,b1,c1,1);gotoend2;}
printf("thenumberofd?--->");/*输入第四高次项系数*/
scanf("%f",&d1);
if(NUMjdg==3)
{FC3(a1,b1,c1,d1);gotoend3;}
printf("thenumberofe?--->");/*输入第五高次项系数*/
scanf("%f",&e1);
if(NUMjdg==4)
{FC4(a1,b1,c1,d1,e1);gotoend4;}
printf("thenumberoff?--->");/*输入常数项系数*/
scanf("%f",&f1);
x5=FC5();
end2:end4:
if(a1!=0)printf("n********nThereisfollowingresult:n");
fp=fopen("","r");
while(fgets(buf,256,fp)!=NULL)
{printf("%s",buf);}
fclo(fp);fp=fopen("","a");
if(NUMjdg==5){
fprintf(fp,"x%s=%gn",fx5,x5);
printf("x%s=%gn",fx5,x5);
}
fclo(fp);
end1:
end3:
if(save=='n')
{unlink("");}
getch描写人生的句子 ();
return0;
}
/*以下为各项函数的定义*/
floatfun(floatx)
{return(x*x*x*x*x*a1+x*x*x*x*b1+x*x*x*c1+x*x*d1+x*e1+f1);
}
floatdfun(floatx)
{return(5*a1*x*x*x*x+4*b北京英文介绍 1*x*x*x+3*c1*x*x+2*d1*x+e1);
}
floatiterate(floatx)
{floatx1;
x1=x-fun(x)/亚洲百合 dfun(x);
return(x1);
}
floatFC5()
{
intk=0,i;
floatx0=-2.0,x1,de;
floatb,c,d,e,temp[5];
b1/=a1;c1/=a1;d1/=a1;e1/=a1;f1/=a1;a1=1.0;
printf("FC5a=%gb=%gc=%gd=%ge=%gf=%gn",a1,b1,c1,d1,e1,f1);
if(b1幼鸽开家 ==0.0&&c1==0.0&&d1==0.0&&e1==0.0&&f1==0.0)
{return0.0;}
temp[0]=-b1/5;temp[1]=-c1/(2*b1);temp[2]=-d1/c1;temp[3]=2*e1/(-d1);
temp[4]=-5*f1/e1;
for(i=0;i<5;i++)printf("temp[%d]:%g",i,temp[i]);
if(temp[0]==temp[1]&&temp[0]==temp[2]&&temp[0]==temp[3]&&temp[0]==temp[
4])
{returntemp[0];}
do
{k++;
x1=iterate(x0);
//printf(win10更新 "%d%gn",k,x1);
de=fabs(x1-x0);
x0=x1;
}
while(de>=0.00001&&k
strcpy(柳青创业史 fx5,"[5]");
b=b1+x1;c=c1+b*x1;d=d1+c*x1;e=e1+d*x1;
FC4(1.0,b,c,d,e);
printf("oneoftheresultis%gn",x1);
returnx1;
}
/*解法请参照盛金公式,费拉里公式,牛顿迭代法,一元二次求根公式*/
floatFC4(floata,floatb,floatc,floatd,floate)
{
floaty;
floatM,N,P;
floaty0[4];
inti;
b/=a;c/=a;d/=a;e/=a;a=1.0;
printf("FC4a:%gb:%gc:%gd:%ge:%gn",a,b,c,d,e);
y0[0]=8.0;
y0[1]=-4.0*c;
y0[2]=-1*(8.0*e-2.0*b*d);
y0[3]=-e*(b*b-4.0*c)-d*d;
for(i=1;i<=3;i++)
{printf("y%d:%g",i,y0[i]);}
printf("n");
y=FC3(y0[0],y0[1],y0[2],y0[3]);
printf("y:%g",y);
M=sqrt(8.0*y+b*b-4.0*c);
N=b*y-d;
printf("M:%gN:%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);
}
el
{
FC2(2.0,b+M,2.0*(y+N/M),1);
FC2(2.0,b-M,2.0*(y-N/M),3);
}
return0.0;
}
floatFC3(floata,floatb,floatc,floatd)
{
floatA,B,C,delta,x[3],ni,fun[3];
floaty1,y2,k,T,theta;
floattemp;
FILE*fp;
inti,y_1=1,y_2=1;
printf("FC3-->a=%gb=%gc=%gd=%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:%gB:%gC:%gn",A,B,C);
printf("fc3delta:%gn",delta);
if(NUMjdg==3){
printf("Thereisfollowingresult:nn");
fp=fopen("","w");
}
el{
strcpy(Fun3,"fun3:");
}
if(delta==0)
{
if(A==B)
{
if(b==0)
{printf("%sx[1]=x[2]=x[3]=%dn",Fun3,0);}
el
{printf("%sx[1]=x[2]=x[3]=%gn",Fun3,-b/(3*a));}
if(b==0)
{fprintf(fp,"%sx[1]=x[2]=x[3]=%dn",Fun3,0);}
el
{fprintf(fp,"%sx[1]=x[2]=x[3]=%gn",Fun3,-b/(3*a));}
fun[0]=-b/(3*a);
fun[1]=fun[0];
fun[2]=fun[1];
if(NUMjdg==3)
{printf("n");fclo(fp);}
returnfun[0];
}
el
{
k=B/A;
printf("%sx[1]=%gn",Fun3,fun[0]=-b/a+k);
printf("%sx[2]=%gn",Fun3,fun[1]=-k/2);
fprintf(fp,"%sx[1]=%gn",Fun3,fun[0]=-b/a+k);
fprintf(fp,"%sx[2]=%gn",Fun3,fun[1]=-k/2);
if(NUMjdg==3)
{printf("n");fclo(fp);}
if(fun[0]>fun[1])
{returnfun[0];}
el
{returnfun[1];}
}
}
elif(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-pow(delta,0.5));
if(y2<0)
{y_2=-1;}
if(NUMjdg!=3)printf("y鱼腥草口服液 1:%gny2:%gn",y1,y2);
if(temp>fun[2])
{returntemp;}
el
{returnfun[2];}
}
}
floatFC2(floata,floatb,floatc,intnum)
{
FILE*fp;
floatd;
printf("FC2a:%gb:%gc:%gn",a,b,c);
d=b*b-4*a*c;
fp=fopen("","a");
if(d<0)
{
fprintf(fp,"x[%d]=%g+%gin",num,-b/2/a,pow(-d,0.5)/(2*a));
fprintf(fp,"x[%d]=%g-%gin",num+1,-b/2/a,pow(-d,0.5)/(2*a));
}
elif(d==0)
{
if(b==0.0)
{fprintf(fp,"x[%d]==x[%d]==0n",num,num+1);}
el
{fprintf(fp,"x[%d]==x[%d]==%gn",num,num+1,-b/2/a);}
}
el
{
fprintf(fp,"x[%d]=%gn",num,(-b+pow(d,0.5))/(2*a));
fprintf(fp,"x[%d]=%gn",num+1,(-b-pow(d,0.5))/(2*a));
}
fclo(fp);
return0.0;
}
floatFC(floata,floatb)
{FILE
*fp;fp=fopen("","a");fprintf(fp,"x=%gn",-b/a);fclo(fp);return
-b/a;}
本文发布于:2023-03-26 23:02:25,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/d8c67ede04879c1c3029ab6362af169d.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:高五.doc
本文 PDF 下载地址:高五.pdf
留言与评论(共有 0 条评论) |