1028:
求方程 的根,用三个函数分别求当b^2-4ac大于0、等于0、和小于0时的根,并输出结果。从主函数输入a、b、c的值。
#include<stdio.h>
#include<math.h>
void twovalue(double a, double b, double c)
{
double value1 = (-b + sqrt(bb - 4ac))/(2a);
double value2 = (-b - sqrt(bb - 4ac))/(2a);
printf("x1=%.3lf x2=%.3lf\n", value1, value2);
}
void onevalue(double a, double b, double c)
{
double value = (-b)/(2*a);
printf(“x1=%.3lf x2=%.3lf\n”, value, value);
}
void floatvalue(double a, double b, double c)
{
double real = -b/(2a);
double fake = sqrt(4ac - bb) / (2*a);
printf("x1=%.3lf+%.3lfi x2=%.3lf%.3lfi", real, fake, real, -fake);
}
int main()
{
double a, b, c;
scanf("%lf %lf %lf", &a, &b, &c);
if (b*b - 4*a*c > 0)
{
twovalue(a, b, c);
}
else if (b*b - 4*a*c == 0)
{
onevalue(a, b, c);
}
else
{
floatvalue(a, b, c);
}
return 0;
}