program giaiptbac3;
uses crt;
{----------------chuong-trinh-con--------------------------Ư
function arccos(k:real):real;
var alpha:real;
begin
ifk>0 then alpha:=arctan(sqrt(1/k/k-1))
else if k<0 then alpha:=arctan(-sqrt(1/k/k-1))
else alpha:=pi/2;
arccos:=alpha;
end;
{--------------chuong-trinh-con-thu-2-----------------------}
function can(k:real):real;
var ex:real;
begin
if k<0 then ex:=-exp(ln(abs(k))/3)
else if k>0 then ex:=exp(ln(k)/3)
else ex:=0;
can:=ex;
end;
{----------chuong-trinh-con-thu-ba---------------------}
procedure nghiemphuc(a,b,c:real);
var d:real;
x,y:real;
begin
d:=-b*b+4*a*c;
x:=-b/2/a;
y:=sqrt(d)/2/a;
writeln(' Nghiem Phuc: x2 = ',x,' + ',y,'i');
writeln(' x3 = ',x,' - ',y,'i');
end;
{-------------chuong-trinh-chinh-----------------------}
uses crt;
var a,b,c,d:real;
s,k:real;
x1,x2,x3:real;
begin
clrscr;
write('Nhap a,b,c,d = ');readln(a,b,c,d);
s:=b*b-3*a*c;
writeln(s);
if s<>0 then
begin
k:=(9*a*b*c-2*b*b*b-27*a*a*d)/2/sqrt(abs(s*s*s));
writeln(k);
if s>0 then
begin
if abs(k)<=1 then
begin
writeln('PT co 3 nghiem thuc: ');
x1:=(2*sqrt(s)*cos(arccos(k)/3)-b)/(3*a);
x2:=(2*sqrt(s)*cos(arccos(k)/3-2*pi/3)-b)/(3*a);
x3:=(2*sqrt(s)*cos(arccos(k)/3+2*pi/3)-b)/(3*a);
writeln('x1 = ',x1:10:12);
writeln('x2 = ',x2:10:12);
writeln('x3 = ',x3:10:12);
end
else begin
x1:=sqrt(abs(s))*abs(k)/3/a/k*(can(abs(k)+sqrt(k*k-1))+can(abs(k)-sqrt(k*k-1)))-b/3/a;
writeln(' Nghiem thuc: x1 = ',x1:10:12);
a:=a;
b:=x1*a+b;
c:=x1*b+c;
nghiemphuc(a,b,c);
end;
end
else begin
x1:=sqrt(abs(s))/(3*a)*(can(k+sqrt(k*k+1))+can(k-sqrt(k*k+1)))-b/3/a;
writeln(' Nghiem thuc: x1 = ',x1:10:12);
a:=a;b:=x1*a+b;c:=x1*b+c;
nghiemphuc(a,b,c);
end;
end
else begin
x1:=(-b+can(b*b*b-27*a*a*d))/3/a;
writeln(' Nghiem thuc: x = ',x1:10:12);
end;
readln;
end.