Давай сначала решим аналитически:
Значит, количество корней зависит от двух условий - что дискриминант больше, меньше или равен нулю и что все подкоренное выражение б, м или р нулю.
] D - дискриминант; t1,t2 - корни
Тогда условия на корни выглядят так:
4 корня: D > 0 and t1 > 0 and t2 > 0
3 корня: D > 0 and (t1 > 0 and t2 = 0 or t1 = 0 and t2 > 0)
2 корня: D = 0 and t1 > 0
1 корень: D = 0 and t1 = 0
0 корней: D = 0 and t1 < 0 or D < 0 or D > 0 and t1 < 0 and t2 < 0
Код: Выделить всё
procedure solve(a,b,c: real; var count: integer; var roots: array of integer);
var
D, sD: real;
t1, st1: real;
t2, st2: real;
begin
D:= b*b - 4 * a * c;
if D > 0 then
begin
sD:= sqrt(D)
t1:= 0.5 * (-b + sD) / a;
t2:= 0.5 * (-b - sD) / a;
if t1 > 0 and t2 > 0 then
begin
count:= 4;
st1:= sqrt(t1);
st2:= sqrt(t2);
roots[1]:= st1;
roots[2]:= -st1;
roots[3]:= st2;
roots[4]:= -st2;
end else
if t1 > 0 and t2 = 0 then
begin
count:= 3;
st1:= sqrt(t1);
roots[1]:= st1;
roots[2]:= -st1;
roots[3]:= 0;
end else
if t1 = 0 and t2 > 0 then
count:= 3;
st2:= sqrt(t2);
roots[1]:= 0;
roots[2]:= st2;
roots[3]:= -st2;
end else count:= 0;
end else
if D=0 then
begin
t1:= -0.5 * b / a;
if t1 > 0 then
begin
count:= 2;
st1:= sqrt(t1);
roots[1]:= st1;
roots[2]:= -st1;
end else
if t1 = 0 then
begin
count:= 1;
roots[1]:= 0;
end else
count:= 0;
end else
count:= 0;
end;
Не уверен, что будет работать, но я старался
писал прямо здесь, в форуме
поэтому не компилировал