我正在尝试解决这个问题:
(编写一个程序来计算二次方程的实根(ax 2 + bx + c = 0)。根可以使用以下公式计算:
x1 = (-b + sqrt(b 2 - 4ac))/2a
和
x2 = (-b - sqrt(b 2 - 4ac))/2a
我写了以下代码,但它不正确:
program week7_lab2_a1;
var a,b,c,i:integer;
x,x1,x2:real;
begin
write('Enter the value of a :');
readln(a);
write('Enter the value of b :');
readln(b);
write('Enter the value of c :');
readln(c);
if (sqr(b)-4*a*c)>=0 then
begin
if ((a>0) and (b>0)) then
begin
x1:=(-1*b+sqrt(sqr(b)-4*a*c))/2*a;
x2:=(-1*b-sqrt(sqr(b)-4*a*c))/2*a;
writeln('x1=',x1:0:2);
writeln('x2=',x2:0:2);
end
else
if ((a=0) and (b=0)) then
write('The is no solution')
else
if ((a=0) and (b<>0)) then
begin
x:=-1*c/b;
write('The only root :',x:0:2);
end;
end
else
if (sqr(b)-4*a*c)<0 then
write('The is no real root');
readln;
end.
你知道为什么吗?
并采取 a=-6,b=7,c=8 .. 你可以在编写伪代码后进行桌面检查吗?