program e_1 (input,output);
uses crt;
type TF = function(x:real):real;
var x1,x2,x3,I1,I2,I,i11,i21,I12,I22,k1,k2,eps1,eps,p:real;
    n0,k,l,o,numf:longint;
    m:boolean;
{----------------------------------------------------------------------------}
{$F+}
{1}{Function F1(x:real):real;
         begin
         f1:=exp(ln(2)*x)+1
         end;
Function F11(x:real):real;
         begin
         f11:=exp(ln(2)*x)*ln(2)
         end;
Function F2(x:real):real;
         begin
         f2:=sqr(sqr(x))*x
         end;
Function F21(x:real):real;
         begin
         f21:=5*sqr(sqr(x))
         end;
Function F3(x:real):real;
         begin
         f3:=(1-x)/3;
         end;
Function F31(x:real):real;
         begin
         f31:=-1/3
         end;}
{----------------------------------------------------------------------------}
{2}{Function F1(x:real):real;
         begin
         f1:=5/x
         end;
Function F11(x:real):real;
         begin
         f11:=-10/sqr(x)
         end;
Function F2(x:real):real;
         begin
         f2:=2.5*x-9.5
         end;
Function F21(x:real):real;
         begin
         f21:=2.5
         end;
Function F3(x:real):real;
         begin
         f3:=1.5/(1+x)+3;
         end;
Function F31(x:real):real;
         begin
         f31:=-1.5/sqr(x+1)
         end;]
{----------------------------------------------------------------------------}
{3}{Function F1(x:real):real;
         begin
         f1:=exp(-x)+3
         end;
Function F11(x:real):real;
         begin
         f11:=-exp(-x)
         end;
Function F2(x:real):real;
         begin
         f2:=2*x-2
         end;
Function F21(x:real):real;
         begin
         f21:=2
         end;
Function F3(x:real):real;
         begin
         f3:=1/x
         end;
Function F31(x:real):real;
         begin
         f31:=-1/sqr(x)
         end;}
{----------------------------------------------------------------------------}
{4}{Function F1(x:real):real;
         begin
         f1:=exp(x)+2
         end;
Function F11(x:real):real;
         begin
         f11:=exp(x)
         end;
Function F2(x:real):real;
         begin
         f2:=-1/x
         end;
Function F21(x:real):real;
         begin
         f21:=1/sqr(x)
         end;
Function F3(x:real):real;
         begin
         f3:=-2*(x+1)/3
         end;
Function F31(x:real):real;
         begin
         f31:=-2/3
         end;}
{----------------------------------------------------------------------------}
{5}{Function F1(x:real):real;
         begin
         f1:=0.35*sqr(x)-0.95*x+2.7
         end;
Function F11(x:real):real;
         begin
         f11:=0.7*x-0.95
         end;
Function F2(x:real):real;
         begin
         f2:=exp(ln(3)*x)+1
         end;
Function F21(x:real):real;
         begin
         f21:=exp(ln(3)*x)*ln(3)
         end;
Function F3(x:real):real;
         begin
         f3:=1/(x+2)
         end;
Function F31(x:real):real;
         begin
         f31:=-1/sqr(x+2)
         end;}
{----------------------------------------------------------------------------}
{6}{Function F1(x:real):real;
         begin
         f1:=0.6*x+3
         end;
Function F11(x:real):real;
         begin
         f11:=0.6
         end;
Function F2(x:real):real;
         begin
         f2:=(x-2)*sqr(x-2)-1
         end;
Function F21(x:real):real;
         begin
         f21:=3*sqr(x-2)
         end;
Function F3(x:real):real;
         begin
         f3:=3/x
         end;
Function F31(x:real):real;
         begin
         f31:=-3/sqr(x)
         end;}
{----------------------------------------------------------------------------}
{7}{Function F1(x:real):real;
         begin
         f1:=1/(2-x)+6
         end;
Function F11(x:real):real;
         begin
         f11:=1/sqr(2-x)
         end;
Function F2(x:real):real;
         begin
         f2:=-2*x+14
         end;
Function F21(x:real):real;
         begin
         f21:=-2
         end;
Function F3(x:real):real;
         begin
         f3:=ln(x)
         end;
Function F31(x:real):real;
         begin
         f31:=1/x
         end;}
{----------------------------------------------------------------------------}
{8}{Function F1(x:real):real;
         begin
         f1:=exp(x)+2
         end;
Function F11(x:real):real;
         begin
         f11:=exp(x)
         end;
Function F2(x:real):real;
         begin
         f2:=-2*x+8
         end;
Function F21(x:real):real;
         begin
         f21:=-2
         end;
Function F3(x:real):real;
         begin
         f3:=-5/x
         end;
Function F31(x:real):real;
         begin
         f31:=5/sqr(x)
         end;}
{----------------------------------------------------------------------------}
{9}Function F1(x:real):real;
         begin
         f1:=3/((x-1)*(x-1)+1)
         end;
Function F11(x:real):real;
         begin
         f11:=-6*(x-1)/sqr((x-1)*(x-1)+1)
         end;
Function F2(x:real):real;
         begin
         f2:=sqrt(x+0.5)
         end;
Function F21(x:real):real;
         begin
         f21:=0.5/sqrt(x+0.5)
         end;
Function F3(x:real):real;
         begin
         f3:=exp(-x)
         end;
Function F31(x:real):real;
         begin
         f31:=-exp(-x)
         end;
{----------------------------------------------------------------------------}
{10}{Function F1(x:real):real;
         begin
         f1:=1+4/(sqr(x)+1)
         end;
Function F11(x:real):real;
         begin
         f11:=-8*x/sqr(1+sqr(x))
         end;
Function F2(x:real):real;
         begin
         f2:=sqr(x)*x
         end;
Function F21(x:real):real;
         begin
         f21:=3*sqr(x)
         end;
Function F3(x:real):real;
         begin
         f3:=exp(ln(2)*(-x))
         end;
Function F31(x:real):real;
         begin
         f31:=-exp(ln(3)*x)*ln(3)
         end;}
{----------------------------------------------------------------------------}
procedure root(f,f1,g,g1:Tf; a,b,eps1:real; var x:real);
var x11,x12,e1,e,l,d:real;
    begin
    repeat
    l:=(f1(a)-g1(a))*((f((A+b)/2)-g((a+b)/2)-(f(b)-g(b)-F(a)+G(b))/2));
    if l<0 then begin d:=b; e:=a end else begin D:=a; e:=b end;
    x11:=d-(f(d)-g(d))/(f1(d)-g1(d));
    x12:=e-(d-e)*(f(E)-g(e))/(f(d)-g(d)-f(E)+g(e));
    e1:=(x11+x12)/2;
    if l<0
       then begin a:=x12;b:=x11 end
       else begin a:=x11;b:=x12 end;
    until abs(e1-x11)<eps1;
    x:=e1
    end;
{----------------------------------------------------------------------------}
Function ft(f,g:TF; a,b:real; var p:real; var n0:longint):real;
var x,IntN,h:real;
         begin
         IntN:=p;
         h:=(b-a)/n0;
         x:=a+h;
         while x<b do
               begin
               IntN:=IntN+f(x)-g(x);
               x:=x+2*h
               end;
         p:=IntN;
         ft:=IntN*h;
         n0:=2*n0
         end;
{----------------------------------------------------------------------------}
function integral(a,b,eps:real; f,g:Tf; n0:LONGint):real;
    var IntN,Int2N,h,p:real;
        o:longint;
    begin
    o:=1;
    p:=(f(b)+f(a)-g(a)-g(b))/2;
    Int2N:=ft(f, g, a, b, p, n0);
    repeat
          intN:=Int2N;
          Int2N:=ft(f, g, a, b, p, n0);
          o:=O+1
    until abs(Int2N-IntN)<(3*eps);
    if m then writeln('   with the number of iteration o=',o:3,' pogreshnost not larger, than ',eps:1:k);
    integral:=IntN
    end;
{----------------------------------------------------------------------------}
begin clrscr;
repeat
       writeln ('Program has been made in order to find points of intersection of functions:');
       {write ('Input the number of homework functions (from 1 to 10) numf:=');
       readln (numf);}
       numf:=9;
       case numf of
            1:writeln ('        F1=2^x+1,              F2=x^5,          F3=(1-x)/3      ');
            2:writeln ('        F1=3(0.5/(x+1)+1),     F2=2.5x-9.5,     F3=1.5/(1+x)+3  ');
            3:writeln ('        F1=exp(-x)+3,          F2=2*x-2,        F3=1/x          ');
            4:writeln ('        F1=exp(x)+2,           F2=-1/x,         F3=-2(x+1)/3    ');
            5:writeln ('        F1=0.35x^2-0.95x+2.7,  F2=3^x+1,        F3=1/(x+2)      ');
            6:writeln ('        F1=0.6x+3,             F2=(x-2)^3-1,    F3=3/x          ');
            7:writeln ('        F1=ln(x),              F2=-2x+14,       F3=1/(2-x)+6    ');
            8:writeln ('        F1=exp(x)+2,           F2=-2x+8,        F3=-5/x         ');
            9:writeln ('        F1=3/((x-1)^2+1),      F2=sqrt(x+0.5),  F3=e^(-x)       ');
           10:writeln ('        F1=1+4/(x^2+1),        F2=x^3,          F3=2^(-x)       ')
       end;

       writeln ('and to count the area of the figure limited with these three functions');
      readln;
      eps:=0.001;
      m:=false;
{----------------------------------------------------------------------------}
       writeln('The counting of points of intersection of functions f1 and f3...');

      case numf of
           1:begin k1:=-2.65; k2:=-2.35 end;
           2:begin k1:= 1.25; k2:= 1.45 end;
           3:begin k1:= 0.25; k2:= 0.28 end;
           4:begin k1:=-4.1 ; k2:=-4    end;
           5:begin k1:=-1.85; k2:=-1.75 end;
           6:begin k1:= 0.8 ; k2:= 0.9  end;
           7:begin k1:= 2.15; k2:= 2.25 end;
           8:begin k1:=-2.45; k2:=-2.35 end;
           9:begin k1:=-0.35; k2:=-0.15 end;
          10:begin k1:=-1.35; k2:=-1.25 end
      end;

      root(f1, f11, f3, f31, k1, k2, eps, x1);
       Writeln('        x1=',x1:5:3,' - point of intersection of f1 i f3, counted with eps=',eps:5:3);
       writeln('The counting is finished!');
      writeln;
{----------------------------------------------------------------------------}
       writeln('The counting of points of intersection of functions f2 and f3...');

     case numf of
          1:begin k1:= 0.55; k2:= 0.75 end;
          2:begin k1:= 4.15; k2:= 4.35 end;
          3:begin k1:= 1.3 ; k2:= 1.4  end;
          4:begin k1:=-1.9 ; k2:=-1.8  end;
          5:begin k1:=-1.3 ; k2:=-1.2  end;
          6:begin k1:= 3.2 ; k2:= 3.3  end;
          7:begin k1:= 4.15; k2:=4.25  end;
          8:begin k1:=-0.6 ; k2:=-0.5  end;
          9:begin k1:= 0.15; k2:=0.35  end;
         10:begin k1:= 0.75; k2:=0.85  end
      end;

      root(f2, f21, f3, f31, k1, k2, eps, x2);
       Writeln('        x2= ',x2:5:3,' - point of intersection of f2 i f3, counted with eps=',eps:5:3);
       writeln('The counting is finished!');
      writeln;
{----------------------------------------------------------------------------}
       writeln('The counting of points of intersection of functions f1 and f2...');

     case numf of
          1:begin k1:= 1.25; k2:= 1.35 end;
          2:begin k1:= 5.05; k2:= 5.15 end;
          3:begin k1:= 2.5 ; k2:= 2.6  end;
          4:begin k1:=-0.45; k2:=-0.35 end;
          5:begin k1:= 0.25; k2:= 0.35 end;
          6:begin k1:= 3.75; k2:= 3.85 end;
          7:begin k1:= 6.05; k2:= 6.15 end;
          8:begin k1:= 1.2 ; k2:= 1.3  end;
          9:begin k1:= 1.85; k2:= 2.25 end;
         10:begin k1:= 1.3 ; k2:= 1.4  end
     end;

      root(f1, f11, f2, f21, k1, k2, eps, x3);
       Writeln('        x3= ',x3:5:3,' - point of intersection of f2 i f1, counted with eps=',eps:5:3);
       writeln('The counting is finished!');
      readln;
{----------------------------------------------------------------------------}
       writeln('The counting of eps1...');
       {write ('Input the first number of segnementation n0:= ');
      readln (n0);}
      n0:=2;
      k:=3;
      I11:=abs(integral(x1+0.001, x2+0.001, eps, f1, f3, n0));
      I21:=abs(integral(x2+0.001, x3+0.001, eps, f1, f2, n0));
      I1:=I11+I21;
      I12:=abs(integral(x1-0.001, x2-0.001, eps, f1, f3, n0));
      I22:=abs(integral(x2-0.001, x3-0.001, eps, f1, f2, n0));
      I2:=I12+I22;
      eps1:=abs((i2-i1)/2);
      Writeln('        Eps1:=',eps1);
      k:=0;
      while eps1<1 do
            begin
            k:=k+1;
            eps1:=eps1*10
            end;
      eps1:=round(eps1);
      for
      l:=1 to k do eps1:=eps1/10;
       Writeln('        round of Eps1 in larger side:=',eps1:1:k);
       writeln('The counting is finished!');
      writeln;
{----------------------------------------------------------------------------}
      m:=true;
       writeln('The counting of I1...');
      I1:=abs(integral(x1, x2, eps1, f1, f3, n0));
       writeln('        I1= ',I1:1:k);
       writeln('The counting is finished!');
      writeln;
{----------------------------------------------------------------------------}
       writeln('The counting of I2...');
      I2:=abs(integral(x2, x3, eps1, f1, f2, n0));
       writeln('        I2= ',I2:1:k);
       writeln('The counting is finished!');
      writeln;
{----------------------------------------------------------------------------}
      I:=I1+I2;
       Writeln('The area of figure S=I1+I2=',I:1:k,' limited of f1,f2,f3,');
       Writeln('counted with eps=',eps1:1:k);
      readln;
{----------------------------------------------------------------------------}
       writeln('Do you want to continue computations???');
       writeln('even number and 0 to quit, not even - to continue');
      readln(n0)
until not odd(n0) or (n0=0);
writeln ('Thank you for using my programm!');
readln
end.