Achieve the solution of a quadratic equation (cycle), a quadratic equation cycle

Source: Internet
Author: User

Achieve the solution of a quadratic equation (cycle), a quadratic equation cycle

// Equation. cpp: Defines the entry point for the console application.
//


# Include <stdio. h>
# Include <stdlib. h>
# Include <math. h>
# Include <float. h> // defines the infinitely small constant 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;
Float B;
Float c;
Char xuanze;

Float r1;
Float r2;
Float i1;
Float i2;

For (;;)
{
Printf ("do you want to calculate a quadratic equation? 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", & 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 selected", 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 = ){
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/;
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/;
I1 = (float) sqrt (-delta );
I2 =-(float) sqrt (-delta );
S = 4;
}
}
Return s;
}




------ This program is used by the user to determine whether to perform a cycle of quadratic equation calculation. The program structure is simple, the scanf method encountered a problem during the write process. The scanf method correctly obtained user input in the first loop, but the scanf method obtained an error in the second loop. After learning about the scanf () method, it is found that the content received by scanf each time is the input + enter key. Therefore, you can use flushll (); To clear the cache.
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.