// equation.cpp : Defines the entry point for the console application.
//
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <float.h> // 定义了浮点型的无穷小常量 const float FLT_EPSILON=1.192092896e-07F;
/*
* The function to caculate a Real coefficient eqution's(ax^2+bx+c=0) root.
* IN: a,b,c ---- the three real coefficient,
* OUT: r1, r2 ---- the two real roots or the real part of the complex roots.
* i1, i2 ---- the image part of the complex roots.
* RET: the status of the equation.
*/
int equation1(float a, float b, float c, float* r1, float* r2, float* i1, float* i2);
int equation2(float a, float b, float c, float& r1, float& r2, float& i1, float& i2);
int main(int argc, char* argv[])
{
float a;
float b;
float c;
char xuanze;
float r1;
float r2;
float i1;
float i2;
for(;;)
{
printf("是否进行一元二次方程计算?Y/N\n");
scanf("%c",&xuanze);
if(xuanze=='Y')
{
printf("Please input a,b,c of the equation ax^2+bx+c=0 [a b c Enter]: ");
scanf("%f %f %f", &a, &b, &c);
switch(equation2(a,b,c, r1, r2, i1, i2))
{
case 0:
printf("The equation %gx^2+%gx+%g=0 has no roots.\n", a, b, c);
break;
case 1:
printf("The equation %gx^2+%gx+%g=0 has one real root:\n \tx=%g.\n", a, b, c, r1);
break;
case 2:
printf("The equation %gx^2+%gx+%g=0 has two real roots:\n \tx1=%g, \tx2=%g.\n", a, b, c, r1, r2);
break;
case 3:
printf("The equation %gx^2+%gx+%g=0 has an arbitrary solution.\n", a, b, c);
break;
case 4:
printf("The equation %gx^2+%gx+%g=0 has a pair of conjugate complex roots:\n\tx1=%g+%gi, \tx2=%g-%gi.\n", a, b, c, r1, i1, r1, i1);
break;
default:
break;
}
_flushall();
}
else
{
return 0;
}
}
}
// printf("选择的是%c\n",xuanze);
/*
* The function to caculate a Real coefficient eqution's(ax^2+bx+c=0) root.
* IN: a,b,c ---- the three real coefficient,
* OUT: r1, r2 ---- the two real roots or the real part of the