解法2:
program ling(input,output);
var a,b,c,d,x,y,z:real;
begin
writeln('This is a program to calculate the quadrie equation with one unknow.');
writeln('From:ax*x+bx+c=0');
writeln;
write('Please input data far a,b,c(for example:1 2 3):');
read(a,b,c);
if (a=0)and(b=0)and(c=0) then
writeln('the calculate result is:x can be all real numbers');
if (a=0)and(b=0)and(c<>0) then
writeln('the calculate result is:this equation has no answer');
if (a=0)and(b<>0)and(c<>0) then
begin
x:=-c/b;
y:=-c/b;
writeln('x1=x2=',x:8:1);
end
else
if (a<>0) then
begin
z:=sqr(b)-4*a*c;
if (z<0) then
writeln('thecalculate result is:this equation has no answer');
if (z>=0) then
begin
x:=(-b+sqrt(z))/(2*a);
y:=(-b-sqrt(z))/(2*a);
writeln('x1=',x:8:2);
writeln('x2=',y:8:2);
end
end
end.
结果验证:
运行时,输入:
0 0 0 提示:x is real
0 0 1 提示:no result
0 1 2 提示:x1=x2=-2.00
1 2 9 提示:no result
1 9 3 提示:x1=-0.35
x2=-8.65
1 2 1 提示:x1=-1.00
x2=-1.00
4 4 1 提示:x1=-0.5
x2=-0.5
4 4 -1 提示:x1=0.21
x2=-1.21
------------------------------------------
举一反三,融会贯通