输入abc的值求一元二次方程的解
程序代码:
/* 2017年6月30日16:14:49 功能:输入abc的值求一元二次方程的解 */ # include <stdio.h> # include <math.h> int main(void) { int a, b, c; double x1,x2; printf("请输入一元二次方程ax^2+bx+c=0中abc的值: "); printf("a = ");scanf("%d",&a); printf("b = ");scanf("%d",&b); printf("c = ");scanf("%d",&c); float jesse = (float)(b*b - 4*a*c); if (jesse>0) { x1 = 2*c/(-b+sqrt(jesse)); x2 = 2*c/(-b-sqrt(jesse)); printf("该方程有两个解: x1 = %f, x2 = %f ",x1, x2); } else if (jesse==0) { x1 = -b/(2*a); x2 = x1; printf("该方程有一个解:x1 = x2 = %f ",x1); } else { printf("该方程无解! "); } return 0; } /* 在VC++6.0中显示的结果: ———————————————————————————— 请输入一元二次方程ax^2+bx+c=0中abc的值: a = 1 b = 23 c = 3 该方程有两个解: x1 = -22.868817, x2 = -0.131183 ———————————————————————————— */