2025年02月26日 如何编写一个C程序来找到二次方程的根 极客笔记
应用软件开发方法解决任意问题在C语言中实现。
输入 - a、b、c的值
输出 - r1、r2的值
r_{1}=\frac{-b+\sqrt{b^2-4ac}}{2a}
r_{2}=\frac{-b-\sqrt{b^2-4ac}}{2a}

# include<stdio.h>
# include<math.h>
int main () {
    float a,b,c,r1,r2,d;
    printf ("Enter the values of a b c: ");
    scanf (" %f %f %f", &a, &b, &c);
    d= b*b - 4*a*c;
    if (d>0) {
        r1 = -b+sqrt (d) / (2*a);
        r2 = -b-sqrt (d) / (2*a);
        printf ("The real roots = %f %f", r1, r2);
    }
    else if (d==0) {
        r1 = -b/(2*a);
        r2 = -b/(2*a);
        printf ("Roots are equal =%f %f", r1, r2);
    }
    else
        printf("Roots are imaginary");
    return 0;
}  
Case 1:  
Enter the values of a b c: 1 4 3  
The real roots = -3.000000 -5.000000  
Case 2:  
Enter the values of a b c: 1 2 1  
Roots are equal =-1.000000 -1.000000  
Case 3:  
Enter the values of a b c: 1 1 4  
Roots are imaginary
                                        本文链接:http://so.lmcjl.com/news/23918/