quinta-feira, 12 de outubro de 2017

Gerador de cpf - Linguagem C

Apenas para fins didáticos,
e sem me responsabilizar por uso indevido,
apresento este gerador de cpf.
O gerador aqui proposto pode gerar
até 11 cpfs de uma só vez,
e foi criado em linguagem C.
Quantidades inferiores a 1,
ou maior que 11 não passa pela entrada,
como também qualquer caractere,
passando mesmo tão somente dígitos.
Fica em sua responsabilidade o uso,
não indicado pelo autor que sou eu mesmo.

Veja abaixo uma imagem do programa em execução:


Veja abaixo o código do programa:

#include <stdio.h>
#include <conio.h>
#include <time.h>

void Pausa ( ) {
     textcolor ( LIGHTBLUE );
     gotoxy ( 19, 19 );
     printf ( "Por: " );
     Sleep ( 800 );
     textcolor ( LIGHTMAGENTA );
     printf ( "Samuel Lima" );
     textcolor ( BLACK );
     gotoxy ( 19, 20 );
     Sleep ( 800 );
     printf ( "sa_sp10@hotmail.com" );
     textcolor ( LIGHTRED );
     gotoxy ( 28, 23 );
     Sleep ( 1800 );
     printf ( "MUITO OBRIGADO" );
     getche ( );
}
void Janela ( ) {
     int n, c;
     c = 0;
     for ( n = 2; n <= 24; n++ )
         for ( c = 3; c <= 63; c++ ) {
              gotoxy ( c, n );
              textbackground ( WHITE );
              printf ( " " );
         }
}
int cpf;
int Restr_caracteres ( char num [ 4 ] ) {
     int i, a = 0;
     a = strlen ( num );
     for ( i = 0; i < a; i++ ) {
         if ( num [ i ] < '0' || num [ i ] > '9' )
              return 0;
     }
     return 1;
}
void Gera_Cpf ( ) {
     textcolor ( LIGHTRED );
     gotoxy ( 26, 3 );
     printf ( "GERADOR DE CPF" );
     textcolor ( LIGHTBLUE );
     gotoxy ( 19, 5 );
     printf ( "Quantos cpfs deseja gerar? => " );
     if ( ( cpf >= 1 && cpf <= 11 ) ) {
         textcolor ( LIGHTRED );
         printf ( "%d", cpf );
     }
}
int main ( ) {
     system ( "title GERADOR DE CPF" );
     char c_pf [ 20 ];
     int *Vet_cpf;
     int Vet_cpf_2 [ 12 ];
     int Vet_cpf_3 [ 12 ];
     int val_cpf [ 10 ] = { 10, 9, 8, 7, 6, 5, 4, 3, 2 };
     int val_cpf_2 [ 11 ] = { 11, 10, 9, 8, 7, 6, 5, 4, 3, 2 };
     int n = 9;
     int a = 0, e = 0, f = 0, g = 0, h = 0, i = 0, j = 0, l = 0;
     Janela ( );
     srand ( time ( NULL ) );
     int erro = 0, tot = 0;
     Janela ( );
     do {
         Janela ( );
         Gera_Cpf ( );
         if ( erro == 1 ) {
              Gera_Cpf ( );
         }
         textcolor ( LIGHTRED );
         gets ( c_pf );
         tot = atoi ( c_pf );
         if ( Restr_caracteres ( c_pf ) == 0 ) {
              textcolor ( LIGHTBLUE );
              gotoxy ( 20, 7 );
              printf ( "Voce digitou ==> " );
              textcolor ( LIGHTRED );
              printf ( "%s", c_pf );
              textcolor ( BLACK );
              gotoxy ( 20, 9 );
              printf ( "\aIsto não é válido !" );
              Pausa ( );
              erro = 1;
              continue;
         }
         if ( tot < 1 || tot > 11 ) {
              textcolor ( LIGHTBLUE );
              gotoxy ( 15, 7 );
              printf ( "\aO mínimo permitido é de " );
              textcolor ( LIGHTRED );
              printf ( "1 " );
              textcolor ( LIGHTBLUE );
              gotoxy ( 15, 8 );
              printf ( "e no máximo " );
              textcolor ( LIGHTRED );
              printf ( "11 " );
              textcolor ( LIGHTBLUE );
              printf ( "cpfs " );
              getche ( );
              erro = 1;
              continue;
         }
         //break;
     } while ( ( tot < 1 || tot > 11 ) );
     Vet_cpf = ( int * ) malloc ( n * sizeof ( int ) );
     for ( a = 0; a < tot; a++ ){
         gotoxy ( 16, a + 7 );
         for ( i = 0; i < n; i++ ){
              Vet_cpf [ i ] = rand () % 10;
         }
         for ( i = 0; i < 9; i++ ) {
              Vet_cpf_2 [ i ] = Vet_cpf [ i ] * val_cpf [ i ];
              e = e += Vet_cpf_2 [ i ];
         }
         f = e % 11;
         g = 11 - f;
         if ( g > 9 )
              g = 0;
         Vet_cpf [ 9 ] = g;
         for ( i = 0; i < 10; i++ ) {
              Vet_cpf_3 [ i ] = Vet_cpf [ i ] * val_cpf_2 [ i ];
              h = h += Vet_cpf_3 [ i ];
         }
         j = h % 11;
         l = 11 - j;
         if ( l > 9 )
              l = 0;
         Vet_cpf [ 9 ] = g;
         Vet_cpf [ 10 ] = l;
         //Abaixo o cpf gerado já com seus dois últimos dígitos
         textcolor ( LIGHTRED );
         for ( i = 0; i < 11; i++ ) {
              if ( i == 3 )
                   printf ( "." );
              if ( i == 6 )
                   printf ( "." );
              if ( i == 9 )
                   printf ( "-" );
              printf ( " %d ", Vet_cpf [ i ] );
         }
         e = 0, f = 0, g = 0, h = 0, i = 0, j = 0, l = 0;
     }
     free ( Vet_cpf );
     Pausa ( );

}

Nenhum comentário:

Postar um comentário

Observação: somente um membro deste blog pode postar um comentário.