打印二元一次方程的根。通过对a和判别式的比较,输出二元一次方程的根。
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define EXP 0.000000001 //宏定义EXP;
//判断:一个浮点数与0判断时候需要判断它们是否介于EXP与-EXP之间就行;
int main()
{
double a = 0.0, b = 0.0, c = 0.0;
double disc = 0.0;
double p1 = 0.0, p2 = 0.0;
printf("please enter a,b,c\n");
scanf("%lf%lf%lf", &a, &b, &c);
disc = b*b - 4 * a*c; //此处定义判别式disc;
if ((a >= -EXP) && (a <= EXP))
{
printf("不是一元二次方程\n");
}
else
{
if (disc == 0)
{
p1 = -b / (2 * a);
printf("x1=x2=%lf", p1);
}
else if (b*b - 4 * a*c > 0)
{
p1 = (-b + sqrt(disc)) / (2 * a);
p2 = (-b - sqrt(disc)) / (2 * a);
printf("x1=%lf,x2=%lf", p1, p2);
}
else
{
printf("方程有两个复根\n&