246 lines
5.6 KiB
ObjectPascal
246 lines
5.6 KiB
ObjectPascal
program cpfs;
|
|
|
|
type
|
|
TCPF = ^ptcpf;
|
|
ptcpf = record
|
|
cpf : string;
|
|
prox : TCPF;
|
|
end;
|
|
THash = array[0..99] of ^ptcpf;
|
|
|
|
var
|
|
cpf:string;
|
|
bool:boolean;
|
|
hash:thash;
|
|
op : integer ;
|
|
cont : boolean ;
|
|
|
|
|
|
|
|
function charInt(var letra:char):integer;
|
|
var tmp:integer;
|
|
begin
|
|
tmp:=Ord(letra)- Ord('0');
|
|
charInt:=tmp; //ord('0')=48... ord('1')=49... dai -48 vira 0...
|
|
end;
|
|
|
|
function validacaoDeInexistencialismoDeOutroCadastroDePessoasFisicaDentroDaLista( var cpf:string;var cpfs:thash ): boolean;
|
|
var
|
|
i:integer;
|
|
aux:^ptcpf;
|
|
existenciaDeTalInformacaoNaLista:boolean;
|
|
begin
|
|
aux := cpfs[(charint(cpf[10]) *10+ charint(cpf[11]))];
|
|
existenciaDeTalInformacaoNaLista := false;
|
|
while (aux^.prox <> nil) and (not existenciaDeTalInformacaoNaLista) do
|
|
begin
|
|
if (cpf = aux^.cpf) then
|
|
existenciaDeTalInformacaoNaLista := true
|
|
else
|
|
aux:= aux^.prox;
|
|
end;
|
|
validacaoDeInexistencialismoDeOutroCadastroDePessoasFisicaDentroDaLista := existenciaDeTalInformacaoNaLista;
|
|
end;
|
|
|
|
|
|
function validarCPF(var cpf:string):boolean;
|
|
var
|
|
i, soma, dig1, dig2:integer;
|
|
begin
|
|
soma:=0;
|
|
for i:=1 to 9 do
|
|
// *11-i, vai decrementando, *9, *8, *7...
|
|
soma:=soma+( charInt(cpf[i]) ) * (11-i);
|
|
|
|
dig1 := 11 - (soma mod 11);
|
|
if (dig1 = 10) or (dig1 = 11) then
|
|
dig1 := 0;
|
|
|
|
|
|
soma := 0;
|
|
for i := 1 to 10 do
|
|
soma := soma + (Ord(cpf[i])-48)*(12-i);
|
|
|
|
dig2 := 11 - (soma mod 11);
|
|
if (dig2 = 10) or (dig2 = 11) then
|
|
dig2 := 0;
|
|
|
|
ValidarCPF := (dig1=charInt( cpf[10] )) and (dig2 = charInt( cpf[11] ));
|
|
end;
|
|
|
|
|
|
procedure inserirCPF(var cpfs:THash; var cpf:string);
|
|
var
|
|
i, doisDigitos:integer;
|
|
novo, atual, ant:TCPF;
|
|
begin
|
|
if validarCPF(cpf) then
|
|
begin
|
|
doisDigitos:=( charInt(cpf[10])*10 ) + charInt(cpf[11]); //*10 pq né, vira dezena + decimal 👍
|
|
if (not validacaoDeInexistencialismoDeOutroCadastroDePessoasFisicaDentroDaLista(cpf, cpfs)) then
|
|
begin
|
|
new(novo);
|
|
if novo = nil then
|
|
writeln('Memoria cheia')
|
|
else
|
|
begin
|
|
novo^.cpf := cpf;
|
|
novo^.prox := nil;
|
|
atual:= cpfs[doisDigitos];
|
|
ant:=nil;
|
|
while (atual <> nil) and (atual^.cpf < cpf) do
|
|
begin
|
|
ant := atual;
|
|
atual := atual^.prox;
|
|
end;
|
|
if ant = nil then
|
|
begin
|
|
novo^.prox := cpfs[doisDigitos];
|
|
cpfs[doisDigitos] := novo;
|
|
end
|
|
else
|
|
begin
|
|
novo^.prox := atual;
|
|
ant^.prox := novo;
|
|
end;
|
|
end;
|
|
end ;
|
|
|
|
end
|
|
else
|
|
writeln('CPF INVALIDO');
|
|
end;
|
|
|
|
procedure escrever(var cpfs:thash);
|
|
var
|
|
i:integer;
|
|
aux:^ptcpf;
|
|
begin
|
|
for i:= 0 to 99 do
|
|
begin
|
|
writeln('Digito final', i);
|
|
aux := cpfs[i];
|
|
while (aux <> nil) do
|
|
begin
|
|
writeln(aux^.cpf);
|
|
aux:= aux^.prox
|
|
end;
|
|
end;
|
|
end;
|
|
|
|
procedure buscar(var cpfs:thash;cpf:string);
|
|
var
|
|
aux:^ptcpf;
|
|
achou:boolean;
|
|
valor:string;
|
|
begin
|
|
achou:=false;
|
|
valor:= 'Nao achou';
|
|
aux := cpfs[ (charint(cpf[10]) *10+ charint(cpf[11])) ];
|
|
while (aux <> nil) and (not achou) do
|
|
begin
|
|
if(aux^.cpf = cpf) then
|
|
begin
|
|
achou:=true;
|
|
valor:=(aux^.cpf) ;
|
|
end
|
|
else
|
|
aux:= aux^.prox;
|
|
end;
|
|
writeln(valor);
|
|
end;
|
|
|
|
|
|
procedure removerCPF(var cpfs:thash;cpf:string) ;
|
|
var
|
|
aux:^ptcpf;
|
|
aux2:^ptcpf;
|
|
achou:boolean;
|
|
begin
|
|
achou:=false;
|
|
aux := cpfs[ (charint(cpf[10]) *10+ charint(cpf[11])) ];
|
|
while (aux <> nil) and (not achou) do
|
|
begin
|
|
if(aux^.cpf = cpf) then
|
|
begin
|
|
achou:=true;
|
|
aux2:=(aux^.prox) ;
|
|
end
|
|
else
|
|
aux:= aux^.prox
|
|
end;
|
|
end;
|
|
procedure cpfsInicializar ( var cpfs : thash ) ;
|
|
var
|
|
i : integer ;
|
|
|
|
begin
|
|
for i := 0 to 99 do
|
|
cpfs[i] := nil ;
|
|
end;
|
|
|
|
|
|
begin
|
|
cont := false ;
|
|
op := 1 ;
|
|
cpfsInicializar(hash) ;
|
|
while not cont do
|
|
begin
|
|
|
|
writeln('ADD cpf : 1');
|
|
writeln('Validar existencia : 2');
|
|
writeln('RM cpf : 3');
|
|
writeln('exit : 0');
|
|
readln(op);
|
|
|
|
case op of
|
|
1 :
|
|
begin
|
|
writeln('Digite o cpf a ser inserido');
|
|
readln(cpf) ;
|
|
if (validarCPF(cpf)) then
|
|
begin
|
|
inserirCPF(hash, cpf);
|
|
writeln('inserido');
|
|
end
|
|
else
|
|
writeln('cpf invalido poxa');
|
|
end;
|
|
|
|
2 :
|
|
begin
|
|
writeln('Digite o cpf a ser validado a existencia');
|
|
readln(cpf) ;
|
|
if (validarCPF(cpf)) then
|
|
begin
|
|
if validacaoDeInexistencialismoDeOutroCadastroDePessoasFisicaDentroDaLista(cpf,hash) then
|
|
writeln('está na lista já')
|
|
else
|
|
writeln('num tá');
|
|
end
|
|
else
|
|
writeln('cpf invalido poxa');
|
|
end;
|
|
|
|
3:
|
|
begin
|
|
writeln('Digite o cpf a ser removido');
|
|
readln(cpf) ;
|
|
if (validarCPF(cpf)) then
|
|
begin
|
|
removerCPF(hash, cpf);
|
|
writeln('removido');
|
|
end
|
|
else
|
|
writeln('cpf invalido poxa');
|
|
end;
|
|
|
|
0 :
|
|
begin
|
|
cont:= true;
|
|
end;
|
|
else
|
|
writeln('opção invalidade ');
|
|
end;
|
|
end;
|
|
end. |