Experiment 3-1 find a quadratic equation of the root (20 points) "C language programming guide and exercises experiment (3rd edition)"

The title requires quadratic root mono-, result 2 decimal places.

Input formats:

3 are given the input floating-point coefficients in a row a, b, c, separated by a space.

Output formats:

The coefficients, the output different results:

1) If the equation has two real roots are not equal, then each output line of a root, the first big small;

2) If the equation has two complex roots are not equal, then each line in the format "real imaginary + i" outputs a root, the output of the first imaginary part is positive, the output of the imaginary part is negative;

3) If equation has only one root, the root directly output;

4) If the coefficient is 0, then output "Zero Equation";

5) If a and b is 0, c is not 0, then output "Not An Equation".

Sample Input 1:

2.1 8.9 3.5

Output Sample 1:

-0.44
-3.80

Sample Input 2:

1 2 3

Output Sample 2:

-1.00+1.41i
-1.00-1.41i

Sample Input 3:

0 2 4

Sample Output 3:

-2.00

Input Sample 4:

0 0 0

Output Sample 4:

Zero Equation

Input Sample 5:

0 0 1

Output Sample 5:

Not An Equation
By Roots formula, but do not pay attention to pure imaginary root a minus sign in front of 0.
Code:
#include <stdio.h>
#include <math.h>
int main() {
    double a,b,c;
    scanf("%lf%lf%lf",&a,&b,&c);
    if(a == 0 && b == 0 && c == 0) printf("Zero Equation\n");
    else if(a == 0 && b == 0 && c != 0) printf("Not An Equation\n");
    else if(a == 0) printf("%.2f\n",-c / b);
    else {
        double det = b * b - 4 * a * c,aa = a * 2;
        if(det == 0) printf("%.2f\n",-b / aa);
        else if(det > 0) printf("%.2f\n%.2f\n",(-b + sqrt(det)) / aa,(-b - sqrt(det)) / aa);
        else printf("%.2f+%.2fi\n%.2f-%.2fi\n",(b == 0 ? b : -b) / aa,sqrt(-det) / aa,(b == 0 ? b : -b) / aa,sqrt(-det) / aa);
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/8023spz/p/11872310.html