program obecna_odmocnina;
uses crt;
var cyklus:integer;
    kroku:longint;
    volba:char;
    AnoNe:boolean;
    JoNe:boolean;
    AnoNeRadek:integer;
    cislo:longint;
    odmocnina:integer;

    moc:integer;
    odhad:extended;
    TEMPor:extended;
    deriv,nederiv:extended;

    null,a,b,c,d,e,f,g,h,i,j:extended;

{
    pozice:byte;
    cislopotomek:byte;
    hodnota:char;
    hodn:char;
    tmp:byte;
    i:byte;
    odloz:byte;
    vstup:byte;
    kroku,krokucelkem:longint;
}
procedure logoprogramu;
          begin
          TextBackground(0);
          TextColor(15);
          clrscr;
          TextColor(3);
          write('--------------------------------------------------------------------------------');
          TextColor(15);
          writeln(' Obecna odmocnina -->> ZK <<-- zk.zk@post.cz ---- SPSSE a VOS Liberec 17.7.2000');
          TextColor(3);
          writeln('--------------------------------------------------------------------------------');
          TextColor(7);
          writeln('');
          end;

procedure VlozOdmocnina;
          begin
          AnoNe:=false;
          repeat
          clrscr;
          logoprogramu;
          writeln;
          textcolor(7);
          write(' Kolikatou ');
          textcolor(15);
          write('odmocninu');
          textcolor(7);
          write(' chcete pocitat (2 az +99): ');
          read(odmocnina);

          if (odmocnina > 1) and (odmocnina < 100) then
             begin
             AnoNe:=true;
             end
            else
             begin
             AnoNe:=false;
             clrscr;
             logoprogramu;
             writeln;
             textcolor(12);
             write(' --- Prekrocen rozsah (2 az +99) "');
             textcolor(14);
             write(odmocnina);
             textcolor(12);
             write('" !!!');
             textcolor(7);
             Sound(220);
             Delay(200);
             NoSound;
             delay(3000);
             end;
          until AnoNe=true;
          end;

procedure VlozCislo;
          begin
          AnoNe:=false;
          repeat
          clrscr;
          logoprogramu;
          writeln;
          textcolor(7);
          write(' Z jakeho ');
          textcolor(15);
          write('cisla');
          textcolor(7);
          write(' chcete pocitat (2 az +2`000`000`000): ');
          read(cislo);

          if (cislo > 1) and (cislo < 2000000001) then
             begin
             AnoNe:=true;
             end
            else
             begin
             AnoNe:=false;
             clrscr;
             logoprogramu;
             writeln;
             textcolor(12);
             write(' --- Prekrocen rozsah (2 az +2`000`000`000) "');
             textcolor(14);
             write(cislo);
             textcolor(12);
             write('" !!!');
             textcolor(7);
             Sound(220);
             Delay(200);
             NoSound;
             delay(3000);
             end;
          until AnoNe=true;
          end;

procedure UkazPriklad;
          begin
          textcolor(13);
          gotoxy(5,8);writeln('Zadany priklad zni:');
          textcolor(3);
          gotoxy(8,10);writeln('  ____________');
          gotoxy(8,11);writeln('\/');

          textcolor(9);
          if odmocnina < 10 then
             begin
             gotoxy(8,10);writeln(odmocnina);
             end
            else
             begin
             gotoxy(7,10);writeln(odmocnina);
             end;
          textcolor(10);
          gotoxy(11,11);writeln(cislo);

          gotoxy(1,24);
          textcolor(7);
          end;

function Newton(TEMPor:extended):extended;
         begin
         {
         -------------Nederiv
         }
         if odmocnina > 1 then
            begin
            odhad:=TEMPor;
            for moc:=2 to odmocnina do
                begin
                odhad:=(odhad*TEMPor);
                end;
            nederiv:=(odhad-cislo);
            end;
            {
            -------------Derivace
            }
            if odmocnina = 2 then
               begin
               odhad:=TEMPor;
               deriv:=(odmocnina*TEMPor);
               end;

            if odmocnina > 2 then
            begin
            odhad:=TEMPor;
            for moc:=3 to odmocnina do
                begin
                odhad:=(odhad*TEMPor);
                end;
            deriv:=(odmocnina*odhad);
            end;
            {
            -------------Newtonova metoda
            }
            odhad:=TEMPor;
            odhad:=(odhad-(((nederiv)/(deriv))));
            Newton:=odhad;
            end;

procedure testideal;
          begin
          textcolor(28);
          write('                 -- !!! Nalezen koren pri ');
          textcolor(14);
          write(kroku-5);
          write('.');
          textcolor(28);
          writeln(' kroku !!! --');
          textcolor(7);
          end;

function testidealok:boolean;
         begin
         testidealok:=false;
         if (f=g) and (f=h) and (f=i) and (f=j) then
            begin
            testidealok:=true;
            end;
         end;

procedure UkazNewton;
          begin
          textcolor(2);
             gotoxy(33,7);write(a:10:18);
             gotoxy(33,8);write(b:10:18);
             gotoxy(33,9);write(c:10:18);
             gotoxy(33,10);write(d:10:18);
             gotoxy(33,11);write(e:10:18);
          textcolor(6);
             gotoxy(33,12);write(f:10:18);
          textcolor(2);
             gotoxy(33,13);write(g:10:18);
             gotoxy(33,14);write(h:10:18);
             gotoxy(33,15);write(i:10:18);
             gotoxy(33,16);write(j:10:18);
          gotoxy(1,24);
          textcolor(7);
          end;

procedure help;
          begin
          clrscr;
          logoprogramu;
          writeln('');
          writeln('  Vzorecek:     1/y                                   y       ');
          writeln(' ^^^^^^^^^^    X    = A                              A  - X   ');
          writeln('                                          x  = x  - ----------');
          writeln('               y                           1    0         y-1 ');
          writeln('              A - X = 0                              y * A    ');
          writeln('                                                      ');
          writeln('            1/2 * A = x                               ');
          writeln('                       0                              ');
          Delay(5000);
          end;

begin
repeat
JoNe:=false;
VlozOdmocnina;
VlozCislo;
clrscr;
LogoProgramu;
kroku:=10;
null:=(cislo/2*2/2);
a:=newton(null);
b:=newton(a);
c:=newton(b);
d:=newton(c);
e:=newton(d);
f:=newton(e);
g:=newton(f);
h:=newton(g);
i:=newton(h);
j:=newton(i);

if (i=h) and (i=g) and (i=f) and (i=e) then
   begin
   JoNe:=true;
   AnoNeRadek:=5;
   end;

if (h=g) and (h=f) and (h=e) and (h=d) then
   begin
   JoNe:=true;
   AnoNeRadek:=4;
   end;

if (g=f) and (g=e) and (g=d) and (g=c) then
   begin
   JoNe:=true;
   AnoNeRadek:=3;
   end;

if (f=e) and (f=d) and (f=c) and (f=b) then
   begin
   JoNe:=true;
   AnoNeRadek:=2;
   end;

if (e=d) and (e=c) and (e=b) and (e=a) then
   begin
   JoNe:=true;
   AnoNeRadek:=1;
   end;

 repeat
  repeat
  clrscr;
  logoprogramu;
  gotoxy(1,4);
  write('--------------------------------------------------------------------------------');
  writeln('                              |');
  writeln('                              |');
  writeln('                              |');
  writeln('                              |');
  writeln('                              |');
  writeln('                              |');
  writeln('                              |');
  writeln('                              |');
  writeln('                              |');
  writeln('                              |');
  writeln('                              |');
  writeln('                              |');
  writeln('                              |');
  writeln('                              |');
  write('--------------------------------------------------------------------------------');
  writeln(' Zadejte pocet kroku:     N - do stisku klavesy           1 -  1 kroku');
  writeln('                          Z - znovu                       2 - 10 kroku');
  writeln('    --- Help ---          K - konec                       3 - 50 kroku');
  writeln('        ^');
  UkazPriklad;

  if testidealok=true then
     begin
     testideal;
     UkazNewton;
     end
   else
     begin
     textcolor(7);
     write(' Jiz provedeno kroku: ');
     textcolor(2);
     writeln(kroku,'.');
     textcolor(7);
     UkazNewton;
     end;
  if JoNe=true then
     begin
     JoNe:=false;
     UkazNewton;
     textcolor(28);
     write('                 -- !!! Nalezen koren pri ');
     textcolor(14);
     write(AnoNeRadek);
     write('.');
     textcolor(28);
     write(' kroku !!! --');
     textcolor(7);
     end;

  textcolor(7);
  writeln;
  write(' Vase volba: ');
  volba:=readkey;
  until (volba='k')or(volba='z')or(volba='n')or(volba='1')or(volba='2')or(volba='3')or(volba='h');

if volba='h' then
   begin
   help;
   volba:=' ';
   textcolor(7);
   end;

 if volba='n' then
   begin
   repeat
   kroku:=kroku+1;
   a:=b;
   b:=c;
   c:=d;
   d:=e;
   e:=f;
   f:=g;
   g:=h;
   h:=i;
   i:=j;
   j:=newton(j);;
   UkazNewton;
   textcolor(2);
   gotoxy(23,24);write(kroku,'.');
   until (keypressed) or (testidealok=true);
   volba:=' ';
   textcolor(7);
   end;

 if volba='1' then
   begin
   cyklus:=0;
   repeat
   cyklus:=cyklus+1;
   kroku:=kroku+1;
   a:=b;
   b:=c;
   c:=d;
   d:=e;
   e:=f;
   f:=g;
   g:=h;
   h:=i;
   i:=j;
   j:=newton(j);;
   UkazNewton;
   textcolor(2);
   gotoxy(23,24);write(kroku,'.');
   volba:=' ';
   until (cyklus=1) or (testidealok=true);
   textcolor(7);
   end;

 if volba='2' then
   begin
   cyklus:=0;
   repeat
   cyklus:=cyklus+1;
   kroku:=kroku+1;
   a:=b;
   b:=c;
   c:=d;
   d:=e;
   e:=f;
   f:=g;
   g:=h;
   h:=i;
   i:=j;
   j:=newton(j);;
   UkazNewton;
   textcolor(2);
   gotoxy(23,24);write(kroku,'.');
   volba:=' ';
   until (cyklus=10) or (testidealok=true);
   textcolor(7);
   end;

 if volba='3' then
   begin
   cyklus:=0;
   repeat
   cyklus:=cyklus+1;
   kroku:=kroku+1;
   a:=b;
   b:=c;
   c:=d;
   d:=e;
   e:=f;
   f:=g;
   g:=h;
   h:=i;
   i:=j;
   j:=newton(j);;
   UkazNewton;
   textcolor(2);
   gotoxy(23,24);write(kroku,'.');
   volba:=' ';
   until (cyklus=50) or (testidealok=true);
   textcolor(7);
   end;

 until (volba='k') or (volba='z');
until volba='k';

{
---------------------------------------------------------------------
---------------------------------------------------------------------
}
{writeln;
writeln;
writeln(a);
writeln(b);
writeln(c);
writeln(d);
writeln(e);
writeln(f);
writeln(g);
writeln(h);
writeln(i);
writeln(j);

textcolor(15);
writeln;
writeln;
writeln;
writeln(' ----- Konec -----');
}
clrscr;
logoprogramu;
textcolor(7);
end.

