C語言一元二次方程簡單求解
阿新 • • 發佈:2019-01-03
#include<stdio.h>
#include<math.h>
void gen1(double a,double b,double c)
{
double x1,x2;
x1=(-b+sqrt(b*b-4*a*c))/(2*a);
x2=(-b-sqrt(b*b-4*a*c))/(2*a);
printf("函式的解為x1=%.2f,x2=%.2f",x1,x2);
}
void gen2(double a,double b,double c)
{
double x;
x=-b/(2*a);
printf("函式的解為x=%.2f",x);
}
void gen3()
{
printf("無解");
}
int main()
{
double a,b,c,m;
printf("請輸入一元二次不等式的係數a,b,c的值\n");
scanf("%lf%lf%lf",&a,&b,&c);
m=b*b-4*a*c;
if(m>0)
gen1(a,b,c);
else if(m==0)
gen2(a,b,c);
else
gen3();
return 0;
}