楼主你好。
以下是我的代码:
#include
#include
int main(){
double a,b,c;
double x1,x2;
double deta;
while(1){
printf("Please enter a, b and c:");
scanf("%lf %lf %lf",&a, &b, &c);
printf("a=%。
2f, b=%。2f, c=%。2f
", a, b, c);
deta = b*b-4*a*c;
if(deta < 0){//若deta小于零,提示用户此方程无实数解
printf("deta is negtive! This quation doesn't have a real solution!
");
}else{
if(deta == 0){
x1=x2= -b/(2*a);
}else{
deta = sqrt(deta);
x1 = (-b deta)/(2*a);
x2 = (-b-deta)/(2*a);
}
printf("x1=%。
2f, x2 = %。2f
", x1, x2);
}
printf("==========================================
");
}
return 1;
}
输出:
Please enter a, b and c:1 -7 12
a=1。
00, b=-7。00, c=12。00
x1=4。00, x2 = 3。00
==========================================
Please enter a, b and c:2 -6 -20
a=2。
00, b=-6。00, c=-20。00
x1=5。00, x2 = -2。00
==========================================
Please enter a, b and c:1 4 3
a=1。
00, b=4。00, c=3。00
x1=-1。00, x2 = -3。00
==========================================
Please enter a, b and c:4 1 4
a=4。
00, b=1。00, c=4。
00
deta is negtive! This quation doesn't have a solution!
==========================================
Please enter a, b and c:。
全部