C语言关于求根公式的简单问题

来源:百度知道 编辑:UC知道 时间:2024/06/22 20:32:05
#include<stdio.h>
#include<math.h>
void main()
{
float a,b,c,x1,x2,d,q,w,r;
printf("Please input the three coefficients:");
scanf("%f,%f,%f",&a,&b,&c);
d=sqrt(b*b-4*a*c);
if((b*b==4*a*c){q=(-b)/2/a;printf("x1=x2=q\n",b,a);}
else if(b*b>4*a*c) {printf("w=(-%f+%f)/2/%f,r=(-%d-%d)/2/%d\n",b,d,a,b,d,a);

}else printf("There is no real root.\n");}请帮我看看哪有错,多谢大家拉。。。。。。。。。。。。请帮我找找详细解决方案,有一个问题是得到的答案是一个运算式如:7/8/2。而不是一个值。请问改怎么解决??

错误:if((b*b==4*a*c),将if后边的括号去掉一个;

这样之后,程序在VC6下编译通过,不过运行有错,必须要对float变量初始化一下,不然会全部是零;修改如下:
#include<stdio.h>
#include<math.h>
void main()
{
float a=1.111,b=0.0,c=0.0,d=0.0,q=0.0;//x1,x2,d,q,w,r;
printf("Please input the three coefficients:");

fflush(stdin);
scanf(" %f",&a);
scanf(" %f",&b);
scanf(" %f",&c);
printf("%fx*x+%fx+%f=0 \n",a,b,c);
d=sqrt(b*b-4*a*c);
if(b*b==4*a*c)
{
q=(-b)/2/a;
printf("x1=x2=q\n",b,a);
}
else if(b*b>4*a*c)
{
//printf("w=(-%f+%f)/2/%f,r=(-%d-%d)/2/%d\n",b,d,a,b,d,a);
printf("x1=%f \n x2=%f \n",(-b+sqrt(b*b-4*a*c))/(2*a),(-b-sqrt(b*b-4*a*c))/(2*a));
}
else
printf("There is no real root.\n");
}

你后边那个问题,其实如果近似解的话,可以照我上边写法,如果一定要是分数,7/8/2可以写