sábado, 14 de junho de 2014

Gerando e contando caracteres repetidos

Tudo começa com a função main(); chamando a função continuando():
que contém poucas informações sobre o autor do código, que sou eu mesmo, E então, temos uma Matriz Multidimensional do tipo char, que são preenchidas em todas as suas posições com os caracteres gerados pela função do tipo inteiro gera_caractere();
A função strlen ( *matriz ); conta o total de caracteres que foram gerados pela função rand(); que está sendo usada pela função
gera_caractere();
Um bloco de ordenação, pelo método da bolha, foi implementado
antes do bloco que faz a contagem dos caracteres que se repetem
e são imprimidos por printf();

Por aqui as dimensões do cmd ( prompt de comando ), ficaram sassim:

Tamanho do buffer da tela

Largura: 80
altura: 36

Tamanho da janela

Largura: 80
altura: 36

Posição da janela

Esquerda: 229
Superior: 36

A fonte usada, foi a Lucida Console 16.

Curta minha página no facebook

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




Veja o código do programa abaixo:



#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define tam 25
void Dimensao ( int x, int y ) {
     DWORD Tamanho = 2000;
     DWORD Escritos;
     COORD Pos = { x, y };
     FillConsoleOutputCharacter ( GetStdHandle ( STD_OUTPUT_HANDLE ), ' ',
              Tamanho, Pos, &Escritos );
     SetConsoleCursorPosition ( GetStdHandle ( STD_OUTPUT_HANDLE ), Pos );
}
void Janela5 ( ) {
     int lin, col;
     system ( "color E3" );
     for ( lin = 0; lin <= 36; lin++ ) {
         for ( col = 0; col <= 80; col++ ) {
              gotoxy ( col, lin );
              if ( lin == 2 ) {
                   textbackground ( BLACK );
                   printf ( " " );
              }
              if ( col == 1 ) {
                   textbackground ( BLACK );
                   printf ( " " );
              }
              if ( lin == 36 ) {
                   textbackground ( BLACK );
              }
              if ( col == 80 ) {
                   textbackground ( BLACK );
                   printf ( " " );
              }
         }
     }
     textbackground ( BLACK );
}
int continuando ( ) {
     int nr;
     do {
         system ( "cls" );
         Janela5 ( );
         textcolor ( LIGHTRED );
         gotoxy ( 23, 5 );
         printf ( "GERANDO E CONTANDO CARACTERES REPETIDOS" );
         textcolor ( YELLOW );
         gotoxy ( 24, 7 );
         printf ( "Programa desenvolvido por:" );
         textcolor ( LIGHTGRAY );
         gotoxy ( 51, 7 );
         printf ( "Samuel Lima" );
         textcolor ( LIGHTGREEN );
         gotoxy ( 34, 9 );
         printf ( "sa_sp10@hotmail.com" );
         textcolor ( LIGHTBLUE );
         gotoxy ( 23, 11 );
         printf ( "Digite 0 para sair ou 1 para continuar : " );
         textcolor ( LIGHTRED );
         scanf ( "%d", &nr );
         fflush ( stdin );
         fflush ( stdin );
         if ( nr == 0 ) {
              textcolor ( LIGHTRED );
              gotoxy ( 35, 21 );
              printf ( "MUITO OBRIGADO" );
              getche ( );
              exit ( 0 );
         } else if ( nr == 1 ) {
              return 1;
         }
         textcolor ( YELLOW );
         gotoxy ( 36, 16 );
         printf ( "\aopção errada!" );
         getche ( );
     } while ( 1 );
     return 1;
}
int gera_caractere ( ) {
     return ( rand ( ) % 26 ) + 65;
     getche ( );
     return ( 0 );
}
int main ( ) {
     system ( "title GERANDO E CONTANDO CARACTERES REPETIDOS" );
     continuando ( );
     system ( "cls" );
     Janela5 ( );
     int i, j, aux, ord, x, y = 0, vezes;
     char VET [ 626 ];
     char matriz [ tam ] [ tam ];
     for ( i = 0; i < tam; i++ ) {
         for ( j = 0; j < tam; j++ ) {
              matriz [ i ] [ j ] = gera_caractere ( );
         }
     }
     textcolor ( LIGHTRED );
     gotoxy ( 25, 3 );
     printf ( "GERANDO E CONTANDO CARACTERES REPETIDOS" );
     textcolor ( LIGHTBLUE );
     gotoxy ( 23, 5 );
     printf ( "Mostrando abaixo os" );
     textcolor ( LIGHTRED );
     printf ( " %d ", strlen ( *matriz ) );
     textcolor ( LIGHTBLUE );
     printf ( "caracteres gerados" );
     printf ( "\n\n" );
     for ( i = 0; i < tam; i++ ) {
         textcolor ( YELLOW );
         Dimensao ( 15, i + 7 );
         printf ( "%d ", i + 0 );
         textcolor ( WHITE );
         for ( j = 0; j < tam; j++ ) {
              printf ( " %c", matriz [ i ] [ j ] );
              VET [ y ] = matriz [ i ] [ j ];
              y++;
         }
         printf ( "\n" );
     }
     Sleep ( 1000 );
     textcolor ( LIGHTRED );
     gotoxy ( 32, 34 );
     printf ( "PRESSIONE QUALQUER TECLA" );
     getche ( );
     ord = 0;
     while ( ord == 0 ) {
         ord = 1;
         for ( x = 1; x <= y; x++ ) {
              if ( VET [ x ] > VET [ ( x + 1 ) ] ) {
                   aux = VET [ x ];
                   VET [ x ] = VET [ ( x + 1 ) ];
                   VET [ ( x + 1 ) ] = aux;
                   ord = 0;
              }
         }
     }
     system ( "cls" );
     Janela5 ( );
     textcolor ( LIGHTRED );
     gotoxy ( 25, 3 );
     printf ( "GERANDO E CONTANDO CARACTERES REPETIDOS" );
     textcolor ( LIGHTBLUE );
     gotoxy ( 18, 5 );
     printf ( "Mostrando abaixo o total que cada caractere se repete" );
     for ( x = 2; x < y; x++ ) {
         vezes = 1;
         j = x + 1;
         while ( j < y )
              if ( VET [ j ] != VET [ x ] )
                   j++;
              else {
                   vezes++;
                   y--;
                   VET [ j ] = VET [ y ];
              }
         Dimensao ( 27, x + 4 );
         textcolor ( LIGHTBLUE );
         printf ( "A letra " );
         textcolor ( YELLOW );
         printf ( " %c ", VET [ x ] );
         textcolor ( LIGHTBLUE );
         printf ( " ocorre " );
         textcolor ( LIGHTRED );
         printf ( " %d ", vezes );
         textcolor ( LIGHTBLUE );
         printf ( " vêzes " );
     }
     Sleep ( 1000 );
     textcolor ( LIGHTRED );
     gotoxy ( 37, 34 );
     printf ( "MUITO OBRIGADO" );
     getche ( );
     exit ( 0 );
}


Nenhum comentário:

Postar um comentário

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