terça-feira, 28 de junho de 2016

Apagando caracteres em matriz

Apagar caracteres de uma matriz de string é a finalidade deste programa.
Na verdade modifiquei poucas linhas do último código,
e acrescentei outras, para mostrar aos interessados em C que não
tem nenhum segredo nisto.
Em uma das imagens abaixo o leitor observa que a ocorrências
de vários caracteres foram apagados, e se continuasse apagando
a tela do dos ficaria toda limpa.
O código é indicado a iniciantes em C.

Veja abaixo algumas imagens do programa em execução:





Veja abaixo o código do programa:



#include <stdio.h>
#include <conio.h>
#include <string.h>
void got_color ( int tam_lin_ini, int tam_lin_fim, int tam_ini_col,
         int tam_fim_col, int a, int b ) {
     int i, c;
     if ( a >= 1 && a <= 16 ) {
         textcolor ( a );
     }
     if ( b >= 1 && b <= 16 ) {
         textbackground ( b );
     }
     for ( i = tam_lin_ini; i < tam_lin_fim; i++ ) {
         for ( c = tam_ini_col; c < tam_fim_col; c++ ) {
              gotoxy ( c, i );
              printf ( " " );
         }
     }
}
int main ( ) {
     system ( "title APAGANDO CARACTERES EM MATRIZ" );
     system ( "Color E7" );
     got_color ( 2, 34, 3, 79, 2, 0 );
     int i, j, n;
     int cont = 0;
char Vet [ 20 ] [ 43 ] = {
              "Q O B G C O H O P P N A Y G B D F R H O",
              "O C V W M L W A U P W G K I S J G A U A",
              "O I I J V I P I D F Q N I O K Z Z Z Y C",
              "T T W S S C P A X B L I G S O R V I O A",
              "N S O J E A M B I N D T H T Y H F L O E",
              "G I H T J B M K J Z I A A G O K I A M T",
              "R L T P N W T Q Y F Y C O T Z W S B H E",
              "X A V C L E R G V W C H N G A J J A I C",
              "A B Z S L F M I K T H E I V O U K G L A",
              "Z A J I Z E A A R I M S A R K P N D F C",
              "R C Y B L B T W H A N A E E H C Z N L L",
              "R J N E D A V N H N L D J N X Z A F E B",
              "C D A U C C N L E O I V X J Q T S F H I",
              "L A T G L O U A R C J P O E V J U V O X",
              "M O J I Q B L A B M A W S U D W G R F D",
              "R Y D H R R C O C Q L J R E G A E D V F",
              "E T K O I R K L G H A K D J B J B T T B",
              "E V B C A H V B A O W D C A I A Z A Z J",
              "O A N B Y C U V G C A C O S Q S W I L N",
              "Y R X U J C V S T X B G K Y W J B M G I"};
     char num [ 3 ];
     char c [ 3 ];
     textcolor ( LIGHTRED );
     gotoxy ( 27, 3 );
     printf ( "APAGANDO CARACTERES EM MATRIZ " );
     textcolor ( LIGHTBLUE );
     gotoxy ( 26, 5 );
     printf ( "Digite um caracter para apagar " );
     textcolor ( LIGHTCYAN );
     for ( i = 0; i < 20; i++ ) {
         gotoxy ( 22, i + 7 );
         for ( j = 0; j < 43; j++ ) {
              printf ( "%1c", Vet [ i ] [ j ] );
         }
     }
     do {
         textcolor ( LIGHTBLUE );
         gotoxy ( 22, 28 );
         printf ( "Para sair digite " );
         textcolor ( LIGHTRED );
         printf ( "[ - 1 ] " );
         gets ( num );
         fflush ( stdin );
         strupr ( num );
         *c = *num;
         n = atoi ( num );
         if ( n == -1 ) {
              textcolor ( LIGHTBLUE );
              gotoxy ( 22, 30 );
              printf ( "Por: " );
              textcolor ( LIGHTMAGENTA );
              printf ( "Samuel Lima" );
              textcolor ( BLACK );
              gotoxy ( 22, 31 );
              printf ( "sa_sp10@hotmail.com" );
              Sleep ( 1800 );
              textcolor ( LIGHTRED );
              gotoxy ( 37, 32 );
              printf ( "MUITO OBRIGADO" );
              getche ( );
              exit ( 0 );
         }
         for ( i = 0; i < 20; i++ ) {
              gotoxy ( 22, i + 7 );
              for ( j = 0; j < 43; j++ ) {
                   if ( Vet [ i ] [ j ] == *num ) {
                        Vet [ i ] [ j ] = ' ';
                        cont++;
                   }
              }
         }
         textcolor ( LIGHTBLUE );
         gotoxy ( 22, 30 );
         printf ( "Removido as" );
         textcolor ( LIGHTRED );
         printf ( " %d", cont );
         textcolor ( LIGHTBLUE );
         printf ( " ocorrências de" );
         textcolor ( LIGHTRED );
         printf ( " %c", *c );
         for ( i = 0; i < 20; i++ ) {
              gotoxy ( 22, i + 7 );
              for ( j = 0; j < 43; j++ ) {
                   if ( Vet [ i ] [ j ] == *num ) {
                        textcolor ( LIGHTRED );
                        printf ( "%c", Vet [ i ] [ j ] );
                   } else {
                        textcolor ( LIGHTCYAN );
                        printf ( "%c", Vet [ i ] [ j ] );
                   }
              }
         }
         getche ( );
         gotoxy ( 22, 30 );
         clreol ( );
         gotoxy ( 33, 28 );
         clreol ( );
     } while ( 1 );
     return 0;
}