#include #include double x1,x2,d; double f1(double a,double b) { x1=(-b+sqrt(d))/(2*a); x2=(-b-sqrt(d))/(2*a); } double f2(double a,double b) { x1=-b/(2*a); x2=-b/(2*a); } int main { double a,b,c; d=b*b-4*a*c; scanf("%f%f%f",&a,&b,&c); if(d>0) { f1(a,b); printf("x1=%0.2f,x2=%0.2f",x1,x2); } if(d==0) { f2(a,b); printf("x1=%0.2f,x2=%0.2f",x1,x2); } }
错误在代码中标出来了,如下。如有帮助,请采纳一下,谢谢。
#include <stdio.h>
#include <math.h>
double x1,x2,d;
double f1(double a,double b)
{
x1=(-b+sqrt(d))/(2*a);
x2=(-b-sqrt(d))/(2*a);
//error 1:函数声明的是double,必须有返回值,或者把函数声明为void
return 0;
}
double f2(double a,double b)
{
x1=-b/(2*a);
x2=-b/(2*a);
//error 2:函数声明的是double,必须有返回值,或者把函数声明为void
return 0;
}
int main()
{
double a,b,c;
//d=b*b-4*a*c; //error 3:a b c还没有初始化,这里会出错,把它移动到scanf下面
scanf("%f%f%f",&a,&b,&c);
d=b*b-4*a*c;
if(d>0)
{
f1(a,b);
printf("x1=%0.2f,x2=%0.2f",x1,x2);
}
if(d==0)
{
f2(a,b);
printf("x1=%0.2f,x2=%0.2f",x1,x2);
}
return 0; //error 4:这里需要返回
}
题主 你把你带你用代码段发出来,以便更好为你解答
修改一下,错误比较多,应该是算ax^2+bx+c=0方程解吧
#include <math.h>
double x1, x2, d;
void f1(double a, double b)
{
x1 = (-b + sqrt(d)) / (2 * a);
x2 = (-b - sqrt(d)) / (2 * a);
}
void f2(double a, double b)
{
x1 = -b / (2 * a);
x2 = -b / (2 * a);
}
int main()
{
double a, b, c;
scanf_s("%lf %lf %lf", &a, &b, &c);
d = b*b - 4 * a*c;
if (d>0)
{
f1(a, b);
printf("x1=%0.2f,x2=%0.2f", x1, x2);
}
if (d == 0)
{
f2(a, b);
printf("x1=%0.2f,x2=%0.2f", x1, x2);
}
return 0;
}
#include <stdio.h>
#include <math.h>
#define m 0.000001
int main()
{
float a,b,c,x,x2,n,q,p;
scanf ("%f%f%f",&a,&b,&c);
n=b*b-4*a*c;
if ((a<m)&&(a>-m))
{
x=(-c)/b;
printf("%f",x);
}
else if ((n<m)&&(n>-m))
{
x=(-b)/(2*a);
printf("%f",x);
}
else if (n>0)
{
x=((-b)+sqrt(n))/(2*a);
x2=((-b)-sqrt(n))/(2*a);
printf("%f\n%f",x,x2);
}
else
{
q=(-b)/(2*a);
p=sqrt(-n)/(2*a);
printf("%f+%fi\n",q,p);
printf("%f-%fi\n",q,p);
}
}
您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632