segunda-feira, 14 de setembro de 2015

Pesquisando apagando e substituindo ocorrências de string em matriz

O último trabalho antes deste recebeu o título de:
"PESQUISANDO E APAGANDO OCORRÊNCIAS DE STRING EM MATRIZ",
e o programa cumpre bem o que promete, não enrolo ninguém,
más observei que poderia fazer ainda melhor, removi todas
as posições que continha uma palavra pesquisada, porque não
substituí-las, com um pouco de esforço consegui fazer isto
e posto agora pra que todos vejam como foi feito e estude
o código, aliás, onde existe outro código como este?

Veja abaixo imagens do programa em execução:







Veja abaixo o código do programa:


#include <stdio.h>
#include <string.h>
#include <conio.h>
#define lin  12
#define col 43
void Janela5 ( ) {
     int n, c;
     c = 0;
     system ( "Color 00" );
     for ( n = 2; n <= 33; n++ )
         for ( c = 2; c <= 78; c++ ) {
              gotoxy ( c, n );
              textbackground ( WHITE );
              printf ( " " );
         }
}
int main ( ) {
     system ( "title PESQUISANDO APAGANDO E SUBSTITUINDO "
              "OCORRÊNCIAS DE STRING EM MATRIZ" );
     char texto [ lin ] [ col ] = {
              "No Meio do Caminho                        ",
              "Carlos Drummond de Andrade                ",
              "No meio do caminho tinha uma pedra        ",
              "Tinha uma pedra no meio do caminho        ",
              "Tinha uma pedra                           ",
              "No meio do caminho tinha uma pedra.       ",
              "Nunca me esquecerei desse acontecimento   ",
              "Na vida de minhas retinas tão fatigadas.  ",
              "Nunca me esquecerei que no meio do caminho",
              "Tinha uma pedra                           ",
              "Tinha uma pedra no meio do caminho        ",
              "No meio do caminho tinha uma pedra.       " };
     char tex_to [ lin ] [ col ];
     int a = 0, b = 0, i, d, j, k, cont = 0, lenv;
     char *pt, *y;
     pt = ( char * ) malloc ( 28 );
     y = ( char * ) malloc ( 28 );
     Janela5 ( );
     textcolor ( LIGHTRED );
     gotoxy ( 7, 3 );
     printf ( "PESQUISANDO APAGANDO E SUBSTITUINDO "
              "OCORRÊNCIAS DE STRING EM MATRIZ" );
     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 ( );
     textcolor ( LIGHTBLUE );
     gotoxy ( 29, 5 );
     printf ( "Abaixo a matriz imprimida" );
     for ( i = 0; i < lin; i++ ) {
         gotoxy ( 13, i + 7 );
         textcolor ( BLACK );
         for ( j = 0; j < col; j++ ) {
              printf ( "%c", texto [ i ] [ j ] );
              memcpy ( tex_to [ i ], texto [ i ], sizeof ( tex_to [ i ] ) );
         }
     }
     do {
         textcolor ( LIGHTBLUE );
         gotoxy ( 13, 20 );
         printf ( "Digite uma palavra para procurar ==> " );
         textcolor ( LIGHTRED );
         gets ( pt );
         fflush ( stdin );
         lenv = strlen ( pt );
         for ( i = 0; i < lin; i++ ) {
              for ( j = 0; j < col; j++ ) {
                   k = 0;
                   while ( texto [ i ] [ j + k ] == pt [ k ] && pt [ k ] != '\0'
                            && texto [ i ] [ j + k ] != '\0' )
                        k++;
                   if ( pt [ k ] == '\0' ) {
                        textbackground ( LIGHTGRAY );
                        ++cont;
                        gotoxy ( 22, a + 22 );
                        a++;
                        textcolor ( LIGHTBLUE );
                        printf ( "linha " );
                        textcolor ( LIGHTRED );
                        printf ( "%d", i + 1 );
                        textcolor ( LIGHTBLUE );
                        printf ( " a partir da coluna " );
                        textcolor ( LIGHTRED );
                        printf ( "%d", j + 1 );
                        memset ( &texto [ i ] [ j ], 0, lenv );
                   }
              }
         }
         if ( cont >= 1 ) {
              textcolor ( LIGHTBLUE );
              gotoxy ( 13, 23 + cont );
              textbackground ( WHITE );
              printf ( "Encontrado " );
              textcolor ( LIGHTRED );
              printf ( "%d", cont );
              textcolor ( LIGHTBLUE );
              printf ( " ocorrência ( s ) da palavra " );
              textcolor ( LIGHTRED );
              printf ( "%s", pt );
              Sleep ( 1800 );
              textcolor ( LIGHTRED );
              gotoxy ( 30, 32 );
              printf ( "PRESSIONE QUALQUER TECLA" );
         }
         if ( cont == 0 ) {
              textcolor ( LIGHTBLUE );
              gotoxy ( 7, 24 + cont );
              printf ( "Não há Nenhuma ocorrência da palavra " );
              textcolor ( LIGHTRED );
              printf ( "%s", pt );
              textcolor ( LIGHTBLUE );
              printf ( " na matriz, tente outra vez" );
              Sleep ( 1800 );
              textcolor ( LIGHTRED );
              gotoxy ( 30, 32 );
              printf ( "PRESSIONE QUALQUER TECLA" );
              getche ( );
              gotoxy ( 7, 24 + cont );
              clreol ( );
              gotoxy ( 30, 32 );
              clreol ( );
              gotoxy ( 13, 20 );
              clreol ( );
              gotoxy ( 13, 23 + cont );
              clreol ( );
         }
     } while ( cont == 0 );
     getche ( );
     Janela5 ( );
     textcolor ( LIGHTRED );
     gotoxy ( 7, 3 );
     printf ( "PESQUISANDO APAGANDO E SUBSTITUINDO "
              "OCORRÊNCIAS DE STRING EM MATRIZ" );
     textcolor ( LIGHTBLUE );
     gotoxy ( 13, 5 );
     printf ( "Matriz imprimida sem as " );
     textcolor ( LIGHTRED );
     printf ( "%d", cont );
     textcolor ( LIGHTBLUE );
     printf ( " ocorrências da palavra" );
     textcolor ( LIGHTRED );
     printf ( " %s", pt );
     textcolor ( BLACK );
     for ( i = 0; i < lin; i++ ) {
         gotoxy ( 13, i + 7 );
         for ( j = 0; j < col; j++ ) {
              printf ( "%c", texto [ i ] [ j ] );
         }
     }
     Sleep ( 1800 );
     do {
         textcolor ( LIGHTBLUE );
         gotoxy ( 7, 21 );
         printf ( "Você removeu todas as ocorrências da palavra " );
         textcolor ( LIGHTRED );
         printf ( " %s ", pt );
         textcolor ( LIGHTBLUE );
         gotoxy ( 7, 23 );
         printf ( "Deseja preencher as ocorrências "
                   "removidas com uma nova palavra ?" );
         textcolor ( LIGHTBLUE );
         gotoxy ( 7, 25 );
         textcolor ( LIGHTRED );
         printf ( "1 " );
         textcolor ( LIGHTBLUE );
         printf ( "- Sim, desejo preencher as ocorrências removidas " );
         textcolor ( LIGHTRED );
         gotoxy ( 7, 26 );
         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, 28 );
         printf ( "\aopcão errada!" );
         getche ( );
         gotoxy ( 36, 28 );
         clreol ( );
         gotoxy ( 50, 26 );
         clreol ( );
     } while ( 1 );
     textcolor ( LIGHTBLUE );
     gotoxy ( 7, 28 );
     printf ( "Digite uma nova palavra para fazer a substituição " );
     textcolor ( LIGHTRED );
     gets ( y );
     fflush ( stdin );
     lenv = strlen ( y );
     textcolor ( LIGHTBLUE );
     gotoxy ( 7, 30 );
     textcolor ( LIGHTRED );
     gotoxy ( 30, 32 );
     printf ( "PRESSIONE QUALQUER TECLA" );
     getche ( );
     Janela5 ( );
     textcolor ( LIGHTRED );
     gotoxy ( 7, 3 );
     printf ( "PESQUISANDO APAGANDO E SUBSTITUINDO "
              "OCORRÊNCIAS DE STRING EM MATRIZ" );
     textcolor ( LIGHTBLUE );
     gotoxy ( 13, 5 );
     printf ( "Abaixo a matriz imprimida sem as " );
     textcolor ( LIGHTRED );
     printf ( "%d", cont );
     textcolor ( LIGHTBLUE );
     printf ( " ocorrências da palavra" );
     textcolor ( LIGHTRED );
     printf ( " %s", pt );
     textcolor ( LIGHTBLUE );
     gotoxy ( 13, 6 );
     printf ( "que foram substituida por" );
     textcolor ( LIGHTRED );
     printf ( " %s", y );
     textcolor ( BLACK );
     gotoxy ( 7, 32 );
     for ( i = 0; i < lin; i++ ) {
         for ( j = 0; j < col; j++ ) {
              k = 0;
              while ( tex_to [ i ] [ j + k ] == pt [ k ] && pt [ k ] != '\0'
                        && tex_to [ i ] [ j + k ] != '\0' )
                   k++;
              if ( pt [ k ] == '\0' ) {
                   textbackground ( LIGHTGRAY );
                   gotoxy ( 22, b + 22 );
                   b++;
                   textcolor ( LIGHTBLUE );
                   printf ( "linha " );
                   textcolor ( LIGHTRED );
                   printf ( "%d", i + 1 );
                   textcolor ( LIGHTBLUE );
                   printf ( " a partir da coluna " );
                   textcolor ( LIGHTRED );
                   printf ( "%d", j + 1 );
                   //memcpy ( &tex_to [ i ] [ j ], y, lenv );
                   memmove ( &tex_to [ i ] [ j ], y, lenv );
              }
         }
     }
     textcolor ( LIGHTBLUE );
     gotoxy ( 13, b + 23 );
     textbackground ( WHITE );
     printf ( "Substituimos " );
     textcolor ( LIGHTRED );
     printf ( "%d", cont );
     textcolor ( LIGHTBLUE );
     printf ( " ocorrência ( s ) da palavra " );
     textcolor ( LIGHTRED );
     printf ( "%s ", pt );
     textcolor ( LIGHTBLUE );
     printf ( "por " );
     textcolor ( LIGHTRED );
     printf ( "%s", y );
     textcolor ( BLACK );
     for ( i = 0; i < lin; i++ ) {
         gotoxy ( 13, i + 8 );
         for ( j = 0; j < col; j++ ) {
              printf ( "%c", tex_to [ i ] [ j ] );
         }
     }
     Sleep ( 1000 );
     textcolor ( LIGHTRED );
     gotoxy ( 34, 32 );
     printf ( "MUITO OBRIGADO" );
     getche ( );
     free ( pt );
     free ( y );
     return 0;
}

Nenhum comentário:

Postar um comentário

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