• C语言:
从键盘任意输入a,b,c的值,编程计算并输出一元二次方程ax2+bx+c=0的根。根据一元二次方程的求根公式,令
p=−b2a,q=∣∣b2−4ac∣∣√2a
当b2−4ac=0时,输出两个相等的实根x1=x2=p;当b2−4ac>0时,输出两个不相等的实根:x1=p+q,x2=p−q;当b2−4ac<0时,输出一对共轭复根:x1=p+qi,x2=p−qi。当a=0时,输出"It is not a quadratic equation!\n"。**输入格式要求:"%f,%f,%f" 提示信息:"Please enter the coefficients a,b,c:"
**输出格式要求:
相等实根: "x1 = x2 = %.2f\n"
不相等的实根: "x1 = %.2f, x2 = %.2f\n"
一对共轭复根: "x1 = %.2f+%.2fi, x2 = %.2f-%.2fi\n"
程序运行示例如下:
Please enter a,b,c: 0,10,2
It is not a quadratic equation!
  • 高数: alt

  • C语言题解:
#include <stdio.h>
#include <math.h>

int main(){
    float a, b, c, p, q, disc;
    printf("Please enter the coefficients a,b,c:");
    scanf("%f,%f,%f", &a, &b, &c);
    if(a == 0){
        printf("It is not a quadratic equation!\n");
        return 0;
    }
    disc = b * b - 4 * a * c;
    p = -b / (2 * a);
    q = sqrt(fabs(disc)) / (2 * a);
    if(disc == 0){
        printf("x1 = x2 = %.2f\n", p);
    }
    else if(disc > 0){
        printf ("x1 = %.2f, x2 = %.2f\n", p + q, p - q);
    }
    else{
        printf("x1 = %.2f+%.2fi, x2 = %.2f-%.2fi\n", p, q, p, q);
    }

  return 0;
}
  • 高数题解: alt