可以請哪位高手幫我寫這個程式嘛@@??

(No: 0, 2, 4, 6, 8) (20 points) Write a program that reads the coefficients, a, b, and c of the equations ax2 + bx + c = 0 and calculates the roots of the equation. You need to consider the situation when there is no solution of real numbers.

Hint: Use the sqrt and fabs function.

A sample run is shown as below:

Enter the coefficents of the equation (a, b, c): 1 -3 2

x = 2.0 and x = 1.0

Enter the coefficents of the equation (a, b, c): 0 1 2

x = -2

Enter the coefficents of the equation (a, b, c): 1 2 3

There is no solution.

1 個解答

評分
  • 1 0 年前
    最佳解答

    #include <stdio.h>

    #include <stdlib.h>

    #include <math.h>

    main()

    { float a,b,c,d;

    printf("Enter the coefficents of the equation (a, b, c): ");

    scanf("%f%f%f",&a,&b,&c);

    d=b*b-4*a*c; /* 判斷式 */

    if(a==0)

    printf("x=%0.1f\n",-1*c); /* 若為一次方程式 */

    else

    { if(d>=0)

    { if(d==0) /* 重根 */

    printf("x=%0.1f\n",(-b+sqrt(d))/(2*a));

    else /* 相異實根 */

    printf("x=%0.1f and x=%.01f\n",(-b+sqrt(d))/(2*a),(-b-sqrt(d))/(2*a));

    }

    else /* 無實根 */

    printf("There is no solution\n");

    }

    system("pause");

    }

    參考資料: 有問題再問吧 ^ ^
還有問題?馬上發問,尋求解答。