domingo, 30 de agosto de 2015

Vetor - Removendo inteiros

Para os apaixonados por linguagem C, apresento este excelente código
didático,onde a finalidade é de remover elementos que foram gerados
e armazenados num vetor de 100 posições.
O código possui tantos recursos em suas mais de 270 linhas que prefiro não comentar,ficaria enfadonho falar  de cada um deles.
É preferível que o futuro programador já tenha bons conhecimentos desta importante linguagem.

 Veja abaixo imagens do programa em execução:









Veja abaixo o código do programa:

#include <stdio.h>
#include <conio.h>
#define tam 100
void Janela5 ( ) {
     int lin, col;
     col = 0;
     system ( "Color 10" );
     for ( lin = 2; lin <= 29 ; lin++ )
         for ( col = 2; col <= 78 ; col++ ) {
              gotoxy ( col , lin );
              textbackground ( LIGHTCYAN );
              printf ( " " );
         }
}
int continuando ( ) {
     int nr;
     do {
         system ( "Color 90" );
         Janela5 ( );
         textcolor ( LIGHTRED );
         gotoxy ( 30 , 3 );
         printf ( "VETOR - REMOVENDO INTEIROS" );
         textcolor ( BROWN );
         gotoxy ( 25 , 7 );
         printf ( "Programa desenvolvido por:" );
         textcolor ( GREEN );
         gotoxy ( 52 , 7 );
         printf ( "Samuel Lima" );
         textcolor ( BLACK );
         gotoxy ( 34 , 9 );
         printf ( "sa_sp10@hotmail.com" );
         textcolor ( LIGHTBLUE );
         gotoxy ( 23 , 11 );
         printf ( "Digite " );
         textcolor ( LIGHTRED );
         printf ( " 0 " );
         textcolor ( LIGHTBLUE );
         printf ( " para sair ou " );
         textcolor ( LIGHTRED );
         printf ( " 1 " );
         textcolor ( LIGHTBLUE );
         printf ( " para continuar " );
         textcolor ( LIGHTRED );
         gotoxy ( 24 , 13 );
         scanf ( "%d" , &nr );
         fflush ( stdin );
         if ( nr == 0 ) {
              textcolor ( LIGHTRED );
              gotoxy ( 36 , 18 );
              printf ( "MUITO OBRIGADO" );
              getche ( );
              exit ( 0 );
         } else if ( nr == 1 ) {
              return 1;
         }
         textcolor ( BLACK );
         gotoxy ( 36 , 16 );
         printf ( "\aopcão errada!" );
         getche ( );
     } while ( 1 );
     return 1;
}
int Gera_Num_Sem_Repetidos ( int *A ) {
     int aut, i, j, result = 0, t = tam;
     for ( i = 0; i < 100 ; i++ ) {
         do {
              do {
                   t = rand ( ) % tam;
                   if ( t == 0 ) {
                        t = tam;
                   }
                   break;
              } while ( 1 );
              aut = 0;
              for ( j = 0; j < tam ; j++ )
                   if ( t == A [ j ] )
                        aut = 1;
              if ( aut == 0 ) {
                   A [ i ] = t;
              }
         } while ( aut == 1 );
     }
     textcolor ( LIGHTRED );
     gotoxy ( 28 , 3 );
     printf ( "VETOR - REMOVENDO INTEIROS" );
     textcolor ( LIGHTBLUE );
     gotoxy ( 21 , 5 );
     printf ( "Abaixo os 100 números gerados sem repetições\n" );
     textcolor ( BLACK );
     gotoxy ( 17 , 7 );
     for ( i = 0; i < 100 ; i++ ) {
         if ( i == 10 ) {
              gotoxy ( 17 , 8 );
         }
         if ( i == 20 ) {
              gotoxy ( 17 , 9 );
         }
         if ( i == 30 ) {
              gotoxy ( 17 , 10 );
         }
         if ( i == 40 ) {
              gotoxy ( 17 , 11 );
         }
         if ( i == 50 ) {
              gotoxy ( 17 , 12 );
         }
         if ( i == 60 ) {
              gotoxy ( 17 , 13 );
         }
         if ( i == 70 ) {
              gotoxy ( 17 , 14 );
         }
         if ( i == 80 ) {
              gotoxy ( 17 , 15 );
         }
         if ( i == 90 ) {
              gotoxy ( 17 , 16 );
         }
         printf ( " %3d " , A [ i ] );
         result += A [ i ];
     }
     textcolor ( LIGHTBLUE );
     gotoxy ( 20 , 18 );
     Sleep ( 500 );
     printf ( "Total de números gerados ==> " );
     textcolor ( LIGHTRED );
     printf ( "%d" , i );
     textcolor ( LIGHTBLUE );
     gotoxy ( 20 , 19 );
     Sleep ( 500 );
     printf ( "Soma dos números gerados ==> " );
     textcolor ( LIGHTRED );
     printf ( "%d" , result );
     Sleep ( 500 );
     return ( 0 );
}
int cnmemadc ( char num [ 3 ] , int n , int x , int y ) {
     if ( n > x && n < y ) {
         return 0;
     }
     if ( n < 0 ) {
         gotoxy ( 20 , 23 );
         printf ( "\aNegativos não são válidos" );
         return 0;
     }
     if ( n > y ) {
         gotoxy ( 20 , 23 );
         printf ( "\aNúmero acima do permitido" );
         return 1;
     }
     if ( *num < '0' || *num > '9' ) {
         gotoxy ( 20 , 23 );
         printf ( "\aCaracteres não são aceitos" );
         Sleep ( 1800 );
         textcolor ( BLACK );
         gotoxy ( 20 , 25 );
         printf ( "PRESSIONE QUALQUER TECLA" );
         return 1;
     } else {
         if ( n < y ) {
              gotoxy ( 20 , 23 );
              printf ( "\aNúmero abaixo do permitido" );
              return 0;
         }
     }
     return ( 0 );
}
float Inte_iro ( float y ) {
     int inteiro = 0;
     inteiro = y;
     if ( y == ( float ) inteiro ) {
         return 1;
     } else {
         return 0;
     }
}
int main ( ) {
     system ( "title VETOR - REMOVENDO INTEIROS" );
     continuando ( );
     system ( "cls" );
     Janela5 ( );
     unsigned int cont, i, j, d, k = 0, x = 0;
     int aut, *A, n, temp, t = 0, y = 0, *vet_or, vezes = 0;
     float h = 0;
     vet_or = ( int* ) malloc ( tam * sizeof(int) );
     A = ( int* ) malloc ( tam * sizeof(int) );
     char num [ 3 ];
     char n_um [ 3 ];
     vezes = Gera_Num_Sem_Repetidos ( A );
     do {
         do {
              textcolor ( LIGHTBLUE );
              gotoxy ( 20 , 21 );
              printf ( "Quantos números deseja remover ? " );
              textcolor ( LIGHTRED );
              gets ( num );
              fflush ( stdin );
              d = atoi ( num );
              h = atof ( num );
              if ( Inte_iro ( h ) == 1 ) {
                   break;
              } else {
                   textcolor ( LIGHTBLUE );
                   gotoxy ( 20 , 23 );
                   printf ( "O Numero " );
                   textcolor ( LIGHTRED );
                   printf ( "%.1f " , h );
                   textcolor ( LIGHTBLUE );
                   printf ( "Não é inteiro" );
                   textcolor ( BLACK );
                   gotoxy ( 20 , 25 );
                   Sleep ( 1000 );
                   printf ( "PRESSIONE QUALQUER TECLA" );
                   getche ( );
              }
              gotoxy ( 20 , 23 );
              clreol ( );
              gotoxy ( 20 , 25 );
              clreol ( );
              gotoxy ( 53 , 21 );
              clreol ( );
         } while ( 1 );
         textcolor ( LIGHTBLUE );
         cnmemadc ( num , d , 4 , 10 );
         getche ( );
         gotoxy ( 20 , 23 );
         clreol ( );
         gotoxy ( 53 , 21 );
         clreol ( );
         gotoxy ( 20 , 25 );
         clreol ( );
     } while ( ( d < 5 ) || ( d > 10 ) );
     int vetor [ d ];
     for ( i = 0; i < d ; i++ ) {
         do {
              do {
                   do {
                        textcolor ( LIGHTBLUE );
                        gotoxy ( 20 , 23 );
                        printf ( "Insira o" );
                        textcolor ( LIGHTRED );
                        gotoxy ( 29 , 23 );
                        printf ( "%do" , i + 1 );
                        textcolor ( LIGHTBLUE );
                        gotoxy ( 32 , 23 );
                        printf ( " Número : " );
                        textcolor ( LIGHTRED );
                        gets ( n_um );
                        n = atoi ( n_um );
                        h = atof ( n_um );
                        if ( Inte_iro ( h ) == 1 ) {
                            break;
                        } else {
                            textcolor ( LIGHTBLUE );
                            gotoxy ( 20 , 23 );
                            printf ( "O Numero " );
                            textcolor ( LIGHTRED );
                            printf ( "%.1f " , h );
                            textcolor ( LIGHTBLUE );
                            printf ( "Não é inteiro" );
                            textcolor ( BLACK );
                            gotoxy ( 20 , 25 );
                            Sleep ( 1000 );
                            printf ( "PRESSIONE QUALQUER TECLA" );
                            getche ( );
                        }
                        gotoxy ( 20 , 23 );
                        clreol ( );
                        gotoxy ( 20 , 25 );
                        clreol ( );
                        gotoxy ( 53 , 21 );
                        clreol ( );
                   } while ( 1 );
                   if ( n < 1 || n > tam ) {
                        textcolor ( LIGHTBLUE );
                        gotoxy ( 20 , 25 );
                        printf ( "\aSó digite números acima de " );
                        textcolor ( LIGHTRED );
                        printf ( "0" );
                        textcolor ( LIGHTBLUE );
                        printf ( " e menor que" );
                        textcolor ( LIGHTRED );
                        printf ( " 100" );
                        Sleep ( 1800 );
                        gotoxy ( 10 , 25 );
                        clreol ( );
                        gotoxy ( 20 , 23 );
                        clreol ( );
                        gotoxy ( 15 , 26 );
                        clreol ( );
                   }
              } while ( n < 1 || n > tam );
              aut = 0;
              for ( j = 0; j <= d ; j++ )
                   if ( n == vetor [ j ] )
                        aut = 1;
              if ( aut == 0 ) {
                   vetor [ x ] = n;
                   x++;
              } else {
                   textcolor ( LIGHTBLUE );
                   gotoxy ( 20 , 25 );
                   printf ( "O Número " );
                   textcolor ( LIGHTRED );
                   printf ( "%d " , n );
                   textcolor ( LIGHTBLUE );
                   printf ( " já foi escolhido" );
                   textcolor ( LIGHTBLUE );
                   gotoxy ( 20 , 27 );
                   printf ( "Veja abaixo os Números escolhidos até aqui " );
                   textcolor ( LIGHTRED );
                   gotoxy ( 22 , 29 );
                   k = x;
                   for ( x = 0; x < k ; x++ ) {
                        printf ( " %d " , vetor [ x ] );
                   }
                   getche ( );
              }
              gotoxy ( 20 , 25 );
              clreol ( );
              gotoxy ( 20 , 27 );
              clreol ( );
              gotoxy ( 20 , 29 );
              clreol ( );
              gotoxy ( 20 , 23 );
              clreol ( );
         } while ( aut == 1 );
     }
     textcolor ( YELLOW );
     gotoxy ( 31 , 25 );
     for ( i = 0; i < tam - 1 ; i++ ) {
         for ( j = 0; j < tam - 1 ; j++ ) {
              if ( A [ j ] > A [ j + 1 ] ) {
                   temp = A [ j ];
                   A [ j ] = A [ j + 1 ];
                   A [ j + 1 ] = temp;
              }
         }
     }
     for ( i = 0; i < tam ; i++ )
         for ( j = 0; j < d ; j++ ) {
              if ( A [ i ] == vetor [ j ] && A [ i ] != 0 )
                   cont++;
         }
     system ( "cls" );
     Janela5 ( );
     textcolor ( LIGHTRED );
     gotoxy ( 28 , 3 );
     printf ( "VETOR - REMOVENDO INTEIROS" );
     textcolor ( LIGHTBLUE );
     gotoxy ( 20 , 5 );
     printf ( "Abaixo os " );
     textcolor ( LIGHTRED );
     printf ( "%d " , d );
     textcolor ( LIGHTBLUE );
     printf ( "números que foram removidos " );
     textcolor ( LIGHTRED );
     gotoxy ( 20 , 7 );
     for ( i = 0; i < d ; i++ ) {
         printf ( " %d " , vetor [ i ] );
     }
     getche ( );
     textcolor ( LIGHTBLUE );
     gotoxy ( 20 , 9 );
     printf ( "Abaixo os " );
     textcolor ( LIGHTRED );
     printf ( "%d" , tam - d );
     textcolor ( LIGHTBLUE );
     printf ( " números que ficaram" );
     textcolor ( BLACK );
     gotoxy ( 17 , 11 );
     for ( i = 0; i < tam ; i++ ) {
         for ( j = 0; j < d ; j++ )
              if ( A [ i ] == vetor [ j ] )
                   break;
         if ( j == d ) {
              vet_or [ t ] = A [ i ];
              t++;
         }
     }
     y = t;
     for ( t = 0; t < y ; t++ ) {
         if ( t == 10 ) {
              gotoxy ( 17 , 12 );
         }
         if ( t == 20 ) {
              gotoxy ( 17 , 13 );
         }
         if ( t == 30 ) {
              gotoxy ( 17 , 14 );
         }
         if ( t == 40 ) {
              gotoxy ( 17 , 15 );
         }
         if ( t == 50 ) {
              gotoxy ( 17 , 16 );
         }
         if ( t == 60 ) {
              gotoxy ( 17 , 17 );
         }
         if ( t == 70 ) {
              gotoxy ( 17 , 18 );
         }
         if ( t == 80 ) {
              gotoxy ( 17 , 19 );
         }
         if ( t == 90 ) {
              gotoxy ( 17 , 20 );
         }
         printf ( " %3d " , vet_or [ t ] );
     }
     Sleep ( 1000 );
     textcolor ( LIGHTRED );
     gotoxy ( 36 , 28 );
     printf ( "MUITO OBRIGADO" );
     getche ( );
     free ( A );
     free ( vet_or );
     return ( 0 );
}