diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8a9dddc --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +teste +teste.o +.vscode/GTEMP.pas +cpfs.o +cpfs +GPATH +GRTAGS +GTAGS \ No newline at end of file diff --git a/.vscode/GTEMP.pas b/.vscode/GTEMP.pas index 3220f6b..42b0b07 100644 --- a/.vscode/GTEMP.pas +++ b/.vscode/GTEMP.pas @@ -6,13 +6,45 @@ type cpf : string; prox : TCPF; end; + THash = array[0..99] of TCPF; -procedure inserirCPF(); +procedure inserirCPF(var cpfs:THash; var cpf:string); var - i:integer; + i, pos:integer; + novo, atual, ant:TCPF; begin - + if validarCPF(cpf) then + begin + pos:=(Ord(cpf[10])-48)*10 + (Ord(cpf[11])-48); //esse ngocio pega os digitos verificadores, gpt ajudou, transformar string em int é pesadelo + new(novo); + if novo = nil then + writeln('Memoria cheia'); + else + begin + novo^.cpf := cpf; + novo^.prox := nil; + atual:= hash[pos]; + ant:=nil + while (atual <> nil) and (atual^.cpf < cpf) do + begin + ant := atual; + atual := atual^.prox; + end; + if ant = nil then + begin + novo^.prox := hash[pos]; + hash[pos] := novo; + end + else + begin + novo^.prox := atual; + ant^.prox := novo; + end; + end; + end + else + writeln('CPF INVALIDO'); end; function validarCPF(var cpf:string):boolean; @@ -46,7 +78,7 @@ function validarCPF(var cpf:string):boolean; var - hash : array[0..99] of TCPF; + hash: THash; cpf:string; bool:boolean; begin diff --git a/cpfs.pas b/cpfs.pas index 3220f6b..42b0b07 100644 --- a/cpfs.pas +++ b/cpfs.pas @@ -6,13 +6,45 @@ type cpf : string; prox : TCPF; end; + THash = array[0..99] of TCPF; -procedure inserirCPF(); +procedure inserirCPF(var cpfs:THash; var cpf:string); var - i:integer; + i, pos:integer; + novo, atual, ant:TCPF; begin - + if validarCPF(cpf) then + begin + pos:=(Ord(cpf[10])-48)*10 + (Ord(cpf[11])-48); //esse ngocio pega os digitos verificadores, gpt ajudou, transformar string em int é pesadelo + new(novo); + if novo = nil then + writeln('Memoria cheia'); + else + begin + novo^.cpf := cpf; + novo^.prox := nil; + atual:= hash[pos]; + ant:=nil + while (atual <> nil) and (atual^.cpf < cpf) do + begin + ant := atual; + atual := atual^.prox; + end; + if ant = nil then + begin + novo^.prox := hash[pos]; + hash[pos] := novo; + end + else + begin + novo^.prox := atual; + ant^.prox := novo; + end; + end; + end + else + writeln('CPF INVALIDO'); end; function validarCPF(var cpf:string):boolean; @@ -46,7 +78,7 @@ function validarCPF(var cpf:string):boolean; var - hash : array[0..99] of TCPF; + hash: THash; cpf:string; bool:boolean; begin diff --git a/teste.pas b/teste.pas new file mode 100644 index 0000000..7c2972b --- /dev/null +++ b/teste.pas @@ -0,0 +1,15 @@ +program tkn; +uses SysUtils; + + +var + cpf: string; + last: integer; + +begin + cpf := '12345678912'; + + last := (Ord(cpf[10])-Ord('0'))*10+(Ord(cpf[11])-Ord('0')); + + writeln(last); +end. \ No newline at end of file