Advertisement
sapitando

Ordenador de números em Pascal(Array).

Jul 29th, 2016
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 5.66 KB | None | 0 0
  1. { Autor : Tiago Portela
  2.   Sobre o programa : Ele ordena os numeros buscando o ultimo menor e colocando ele em primeiro,
  3.                      e deslocando todos a frente e pulando ele mesmo depois. O limite de 38 é
  4.                      por causa da interface, mas ele ordenaria indefinidamente.
  5.                      Compatível com Turbo Pascal e FreePascal.
  6.                      Lazarus não aceitou minha manipulação de string "NroStr[0]".
  7.   Obs : Apenas tentando aprender algoritimos, sozinho, por hobby. }
  8. {$G+}
  9. {$N+}
  10. program ordenador_de_numeros;
  11. uses crt;
  12.  
  13. const MaxSizeList = 38;
  14.  
  15.  
  16. var Nro : array [1..MaxSizeList] of extended;
  17.     NroAux1, NroAux2 : extended;
  18.     PosArray, PosLastSmaller, CounterPos, TotalPos, LastPos, FirstPos : word;
  19.     NroStr : string;
  20.     Code : integer;
  21.  
  22.  
  23.  
  24. begin
  25.  clrscr;
  26.  textcolor(white);
  27.  write('     Programa '); textcolor(red); write('ORDENADOR DE NUMEROS');
  28.  textcolor(white); write('. Voce pode digitar ate ');
  29.  textcolor(yellow); write(MaxSizeList) ; textcolor(white); writeln(' numeros.');
  30.  writeln;
  31.  textcolor(white);
  32.  writeln(' Digite o numero : ');
  33.  gotoxy(1,25); write(' Digite '); textcolor(yellow); write('Esc'); textcolor(white); write(' para sair.');
  34.  write(' Digite '); textcolor(yellow); write('Espaco'); textcolor(white); write(' para ordenar.');
  35.  gotoxy(21,3);
  36.  TotalPos := 0;
  37.  CounterPos := 1;
  38.  repeat
  39.   NroStr[CounterPos] := readkey;
  40.   case NroStr[CounterPos] of
  41.  
  42.    #0       : readkey;
  43.  
  44.    #8       : if CounterPos > 1
  45.               then begin
  46.                     write(#8#32#8);
  47.                     dec(CounterPos);
  48.                     dec(NroStr[0]);
  49.                    end;
  50.  
  51.    #13      : if (CounterPos > 1) and (TotalPos < MaxSizeList) and
  52.               (not ((pos(#46,NroStr) = 1) and (CounterPos = 2)))
  53.               then begin
  54.                     inc(TotalPos);
  55.                     val(NroStr,Nro[TotalPos],Code);
  56.                     window(2,5,39,23);
  57.                     if TotalPos > 19
  58.                     then gotoxy(18, TotalPos - 19)
  59.                     else gotoxy(1, TotalPos);
  60.                     write(Nro[TotalPos] : 15 : 2);
  61.                     window(1,1,80,25);
  62.                     gotoxy(21,3);
  63.                     clreol;
  64.                     CounterPos := 1;
  65.                     NroStr := '';
  66.                    end;
  67.  
  68.    #32      : if TotalPos > 1
  69.                  then begin
  70.                        FirstPos := 1;
  71.                        LastPos := TotalPos;
  72.                        for PosArray := FirstPos to (LastPos - 1) do
  73.                         begin
  74.                          NroAux1 := Nro[PosArray];
  75.                          PosLastSmaller := PosArray;
  76.                          for CounterPos := (PosArray + 1) to LastPos do
  77.                           if NroAux1 >= Nro[CounterPos]
  78.                           then begin;
  79.                                 PosLastSmaller := CounterPos;
  80.                                 NroAux1 := Nro[PosLastSmaller];
  81.                                end;
  82.                          if PosLastSmaller > PosArray
  83.                          then for CounterPos := PosArray to LastPos do
  84.                                begin
  85.                                 if (CounterPos >= PosLastSmaller) and (CounterPos < LastPos)
  86.                                 then NroAux2 := Nro[CounterPos + 1]
  87.                                 else NroAux2 := Nro[CounterPos];
  88.                                 Nro[CounterPos] := NroAux1;
  89.                                 NroAux1 := NroAux2;
  90.                                end;
  91.                         end;
  92.                        window(42,5,79,23);
  93.                        textcolor(magenta);
  94.                        for CounterPos := 1 to TotalPos do
  95.                         begin
  96.                          if CounterPos > 19
  97.                          then gotoxy(18, CounterPos - 19)
  98.                          else gotoxy(1, CounterPos);
  99.                          write(Nro[CounterPos] : 15 : 2);
  100.                         end;
  101.                        window(1,1,80,25);
  102.                        textcolor(white);
  103.                        gotoxy(52,25);
  104.                        write('Digite '); textcolor(red); write('Enter');
  105.                        textcolor(white); write(' para zerar.');
  106.                        gotoxy(21,3);
  107.                        clreol;
  108.                        repeat
  109.                         NroStr[1] := readkey;
  110.                        until NroStr[1] in [#13, #27];
  111.                        gotoxy(52,25);
  112.                        clreol;
  113.                        window(1,5,80,23);
  114.                        clrscr;
  115.                        window(1,1,80,25);
  116.                        gotoxy(21,3);
  117.                        TotalPos := 0;
  118.                        CounterPos := 1;
  119.                        NroStr := '';
  120.                       end;
  121.  
  122.    #46, #44 : if (CounterPos <= 13) and (pos(#46,NroStr) = 0) and (TotalPos < MaxSizeList)
  123.               then begin
  124.                     NroStr[CounterPos] := #46;
  125.                     write(NroStr[CounterPos]);
  126.                     inc(CounterPos);
  127.                     inc(NroStr[0]);
  128.                    end;
  129.  
  130.    #48..#57 : if ((CounterPos <= 12) and (TotalPos < MaxSizeList)
  131.               and ((CounterPos < pos(#46,NroStr) + 3) or (pos(#46,NroStr) = 0)))
  132.               or (((pos(#46,NroStr) = 13) or (pos(#46,NroStr) = 12)
  133.               or (pos(#46,NroStr) = 11)) and (CounterPos < pos(#46,NroStr) + 3))
  134.               then begin
  135.                     write(NroStr[CounterPos]);
  136.                     inc(CounterPos);
  137.                     inc(NroStr[0]);
  138.                    end;
  139.  
  140.   end;
  141.  until NroStr[CounterPos] in [#27];
  142.  textcolor(lightgray);
  143.  clrscr;
  144. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement