Experiment 3-1 Find the roots of a quadratic equation (20 points)

This topic requires a quadratic equation in one unknown ax 2 + bx + c = 0 ax^{2} + bx + c = 0ax2 +bx+c=the root of0, and the result retains 2 decimal places.

Input format:

The input gives 3 floating-point coefficients in one line, a、b、cseparated by spaces.

Output format:

According to the coefficient, different results are output:

  • 1) If the equation has two real roots that are not equal, then one root will be output per line, with the larger first and the smaller one;

  • 2) If the equation has two unequal complex roots, output one root in each line according to the format "real part + imaginary part i", first output the imaginary part as positive, and then output the imaginary part as negative;

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

  • 4) If the coefficients are all 0, then output " Zero Equation";

  • 5) If a和b为0,c不为0, then output " Not An Equation".

Input example 1:

2.1 8.9 3.5

Output example 1:

0.44
-3.80

Input example 2:

1 2 3

Output example 2:

1.00+1.41i
-1.00-1.41i

Input example 3:

0 2 4

Output sample 3:

2.00

Input example 4:

0 0 0

Output sample 4:

Zero Equation

Input example 5:

0 0 1

Output sample 5:

Not An Equation

Code:

# include <stdio.h>
# include <stdlib.h>
# include <math.h>

int main() {
    
    
    double a,b,c,value,value1,complex;
    scanf("%lf %lf %lf",&a,&b,&c);
    double de_ta = b * b - 4 * a * c;
    if (a == 0 && b == 0) {
    
    
        if (c == 0) printf("Zero Equation");
        else printf("Not An Equation");
    }else {
    
    
        if (de_ta == 0) {
    
    
            value = (-1 * b) / (2 * a);
            printf("%.2lf",value);
        }else if (de_ta > 0) {
    
    
            // 有特殊情况a = 0时,方程有唯一实数根
            if (a == 0) {
    
    
                value = (-1) * (c / b);
                printf("%.2lf",value);
            }else {
    
    
                value = (-1 * b - sqrt(de_ta)) / (2 * a);
                value1 = (-1 * b + sqrt(de_ta)) / (2 * a);
                printf("%.2lf\n%.2lf",value1,value);
            }
        }else {
    
    
            value1 = sqrt(-1 * de_ta) / (2 * a);
            value = (-1) * (b / (2 * a));
            // 有特殊情况,为纯虚数且前面输出要加上0.00
            if (b == 0) {
    
    
                printf("0.00+%.2lfi\n0.00%.2lfi",value1,-1*value1);
            }else {
    
    
                // 有两个复数根,先输出虚部是正的
                complex = (value1 > 0) ? value1:(-1)*value1;
                printf("%.2lf+%.2lfi\n%.2lf%.2lfi",value,complex,value,-1*complex);
            }
        }
    }
    return 0;
}

Submit screenshot:

Insert picture description here

Problem-solving instructions:

The situation to be considered for this question is really too great. You will know what it feels like by doing it yourself. Here are a few points to note:

  • When there is a unique real number root, it corresponds to two situations, one is that there are two same real number roots, and the other situation is a = 0so obtainedx = -c / b
  • When there are two complex roots, if b = 0the equation has two 纯虚根, then pay attention to adding before the output0.00
  • The normal complex root calculation equation is
    (x + b 2 a) 2 = b 2 − 4 ac 4 a 2 (x + \frac(b)(2a))^{2) = \frac(b^2-4ac }{4a^2}(x+2 ab)2=4 a2b24ac

Guess you like

Origin blog.csdn.net/weixin_43862765/article/details/114435862