quarta-feira, 22 de abril de 2015

Vetor de caracteres sem repetições


Neste código trago um excelente exemplo de como preencher
um vetor de caracteres sem que nenhum elemento se repita.
Já postei um código como este onde caracteres e números decimais eram reprovados,
 neste a entrada de dados não permite
que números passem entre os caracteres, para que não seja
inseridos no vetor.
Código indicado a iniciantes em C, portanto os marmanjos e sabichões da linguagem nem precisa compilar.

Veja abaixo imagens do programa em execução:






Veja abaixo o código do programa:



#include <stdio.h>
#include <conio.h>
#define tam 20
void Janela5 ( ) {
     int lin, col;
     for ( lin = 0; lin <= 25; lin++ ) {
         for ( col = 0; col <= 80; col++ ) {
              gotoxy ( col, lin );
              if ( lin == 2 ) {
                   textbackground ( LIGHTBLUE );
                   printf ( " " );
              }
              if ( col == 1 ) {
                   textbackground ( LIGHTBLUE );
                   printf ( " " );
              }
              if ( lin == 25 ) {
                   textbackground ( LIGHTBLUE );
              }
              if ( col == 80 ) {
                   textbackground ( LIGHTBLUE );
                   printf ( " " );
              }
         }
     }
     textbackground ( BLACK );
}
int main ( ) {
     system ( "title VETOR DE CARACTERES SEM REPETIÇÕES" );
     char A [ tam ];
     char B [ tam ];
     char aut, n1 = 0, r = 0, temp;
     int i, j, k, m, x = 0;
     for ( i = 0; i < tam; i++ ) {
         do {
              do {
                   system ( "cls" );
                   Janela5 ( );
                   textcolor ( LIGHTRED );
                   gotoxy ( 26, 3 );
                   printf ( "VETOR DE CARACTERES SEM REPETIÇÕES" );
                   textcolor ( LIGHTBLUE );
                   gotoxy ( 33, 5 );
                   printf ( "Digite o" );
                   textcolor ( LIGHTRED );
                   gotoxy ( 40, 5 );
                   printf ( " %do ", i + 1 );
                   textcolor ( LIGHTBLUE );
                   gotoxy ( 44, 5 );
                   printf ( " caractere " );
                   textcolor ( LIGHTRED );
                   scanf ( "%c", &n1 );
                   fflush ( stdin );
                   if ( ( n1 < 'a' || n1 > 'z' ) && ( n1 < 'A' || n1 > 'Z' ) ) {
                        textcolor ( LIGHTBLUE );
                        gotoxy ( 31, 9 );
                        printf ( "\aNúmeros não são aceitos" );
                        textcolor ( YELLOW );
                        gotoxy ( 31, 15 );
                        Sleep ( 1000 );
                        printf ( "PRESSIONE QUALQUER TECLA" );
                        getche ( );
                   }
                   continue;
              } while ( ( n1 < 'a' || n1 > 'z' ) && ( n1 < 'A' || n1 > 'Z' ) );
              aut = 0;
              for ( j = 0; j < tam; j++ )
                   if ( n1 == A [ j ] )
                        aut = 1;
              if ( aut == 0 ) {
                   A [ i ] = n1;
                   B [ x ] = A [ i ];
                   x++;
              } else {
                   textcolor ( LIGHTBLUE );
                   gotoxy ( 22, 7 );
                   printf ( "Você já digitou o caractere" );
                   textcolor ( LIGHTRED );
                   gotoxy ( 47, 7 );
                   printf ( " %c", n1 );
                   textcolor ( LIGHTBLUE );
                   gotoxy ( 14, 9 );
                   printf ( "Veja abaixo os caracteres digitados até aqui " );
                   k = x;
                   textcolor ( LIGHTRED );
                   gotoxy ( 14, 11 );
                   for ( x = 0; x < k; x++ )
                        printf ( " %c ", B [ x ] );
                   textcolor ( YELLOW );
                   gotoxy ( 32, 15 );
                   printf ( "PRESSIONE QUALQUER TECLA" );
                   getche ( );
              }
         } while ( aut == 1 );
     }
     gotoxy ( 14, 7 );
     textcolor ( LIGHTBLUE );
     printf ( "Veja abaixo o vetor preenchido" );
     gotoxy ( 14, 9 );
     textcolor ( WHITE );
     for ( i = 0; i < tam; i++ ) {
         printf ( " %c ", A [ i ] );
     }
     getche ( );
     gotoxy ( 14, 11 );
     textcolor ( LIGHTBLUE );
     printf ( "Veja abaixo o vetor ordenado" );
     gotoxy ( 14, 13 );
     textcolor ( WHITE );
     for ( i = 0; i < tam - 1; i++ ) {
         m = i;
         for ( j = i + 1; j < tam; j++ ) {
              if ( A [ j ] < A [ m ] ) {
                   m = j;
                   r = 1;
              }
         }
         if ( r == 1 ) {
              temp = A [ m ];
              A [ m ] = A [ i ];
              A [ i ] = temp;
         }
     }
     for ( i = 0; i < tam; i++ ) {
         printf ( " %c ", A [ i ] );
     }
     Sleep ( 1800 );
     textcolor ( YELLOW );
     gotoxy ( 28, 17 );
     printf ( "Criado por:" );
     textcolor ( LIGHTRED );
     printf ( " Samuel Lima " );
     textcolor ( LIGHTBLUE );
     gotoxy ( 36, 23 );
     printf ( "MUITO OBRIGADO" );
     getche ( );
     exit ( 0 );
}