Instituto de Ciências Matemáticas de São Carlos
Departamento de Computação e Estatística
SCE182 - Algoritmos e Estruturas de Dados 1
Profs. Resp.: Graça Pimentel, Maria Cristina e Rosane Minghim

Exercícios - Uso de TAD e UNIT
Programa Principal - Agenda.pas

Programa para armazenar dados de candidatos que estão sendo entrevistados para uma vaga de garçom. Implementação sem o uso dos conceitos de TAD e UNIT.

Program PAgenda;
Uses
  CRT;

Const
  MAXREG = 100;

Type
  TReg = record
      RG       : string[15];
      Nome     : string[40];
      Endereco : string[60];
    end;

  TAgenda = record
      VReg : array [1..MAXREG] of TReg;
      NReg : 0..MAXREG;
    end;

Var
  Agenda : TAgenda;

Procedure InicializaAg;
begin
  Agenda.NReg := 0;
end;

Function PesquisaAg(RG : string) : integer;
Var
  i     : integer;
  achou : boolean;
begin
  achou := FALSE;
  i := 1;

  while (not achou) and (i <= Agenda.NReg) do
  begin
    achou := (Agenda.VReg[i].RG = RG);
    i := i + 1;
  end;

  if achou
   then PesquisaAg := (i-1)
   else PesquisaAg := -1;
end;

Procedure InsereAg;
Var
  flag   : char;
  RegAux : TReg;
begin
  flag := 's';

  while ((flag = 's') or (flag = 'S')) and (Agenda.NReg < MAXREG) do
  begin
    clrscr;
    writeln; writeln;
    write('Entre com o RG: ');
    readln(RegAux.RG);
    if (PesquisaAg(RegAux.RG) > 0)
     then begin
       write('Erro! Registro ja existe.');
       readln;
     end
     else begin
       write('Entre com o Nome: ');
       readln(RegAux.Nome);
       write('Entre com o Endereco: ');
       readln(RegAux.Endereco);

       Agenda.NReg := Agenda.NReg + 1;
       Agenda.VReg[Agenda.NReg] := RegAux;
     end;
    writeln; writeln;
    write('Deseja incluir outro registro (S/N)? ');
    readln(flag);
  end;

  if (flag = 's') or (flag = 'S')
   then begin
     write('Erro! Espaco para inclusao esgotado.');
     readln;
   end
end;

Procedure RemoveAg;
Var
  flag1, 
  flag2 : char;
  aux   : integer;
  RegAux : TReg;
begin
  flag1 := 's';

  while ((flag1 = 's') or (flag1 = 'S')) and (Agenda.NReg > 0) do
  begin
    clrscr;
    writeln; writeln;
    write('Entre com o RG: ');
    readln(RegAux.RG);
    aux := PesquisaAg(RegAux.RG);
    if (aux < 0)
     then begin
       write('Erro! Registro nao existe.');
       readln;

     end
     else begin
       writeln; writeln;
       writeln('Dados do registro a ser removido:');
       writeln('RG       : ', Agenda.VReg[aux].RG);
       writeln('Nome     : ', Agenda.VReg[aux].Nome);
       writeln('Endereco : ', Agenda.VReg[aux].Endereco);
       writeln; writeln;
       write('Confirma remocao (S/N)? ');
       readln(flag2);

       if (flag2 = 's') or (flag2 = 'S')
        then begin
          Agenda.VReg[aux] := Agenda.VReg[Agenda.NReg];
          Agenda.NReg := Agenda.NReg - 1;
        end
     end;
    writeln; writeln;
    write('Deseja remover outro registro (S/N)? ');
    readln(flag1);
  end;

  if (flag1 = 's') or (flag1 = 'S')
   then begin
     write('Agenda Vazia!');
     readln;
   end
end;

Procedure AlteraAg;
Var
  flag   : char;
  RegAux : TReg;
  aux    : integer;
begin
  if (Agenda.NReg < 1)
   then writeln('Agenda Vazia!')
   else begin
     flag := 's';

     while (flag = 's') or (flag = 'S') do
     begin
       clrscr;
       writeln; writeln;
       write('Entre com o RG: ');
       readln(RegAux.RG);
       aux := PesquisaAg(RegAux.RG);
       if (aux < 0)
        then begin
          write('Erro! Registro nao existe.');
          readln;
        end
        else begin
          writeln; writeln;
          writeln('Dados do registro a ser alterado:');
          writeln('RG       : ', Agenda.VReg[aux].RG);
          writeln('Nome     : ', Agenda.VReg[aux].Nome);
          writeln('Endereco : ', Agenda.VReg[aux].Endereco);
          writeln; writeln;

          writeln('Entre com novos dados. Tecle <<enter>> para manter os atuais.');
          write('Entre com o novo Nome: ');
          readln(RegAux.Nome);
          if RegAux.Nome = ''
           then RegAux.Nome := Agenda.VReg[aux].Nome;
          write('Entre com o novo Endereco: ');
          readln(RegAux.Endereco);
          if RegAux.Endereco = ''
           then RegAux.Endereco := Agenda.VReg[aux].Endereco;

          Agenda.VReg[aux] := RegAux;
        end;
       writeln; writeln;
       write('Deseja alterar outro registro (S/N)? ');
       readln(flag);
     end;
   end;
end;

Var
  flag : byte;
begin
  InicializaAg;

  flag := 1;
  while (flag <> 4) do 
  begin
    clrscr;
    writeln;writeln;
    writeln('Agenda de Enderecos');
    writeln('====== == =========');
    writeln;
    writeln('1 - Incluir');
    writeln('2 - Excluir');
    writeln('3 - Alterar');
    writeln('4 - Sair');
    writeln;
    write('Entre com a opcao desejada: ');
    readln(flag);

    case flag of
      1 : InsereAg;
      2 : RemoveAg;
      3 : AlteraAg;
    end;
  end;
end.