求一元二次方程的两个实数根
#include <stdio.h> #include <math.h>//包含 sqrt()函数,求平方根 int main() { double a,b,c,disc,x1,x2,p,q;//定义为double类型,满足计算精度要求 printf("Please enter a,b,c in order:\n");//提示用户输入 scanf("%lf%lf%lf",&a,&b,&c);//注意用 %lf disc=b*b-4*a*c; if(disc<0)//判断是否有根 printf("This question hasn't real roots\n"); else { p=-b/(2.0*a); q=sqrt(disc)/(2.0*a); x1=p+q; x2=p-q; printf("real root:\nx1=%7.2f\nx2=%7.2f\n",x1,x2); } return 0; }