Advertisement
mariosimao

trabalhoAc

Dec 15th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 4.06 KB | None | 0 0
  1. {======================================================================================}
  2. {
  3.           Autor:            Mario Günter Simão
  4.  
  5. Nome do arquivo:            trabalhoAc.pas
  6.  Tamanho do TAB:            4
  7.  
  8.            Data:            14.12.2016
  9.     Atualização:          14.12.2016
  10.  
  11.        Objetivo:            Aprimoramento dos procedimentos envolvendo árvore
  12.                             binária,  impedindo de adicionar  na árvore itens
  13.                             já existentes e  impressão dos itens usando pilha,
  14.                             sem usar recursividade.
  15.  
  16.      Observação:          Não usar o vi/vim para ler o código fonte, devido
  17.                             aos problemas que apresenta em comentários longos
  18.                             de uma só linha.
  19. }
  20. {======================================================================================}
  21.  
  22. program trabalhoAc;
  23.  
  24. uses crt;
  25.  
  26. type
  27.     noAgenda = ^regAgenda;
  28.    
  29.     regAgenda = record
  30.         nome: string[30];
  31.         telefone: string[16];
  32.         esq, dir: noAgenda;
  33.     end;
  34.  
  35. {======================================================================================}
  36. // Verifica se o contato já existe na árvore binária
  37. // Se existe, retorna true
  38. // Se não existe, retorna false
  39.  
  40. function contatoExiste (var atual: noAgenda; novo: noAgenda): boolean;
  41.  
  42. begin
  43.  
  44.     if atual = nil then atual:= novo
  45.     else
  46.     begin
  47.    
  48.         if novo^.nome <> atul^.nome then contatoExiste(atual^.dir,novo)
  49.         else contatoExiste(atual^.esq,novo);
  50.    
  51.     end;
  52.  
  53. end;
  54. procedimento buscarLugar(var atual: noAgenda; novo: noagenda)
  55. (* buscar o lugar na árvores descendo até um nó livre;
  56. ATENÇÃO: o primeiro parâmetro é por referência, o que é necessário para o novo nó ser ligado à árvore *)
  57. inicio
  58. se atual = nil então
  59.     atual ← novo
  60.     senão se novo^.nome > atual^.nome então
  61.                     buscarLugar(atual^.dir,novo)
  62.                     senão buscarLugar(atual^.esq,novo)
  63. fim
  64.  
  65. {======================================================================================}
  66. // Inclui novo contato na árvore binária
  67. // Se o contato já existe: não inclui + mensagem
  68.  
  69. procedure procIncluir(var raiz,atual: noAgenda);
  70.  
  71. var
  72.     novo: noAgenda;
  73.     nome: string[30];
  74.     telefone: string[16];
  75.  
  76. begin
  77.  
  78.     new(novo);
  79.     atual:= raiz;
  80.  
  81.     write('Nome: ');
  82.     readln(nome);                                   // FAZER TRATAMENTO
  83.     write('Telefone: ');
  84.     readln(telefone);                               // FAZER TRATAMENTO
  85.  
  86.     novo^.nome:= nome;
  87.     novo^.telefone:= telefone;
  88.  
  89.     novo^.esq:= nil;
  90.     novo^.dir:= nil;
  91.  
  92.     if (contatoExiste(raiz,novo) = true) then
  93.     begin
  94.    
  95.         if raiz = nil then raiz:= novo
  96.         else buscarLugar(raiz,novo);
  97.  
  98.         atual:= novo;  
  99.    
  100.     end
  101.     else
  102.     begin
  103.    
  104.         writeln('Nome já existe.');
  105.  
  106.  
  107.     end;
  108.  
  109.  
  110. end;
  111.  
  112. {======================================================================================}
  113. {                         P R O G R A M A    P R I N C I P A L                         }
  114. {======================================================================================}
  115.  
  116. var
  117.     opcao: integer;
  118.     raiz, atual, novo, aux: noAgenda;
  119.  
  120. begin
  121.  
  122.     raiz:= nil;
  123.  
  124.     repeat
  125.     begin
  126.  
  127.         clrscr;
  128.         writeln;
  129.         writeln('________________________________________________');
  130.         writeln('|                                              |');
  131.         writeln('|                  M  E  N  U                  |');
  132.         writeln('|______________________________________________|');
  133.         writeln('|   |                                          |');
  134.         writeln('| 1 | Adicionar Contato                        |');
  135.         writeln('| 2 | Opção 02                                 |');
  136.         writeln('| 3 | Opção 03                                 |');
  137.         writeln('| 4 | Opção 04                                 |');
  138.         writeln('| 5 | Opção 05                                 |');
  139.         writeln('|   |                                          |');
  140.         writeln('| 0 | Sair                                     |');
  141.         writeln('|___|__________________________________________|');
  142.         writeln;
  143.         write('Opção: ');
  144.         readln(opcao);
  145.  
  146.         case opcao of
  147.  
  148.             1:
  149.             begin
  150.  
  151.             end;
  152.  
  153.             2:
  154.             begin
  155.    
  156.             end;
  157.  
  158.             3:
  159.             begin
  160.  
  161.             end;
  162.  
  163.             4:
  164.             begin
  165.  
  166.             end;
  167.  
  168.             5:
  169.             begin
  170.  
  171.             end;
  172.  
  173.             0:
  174.             begin
  175.                 clrscr;
  176.             end;
  177.  
  178.         end;
  179.  
  180.     end;
  181.     until opcao = 0;
  182.  
  183. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement