program interroot(input, output);
uses crt;
label 57;
Const f12a=-0.56; f12b=-0.545;
      f13a=0.5; f13b=0.65;
      f23a=0; f23b=0.65;

      f4a = -2; f4b = 0;
  Type TF=function (x:real):real;
  Var S1,S2,S3,SF,x12,x13,x23:real; ing:boolean;
   {$F+}
   x:char;
{***************** zanosim zadannie funkcii *******************}
   function g1(x:real):real;
      begin
        g1:=exp(-x*x);
      end;

   function g2(x:real):real;
      begin
        g2:=sqrt(-x);
      end;

   function g3(x:real):real;
      begin
        g3:=x+0.1;
      end;

   function g1i(x:real):real;
      begin
        g1i:=-2*x*exp(-x*x);
      end;

   function g2i(x:real):real;
      begin
        g2i:=-0.5*sqrt(-x);
      end;

   function g3i(x:real):real;
      begin
        g3i:=1;
      end;

    function f4(x:real):real;
    begin
    f4:=x*x-1;
    end;

     function f4i(x:real):real;
    begin
    f4i:=2*x;
    end;

     function f5(x:real):real;
    begin
    f5:=0;
    end;
     function f5i(x:real):real;
    begin
    f5i:=0;
    end;

{******************* ROOT **********************************}


procedure Root_mix(H,dh,g,dg:TF;left,right,eps:real;var x:real);
function f(x:real):real;
begin
f:=h(x)-g(x);
end;
function df(x:real):real;
begin
df:=dh(x)-dg(x);
end;
begin
    repeat
    if (F(left)<0) and  ( F((left+right)*0.5) <=  (0.5*(F(left)+F(right))) )then
    begin
            left:=(left*F(right)-right*F(left))/(F(right)-F(left));
            right:=right-F(right)/dF(right);
            end
    else
    begin
            right:=(left*F(right)-right*F(left))/(F(right)-F(left));
            left:=left-F(left)/dF(left);
            end
    until abs(right-left)<eps;
    writeln('left = ', left:10:7, 'right = ' , right:10:7);
    writeln('eps = ', eps:10:8);
    x:=(right+left)*0.5;
end;

procedure root(f,g,f1,g1 : func; a,b: real; var x:real);
      var t: boolean; d,eps2: real;
        function h(x: real): real;
          Begin
           h:=f(x)-g(x)
          end;
       function h1(x: real): real;
         Begin
          h1:=f1(x)-g1(x)
         end;
        function kas(d: real): real;
           Begin
            kas:=d-h(d)/h1(d)
          end;
        function hord: real;
         var p,q: real;
          Begin
            p:=h(b); q:=h(a);
            hord:=(a*p-b*q)/(p-q)
          end;
       function xor(x1,x2: real): boolean;
         Begin
           if abs(x1-x2)<=eps2 then xor:=false else xor:=true;
         end;
     Begin
         d:=(a+b)/2;
         eps2:=2*eps;
         if h1(d)*(h(d)-(h(a)+h(b)/2))>0 then t:=true else t:=false;
          while xor(a,b) do
            begin
               if t then begin b:=kas(b); a:=hord end
               else begin a:=kas(a); b:=hord end
            end;
       x:=a
       end;
{**************INTEGRAL**********************************}


function trap(f:tf;a,b:real):real;
var  sn,s2n,h,eps,p:real;
     j,n:integer;


  function FPart(k:integer):real;
  var summ:real;
      i:integer;
  begin
    h:=(b-a)/k;
    summ:=0;
    for i:=0 to k do
      if (i<>k) and (i<>0)  then begin
        summ:=summ + f(a + (i * h))*h;
        end
      else
        summ:=summ + 0.5 * f(a + (i * h)) * h;
    FPart:=summ;
  end;

begin
  p:=20;
  n:=100;
  j:=2;
  eps:=0.00000001;
  sn:=FPart(n);
  s2n:=FPart(2*n);
  while ( abs(s2n-sn) > p*eps ) do
  begin
    j:=j*2;
    sn:=s2n;
    s2n:=FPart(j*n);
  end;
  trap:=s2n
end;

{*************MAIN************************************}

Begin
57:
begin
clrscr;
writeln(' functions:');
writeln(' 1) F1=exp(-x*x)');
writeln(' 2) F2=sqrt(-x)');
writeln(' 3) F3=x+0.1');
writeln;

{root_mix (f4,f4i,f5,f5i,f4a,f4b,0.0000001,x12);
writeln ('>>>>>', x12);   }
  root_mix      (g1,g1i,g2,g2i,f12a,f12b,0.00000001,x12);
  writeln ('  plots crossing point 1 and 2=',x12:10:7);
  {root_mix    (g2,g2i,g3,g3i,f23a,f23b,0.00000001,x23);
  writeln ('  plots crossing point 2 and 3=',x23:10:7);  }
  root_mix    (g3,g3i,g1,g1i,f13a,f13b,0.00000001,x13);
  writeln ('  plots crossing point 1 and 3=',x13:10:7);

   S1:=trap (g1,x12,x13);
   S2:=trap (g3,x23,x13);
   S3:=trap (g2,x12,x23);

writeln(s1:10:7, ' ', s2:10:7, ' ', s3:10:7);
SF:=S1-S2-S3;
writeln;
  writeln ('square =  ',SF:10:7);
  readln;
  end;
end.