117 lines
3.2 KiB
ObjectPascal
117 lines
3.2 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;
|
|
|
|
|
|
|
|
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; final:integer; var cpfs:thash ): boolean;
|
|
var
|
|
i:integer;
|
|
aux:^ptcpf;
|
|
existenciaDeTalInformacaoNaLista:boolean;
|
|
begin
|
|
aux := cpfs[final];
|
|
existenciaDeTalInformacaoNaLista := false;
|
|
while (aux^.prox <> nil) and (not existenciaDeTalInformacaoNaLista) do
|
|
begin
|
|
if (cpf = aux^.cpf) then
|
|
existenciaDeTalInformacaoNaLista := true
|
|
else
|
|
aux:= aux^.prox;
|
|
end;
|
|
validacaoDeInexistencialismoDeOutroCadastroDePessoasFisicasDentroDaLista := 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[10] );
|
|
end;
|
|
|
|
|
|
procedure inserirCPF(var cpfs:THash; var cpf:string);
|
|
var
|
|
i, pos: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 (validacaoDeInexistencialismoDeOutroCadastroDePessoasFisicasDentroDaLista(cpf, doisDigitos, 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
|
|
else
|
|
writeln('CPF INVALIDO');
|
|
end;
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
begin
|
|
readln(cpf);
|
|
inserirCPF(hash,cpf);
|
|
end. |