Advertisement
Guest User

OrdenaMatriz

a guest
Nov 16th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 3.11 KB | None | 0 0
  1. program OrganizaTbl;
  2. uses crt, SysUtils;
  3.  
  4. type
  5.   TOpcao = (CrescHoriz, CrescVert, DecHoriz, DecVert, Sair);
  6.  
  7. var opcao: TOpcao;
  8.     tabela: array of array of Integer;
  9.  
  10. procedure DesenhaTbl(Linhas, Colunas: Integer);
  11. var c, l:Integer;
  12. begin
  13.   WriteLn;
  14.   writeln('Desenhando tabela . . . ');
  15.   WriteLn;
  16.   for l:=0 to Linhas - 1 do
  17.   begin
  18.     for c:=0 to Colunas - 1 do  
  19.       write(format('[%4d ]', [tabela[l, c]]));
  20.     WriteLn;
  21.   end;
  22.   WriteLn;
  23.   WriteLn('Pressione enter para continuar . . .');
  24.   ReadLn;
  25. end;
  26.  
  27. procedure PreencheArray(Linhas, Colunas: Integer);
  28. var c, l: Integer;
  29. begin
  30.   SetLength(tabela, Linhas, Colunas);
  31.   Randomize();
  32.   for l:=0 to Linhas - 1 do
  33.     for c:=0 to Colunas - 1 do
  34.     begin
  35.       tabela[l, c]:= random(1000);
  36.     end;
  37. end;
  38.  
  39. procedure OrdCrescHoriz;
  40. var aux, l, c: Integer;
  41.     auxVetor: array of Integer;
  42. begin
  43.   WriteLn('Reordenando o array . . .');
  44.   aux:=-1;
  45.   setlength(auxVetor, ((high(tabela)+1)*(high(tabela[0])+1)));
  46.   for l:=0 to high(tabela) do
  47.     for c:=0 to high(tabela[0]) do
  48.     begin
  49.       inc(aux);
  50.       auxVetor[aux]:=tabela[l, c];
  51.     end;
  52.   for c:=0 to high(auxVetor) do
  53.     for l:=1 to high(auxVetor) - c do
  54.     begin
  55.       if auxVetor[l] < auxVetor[l-1] then
  56.       begin
  57.         aux := auxVetor[l-1];
  58.         auxVetor[l-1] := auxVetor[l];
  59.         auxVetor[l] := aux;
  60.       end;
  61.     end;
  62.   aux:=-1;
  63.   for l:=0 to high(tabela) do
  64.     for c:=0 to high(tabela[0]) do
  65.     begin
  66.       inc(aux);
  67.       tabela[l, c]:=auxVetor[aux];
  68.     end;
  69.  
  70.   l:=high(tabela);
  71.   c:=high(tabela[l]);
  72.   DesenhaTbl(l+1, c+1);
  73. end;
  74.  
  75. procedure MenuDefineTbl;
  76. var lnhs, cls:Integer;
  77. begin
  78.   while not (cls in [1..7]) do
  79.   begin
  80.     Write('De 1 à 7: quantas colunas terá a tabela? : ');
  81.     ReadLn(cls);
  82.   end;
  83.   while not (lnhs in [1..20]) do
  84.   begin
  85.     Write('De 1 à 20: quantas linhas terá a tabela? : ');
  86.     ReadLn(lnhs);
  87.   end;
  88.   PreencheArray(lnhs, cls);
  89.   DesenhaTbl(lnhs, cls);
  90. end;
  91.  
  92. function MenuOpcOrdem: TOpcao;
  93. var opc : Integer ;
  94. begin
  95.   while not (opc in [1..5]) do
  96.   begin
  97.     WriteLn;
  98.     WriteLn('Como você deseja organizar o array?');
  99.     WriteLn;
  100.     WriteLn('1: Para ordem crescente na horizontal.');
  101.     WriteLn('2: Para ordem crescente na vertical.');
  102.     WriteLn('3: Para ordem decrescente na horizontal.');
  103.     WriteLn('4: Para ordem decrescente na vertical.');
  104.     WriteLn('5: Para sair.');
  105.     ReadLn(opc);
  106.     case opc of
  107.       1: MenuOpcOrdem := CrescHoriz;
  108.       2: MenuOpcOrdem := CrescVert;
  109.       3: MenuOpcOrdem := DecHoriz;
  110.       4: MenuOpcOrdem := DecVert;
  111.       5: MenuOpcOrdem := Sair;
  112.     end;
  113.   end;
  114. end;
  115.  
  116. begin
  117.   opcao := CrescHoriz;
  118.   while not (opcao = Sair) do
  119.   begin
  120.     clrscr();
  121.     WriteLn('------ Organizar um array bidimensional ------');
  122.     WriteLn;
  123.     opcao := MenuOpcOrdem;
  124.     if opcao <> Sair then
  125.       MenuDefineTbl();
  126.     case opcao of
  127.       CrescHoriz: OrdCrescHoriz;
  128.       CrescVert: {fazalgo};;
  129.       DecHoriz: {fazalgo};;
  130.       DecVert: {fazalgo};;
  131.       Sair: WriteLn('Saindo, pressione enter . . .');
  132.     end;
  133.   end;
  134.   ReadLn;
  135. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement