sexta-feira, 11 de setembro de 2015

Inteiros : pesquisando - removendo e substituindo ocorrências

No pouco tempo que tenho estive pensando por aqui:
Ora, se meu programa recebe inteiros digitados pelo usuário, faz uma eficiente pesquisa,
e achando-o número pedido faz a remoção das ocorrências daquele número, por que não
colocar mais um recurso permitindo ao usuário recolocar o número removido em todas as suas
ocorrências ou até mesmo de inserir um novo número em todas as posições que foram removidas?
E foi isto que fiz, e disponibilizo-o gratuitamente para todos interessados em linguagem C.
Veja abaixo imagens do programa em execução:






Veja abaixo o código do programa:

#include <stdio.h>
#include <conio.h>
#define tam 50
void Janela5 ( ) {
     int lin, col;
     col = 0;
     for ( lin = 2; lin < 37; lin++ )
         for ( col = 2; col < 80; col++ ) {
              gotoxy ( col, lin );
              textbackground ( WHITE );
              printf ( " " );
         }
}
int Subs_titui ( int *s, int c, int x ) {
     int *p;
     for ( p = s; *p != '\0'; p++ )
         if ( *p == c ) {
              *p = x;
         }
     return *p;
}
int main ( ) {
     system ( "title INTEIROS : PESQUISANDO - REMOVENDO"
              " E SUBSTITUINDO OCORRÊNCIAS" );
     unsigned int cont, d, i, j, p, y;
     int *Ve_t;
     Ve_t = ( int* ) malloc ( tam * sizeof(int) );
     int Vet [ tam ];
     Janela5 ( );
     textcolor ( LIGHTRED );
     gotoxy ( 11, 3 );
     printf ( "INTEIROS : PESQUISANDO - REMOVENDO"
              " E SUBSTITUINDO OCORRÊNCIAS " );
     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" );
     Sleep ( 1800 );
     textcolor ( LIGHTRED );
     gotoxy ( 30, 23 );
     printf ( "PRESSIONE QUALQUER TECLA" );
     getche ( );
     gotoxy ( 25, 7 );
     clreol ( );
     gotoxy ( 52, 7 );
     clreol ( );
     gotoxy ( 34, 9 );
     clreol ( );
     gotoxy ( 30, 23 );
     clreol ( );
     do {
         textcolor ( LIGHTBLUE );
         gotoxy ( 20, 5 );
         printf ( "Quantos números deseja inserir ? " );
         textcolor ( LIGHTRED );
         scanf ( "%d", &p );
         fflush ( stdin );
         textcolor ( BLACK );
         gotoxy ( 28, 9 );
         if ( p < 1 || p > tam ) {
              printf ( "\aNúmero acima de 0 e menor que %d ", tam );
              Sleep ( 1800 );
              textcolor ( LIGHTRED );
              gotoxy ( 20, 23 );
              printf ( "Pressione qualquer tecla para tentar outra vez" );
              getche ( );
              gotoxy ( 7, 13 );
              clreol ( );
              gotoxy ( 20, 9 );
              clreol ( );
              gotoxy ( 52, 5 );
              clreol ( );
              gotoxy ( 20, 23 );
              clreol ( );
         }
     } while ( ( p < 1 ) || ( p > tam ) );
     for ( i = 0; i < p; i++ ) {
         textcolor ( LIGHTBLUE );
         gotoxy ( 20, 7 );
         printf ( "Insira o " );
         textcolor ( LIGHTRED );
         gotoxy ( 28, 7 );
         printf ( " %do", i + 1 );
         textcolor ( LIGHTBLUE );
         gotoxy ( 32, 7 );
         printf ( " número : " );
         textcolor ( LIGHTRED );
         scanf ( "%d", &Vet [ i ] );
         fflush ( stdin );
         gotoxy ( 41, 7 );
         clreol ( );
     }
     textcolor ( LIGHTBLUE );
     gotoxy ( 7, 9 );
     printf ( "Números digitados ==> " );
     for ( i = 0; i < p; i++ ) {
         textcolor ( BLACK );
         printf ( " %d", Vet [ i ] );
     }
     getche ( );
     do {
         textcolor ( LIGHTBLUE );
         gotoxy ( 7, 11 );
         printf ( "Digite um número para ser removido ==> " );
         i = 0;
         textcolor ( LIGHTRED );
         scanf ( "%d", &Ve_t [ i ] );
         fflush ( stdin );
         cont = 0;
         for ( i = 0; i < p; i++ )
              for ( j = 0; j < p - 1; j++ )
                   if ( Vet [ i ] == Ve_t [ j ] && Vet [ i ] != 0 )
                        cont++;
         if ( cont == 0 ) {
              textcolor ( LIGHTBLUE );
              gotoxy ( 7, 13 );
              printf ( "Não há Nenhuma ocorrência de " );
              textcolor ( LIGHTRED );
              printf ( "%d", Ve_t [ cont ] );
              textcolor ( LIGHTBLUE );
              printf ( " entre os números digitados" );
              Sleep ( 1800 );
              textcolor ( LIGHTRED );
              gotoxy ( 7, 15 );
              printf ( "Pressione qualquer tecla para tentar outra vez" );
              getche ( );
              gotoxy ( 46, 11 );
              clreol ( );
              gotoxy ( 7, 13 );
              clreol ( );
              gotoxy ( 7, 15 );
              clreol ( );
         }
     } while ( cont == 0 );
     textcolor ( LIGHTBLUE );
     gotoxy ( 7, 13 );
     printf ( "Entre os números ==> " );
     for ( i = 0; i < p; i++ ) {
         textcolor ( BLACK );
         printf ( "%d ", Vet [ i ] );
     }
     textcolor ( LIGHTBLUE );
     gotoxy ( 7, 15 );
     printf ( "Foram encontrados " );
     textcolor ( LIGHTRED );
     printf ( " %d ", cont );
     textcolor ( LIGHTBLUE );
     printf ( " números" );
     i = 0;
     textcolor ( LIGHTRED );
     printf ( " %d ", Ve_t [ i ] );
     textcolor ( LIGHTBLUE );
     gotoxy ( 7, 17 );
     printf ( "Mostrando abaixo os números ==> " );
     for ( i = 0; i < p; i++ ) {
         textcolor ( BLACK );
         printf ( "%d ", Vet [ i ] );
     }
     textcolor ( LIGHTBLUE );
     gotoxy ( 7, 19 );
     printf ( "Sem seus " );
     textcolor ( LIGHTRED );
     printf ( " %d ", cont );
     textcolor ( LIGHTBLUE );
     printf ( " dígitos " );
     i = 0;
     textcolor ( LIGHTRED );
     printf ( " %d ", Ve_t [ i ] );
     textcolor ( LIGHTBLUE );
     printf ( " que foram removidos " );
     textcolor ( BLACK );
     gotoxy ( 7, 21 );
     for ( i = 0; i < p; i++ ) {
         for ( j = 0; j < p - 1; j++ )
              if ( Vet [ i ] == Ve_t [ j ] )
                   break;
         if ( j == p - 1 ) {
              printf ( " %d", Vet [ i ] );
         }
     }
     getche ( );
     do {
         textcolor ( LIGHTBLUE );
         gotoxy ( 7, 23 );
         printf ( "Você removeu todas as ocorrências do número " );
         i = 0;
         textcolor ( LIGHTRED );
         printf ( " %d ", Ve_t [ i ] );
         textcolor ( LIGHTBLUE );
         gotoxy ( 7, 25 );
         printf ( "Deseja preencher as ocorrências removidas"
                   " com um novo número ?" );
         textcolor ( LIGHTBLUE );
         gotoxy ( 7, 27 );
         textcolor ( LIGHTRED );
         printf ( "1 " );
         textcolor ( LIGHTBLUE );
         printf ( "- Sim, desejo preencher as ocorrências removidas " );
         textcolor ( LIGHTRED );
         gotoxy ( 7, 28 );
         printf ( "2 " );
         textcolor ( LIGHTBLUE );
         printf ( "- Não, prefiro finalizar o programa assim mesmo " );
         textcolor ( LIGHTRED );
         scanf ( "%d", &d );
         fflush ( stdin );
         if ( d == 1 ) {
              break;
         }
         if ( d == 2 ) {
              Sleep ( 1000 );
              textcolor ( LIGHTRED );
              gotoxy ( 36, 32 );
              printf ( "MUITO OBRIGADO" );
              getche ( );
              exit ( 0 );
         }
         textcolor ( BLACK );
         gotoxy ( 36, 32 );
         printf ( "\aopcão errada!" );
         getche ( );
         gotoxy ( 36, 32 );
         clreol ( );
         gotoxy ( 50, 28 );
         clreol ( );
     } while ( 1 );
     textcolor ( LIGHTBLUE );
     gotoxy ( 7, 30 );
     printf ( "Digite o novo número para fazer a substituição " );
     textcolor ( LIGHTRED );
     scanf ( "%d", &y );
     fflush ( stdin );
     i = 0;
     Subs_titui ( Vet, Ve_t [ i ], y );
     textcolor ( LIGHTBLUE );
     gotoxy ( 7, 32 );
     printf ( "Os números foram substituidos com"
              " sucesso veja abaixo " );
     textcolor ( BLACK );
     gotoxy ( 7, 34 );
     for ( i = 0; i < p; i++ ) {
         printf ( " %d", Vet [ i ] );
     }
     Sleep ( 1000 );
     textcolor ( LIGHTRED );
     gotoxy ( 36, 36 );
     printf ( "MUITO OBRIGADO" );
     getche ( );
     free ( Ve_t );
     return ( 0 );
}

Nenhum comentário:

Postar um comentário

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