terça-feira, 11 de março de 2014

Gerando - Imprimindo e Embaralhando Matriz

Neste programa mostro como criar uma Matriz
carregada por um contador, mostro ainda como
embaralhar todos os valores que foram gerados.


Veja abaixo, o código do programa:

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <windows.h>
#define tam 10
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 Imp_Matr ( char *num ) {
     unsigned int i;
     for ( i = 0; i < strlen ( num ); i++ ) {
         if ( num [ i ] < '0' || num [ i ] > '9' )
              return 0;
     }
     return 1;
}
void Pesquisa_Matriz_Embaralhada ( int M [ tam ] [ tam ] );
void Matriz_EMbaralhada_Sem_Efeito ( int M [ tam ] [ tam ] );
void Imprime_Matriz_Embaralhada ( int M [ tam ] [ tam ] );
void Embaralha_Matriz ( int M [ tam ] [ tam ] );
void Gera_Matriz ( int M [ tam ] [ tam ] ) {
     system ( "title GERANDO - IMPRIMINDO E EMBARALHANDO MATRIZ" );
     int i, j, t = 0;
     Janela5 ( );
     textcolor ( LIGHTRED );
     gotoxy ( 23, 3 );
     printf ( "GERANDO - IMPRIMINDO E EMBARALHANDO MATRIZ" );
     textcolor ( LIGHTBLUE );
     gotoxy ( 35, 5 );
     printf ( "GERANDO A MATRIZ" );
     textcolor ( YELLOW );
     for ( i = 0; i < tam; i++ ) {
         gotoxy ( 18, i + 8 );
         for ( j = 0; j < tam; j++ ) {
              t++;
              M [ i ] [ j ] = t;
              Sleep ( 100 );
              printf ( " %3d ", M [ i ] [ j ] );
         }
     }
     textcolor ( LIGHTBLUE );
     gotoxy ( 28, 20 );
     printf ( "A MATRIZ FOI GERADA COM SUCESSO" );
     textcolor ( LIGHTRED );
     gotoxy ( 32, 22 );
     printf ( "PRESSIONE QUALQUER TECLA" );
     getche ( );
     Embaralha_Matriz ( M );
}
void Embaralha_Matriz ( int M [ tam ] [ tam ] ) {
     clrscr ( );
     int i, j, r, y, temp;
     textcolor ( LIGHTRED );
     gotoxy ( 23, 3 );
     printf ( "GERANDO - IMPRIMINDO E EMBARALHANDO MATRIZ" );
     textcolor ( LIGHTBLUE );
     gotoxy ( 29, 5 );
     printf ( "IMPRIMINDO A MATRIZ EMBARALHADA" );
     for ( i = 0; i < tam; i++ ) {
         for ( j = 0; j < tam; j++ ) {
              r = rand ( ) % tam;
              y = rand ( ) % tam;
              temp = M [ i ] [ j ];
              M [ i ] [ j ] = M [ r ] [ y ];
              M [ r ] [ y ] = temp;
         }
     }
     Imprime_Matriz_Embaralhada ( M );
}
void Imprime_Matriz_Embaralhada ( int M [ tam ] [ tam ] ) {
     int r, y;
     clrscr ( );
     Janela5 ( );
     textcolor ( LIGHTRED );
     gotoxy ( 23, 3 );
     printf ( "GERANDO - IMPRIMINDO E EMBARALHANDO MATRIZ" );
     textcolor ( LIGHTBLUE );
     gotoxy ( 29, 5 );
     printf ( "EMBARALHANDO AGORA A MATRIZ ..." );
     textcolor ( LIGHTGREEN );
     for ( r = 0; r < tam; r++ ) {
         gotoxy ( 18, r + 8 );
         for ( y = 0; y < tam; y++ ) {
              Sleep ( 100 );
              printf ( " %3d ", M [ r ] [ y ] );
         }
     }
     textcolor ( LIGHTBLUE );
     gotoxy ( 26, 5 );
     printf ( "A MATRIZ FOI EMBARALHADA COM SUCESSO" );
     Pesquisa_Matriz_Embaralhada ( M );
}
void Matriz_EMbaralhada_Sem_Efeito ( int M [ tam ] [ tam ] ) {
     int r, y;
     Janela5 ( );
     textcolor ( LIGHTRED );
     gotoxy ( 23, 3 );
     printf ( "GERANDO - IMPRIMINDO E EMBARALHANDO MATRIZ" );
     textcolor ( LIGHTBLUE );
     gotoxy ( 33, 5 );
     printf ( "PESQUISANDO NA MATRIZ" );
     textcolor ( LIGHTGRAY );
     for ( r = 0; r < tam; r++ ) {
         gotoxy ( 18, r + 8 );
         for ( y = 0; y < tam; y++ ) {
              printf ( " %3d ", M [ r ] [ y ] );
         }
     }
     getche ( );
     Pesquisa_Matriz_Embaralhada ( M );
}
void Pesquisa_Matriz_Embaralhada ( int M [ tam ] [ tam ] ) {
     textcolor ( LIGHTGRAY );
     int i, j, n;
     char num [ 3 ];
     textcolor ( LIGHTBLUE );
     gotoxy ( 20, 19 );
     printf ( "Digite um N%c pra pesquisa ou [ -1 ] Para sair  ", 167 );
     textcolor ( YELLOW );
     gets ( num );
     fflush ( stdin );
     n = atoi ( num );
     if ( n == -1 ) {
         textcolor ( LIGHTRED );
         gotoxy ( 33, 22 );
         printf ( "O ROCCO AGRADECE" );
         Sleep ( 1800 );
         exit ( 0 );
     }
     if ( n < 0 ) {
         textcolor ( LIGHTGREEN );
         gotoxy ( 25, 21 );
         printf ( "\aO Unico Negativo Valido e o -1" );
         Sleep ( 1800 );
     }
     if ( n > 100 ) {
         textcolor ( LIGHTGREEN );
         gotoxy ( 22, 22 );
         printf ( "\aSo sao aceitos Numeros menores que 100 " );
         Sleep ( 1800 );
         system ( "cls" );
     }
     if ( Imp_Matr ( num ) == 0 || n == 0 ) {
         textcolor ( LIGHTGREEN );
         gotoxy ( 25, 22 );
         printf ( "\aVoce digitou: %s", num );
         Sleep ( 1800 );
         textcolor ( LIGHTGREEN );
         gotoxy ( 25, 23 );
         printf ( "\a%s Nao e Valido !", num );
         Sleep ( 1800 );
         system ( "cls" );
     } else {
         for ( i = 0; i < tam; i++ ) {
              for ( j = 0; j < tam; j++ ) {
                   if ( n == M [ i ] [ j ] ) {
                        textcolor ( LIGHTGREEN );
                        gotoxy ( 26, 21 );
                        printf ( "O valor %d foi encontrado ", n );
                        gotoxy ( 26, 22 );
                        printf ( "Na linha %d e coluna %d ", i, j );
                        getche ( );
                        system ( "cls" );
                   }
              }
         }
     }
     return Matriz_EMbaralhada_Sem_Efeito ( M );
}
void inicio ( int M [ tam ] [ tam ] ) {
     system ( "title GERANDO - IMPRIMINDO E EMBARALHANDO MATRIZ" );
     int i;
     system ( "cls" );
     do {
         Janela5 ( );
         textcolor ( LIGHTRED );
         gotoxy ( 23, 7 );
         printf ( "GERANDO - IMPRIMINDO E EMBARALHANDO MATRIZ" );
         textcolor ( YELLOW );
         gotoxy ( 25, 10 );
         printf ( "Programa desenvolvido por:" );
         textcolor ( LIGHTCYAN );
         gotoxy ( 52, 10 );
         printf ( "Samuel Lima" );
         textcolor ( LIGHTGREEN );
         gotoxy ( 34, 12 );
         printf ( "sa_sp10@hotmail.com" );
         textcolor ( LIGHTBLUE );
         gotoxy ( 24, 14 );
         printf ( "DIGITE    PARA SAIR OU   PARA CONTINUAR  " );
         textcolor ( LIGHTRED );
         gotoxy ( 32, 14 );
         printf ( "1" );
         textcolor ( LIGHTRED );
         gotoxy ( 47, 14 );
         printf ( "2" );
         gotoxy ( 35, 16 );
         scanf ( "%d", &i );
         fflush ( stdin );
         if ( i == 0 ) {
              inicio ( M );
         }
         if ( i == 1 ) {
              textcolor ( LIGHTRED );
              gotoxy ( 30, 20 );
              printf ( "O ROCCO AGRADECE" );
              Sleep ( 1800 );
              exit ( 0 );
         }
         if ( i == 2 ) {
              system ( "cls" );
              Gera_Matriz ( M );
         } else {
              textcolor ( LIGHTBLUE );
              gotoxy ( 37, 20 );
              printf ( "\aOPCAO ERRADA" );
              Sleep ( 1800 );
              system ( "cls" );
         }
     } while ( i );
}
int main ( ) {
     int M [ tam ] [ tam ];
     Janela5 ( );
     inicio ( M );
}