// 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/Nn");
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:ntx1=%g+%gi, tx2=%g-%gi.n", a, b, c, r1, i1, r1, i1);
break;
default:
break;
}
_flushall();
}
else
{
return 0;
}
}
}
// printf("选择的是%cn",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 complex roots.
* i1, i2 ---- the image part of the complex roots.
* RET: the status of the equation.
* 0 ---- no solution.
* 1 ---- one real root.
* 2 ---- two real root.
* 3 ---- has an arbitrary solution.
* 4 ---- has a pair of conjugate complex roots.
*/
int eqution1(float a, float b, float c, float* r1, float* r2, float* image)
{
return 0;
}
/*
* 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.
* 0 ---- no solution.
* 1 ---- one real root.
* 2 ---- two real root.
* 3 ---- has an arbitrary solution.
* 4 ---- has a pair of conjugate complex roots.
*/
int equation2(float a, float b, float c, float& r1, float& r2, float& i1, float& i2)
{
int s;
double delta;
if (0==a){
if(0==b){
if(0==c){
s= 3;
}
else{
s=0;
}
}
else{
r1=-c/b;
s=1;
}
}
else
{
delta=b*b-4.0f*a*c;
if(delta>=-FLT_EPSILON && delta<=FLT_EPSILON) // delta==0
{
r1=r2=-b/2.0f/a;
s=1;
}
else if(delta>FLT_EPSILON){ // delta>0
r1=-b/2.0f/a+(float)sqrt(delta);
r2=-b/2.0f/a-(float)sqrt(delta);
s=2;
}
else{ // delta<0
r1=r2=-b/2.0f/a;
i1=(float)sqrt(-delta);
i2=-(float)sqrt(-delta);
s=4;
}
}
return s;
}
- 还没有人评论,欢迎说说您的想法!