terça-feira, 28 de outubro de 2014

Memchr - Pesquisando números num vetor


Podemos realizar uma eficiente pesquiza num vetor de inteiros, ou até mesmo numa matriz do tipo char, usando esta versátil função: memchr();
cuja sintaxe é: void * memchr ( const void *, int, size_t );
Na verdade só vi um único e simples exemplo mostrando como se pesquisar um caractere numa string pré definida num vetor de caracteres, então resolvi adapta-lo, pra que as pesquisas possam ser feitas também num vetor de inteiros
e fiquei satisfeito com o rendimento do programa.
Acho que não tem método mais simples de se fazer uma pesquisa num vetor,
do que este apresentado neste código, teste e aproveite este ótimo programa.

Veja abaixo imagens do programa em execução:





Veja abaixo o código do programa:



#include <stdio.h>
#include <string.h>
#include <conio.h>
void Janela5 ( ) {
     system ( "Color F0" );
     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 );
}
void inicio ( ) {
     system ( "title MEMCHR - PESQUISANDO NÚMEROS NUM VETOR" );
     int i;
     do {
         Janela5 ( );
         textbackground ( WHITE );
         textcolor ( LIGHTRED );
         gotoxy ( 25, 7 );
         printf ( "MEMCHR - PESQUISANDO NÚMEROS NUM VETOR" );
         textcolor ( BROWN );
         gotoxy ( 25, 10 );
         printf ( "Programa desenvolvido por:" );
         textcolor ( BLACK );
         gotoxy ( 52, 10 );
         printf ( "Samuel Lima" );
         textcolor ( BLUE );
         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 ( 41, 16 );
         scanf ( "%d", &i );
         fflush ( stdin );
         if ( i == 1 ) {
              textcolor ( LIGHTRED );
              gotoxy ( 35, 20 );
              printf ( "MUITO OBRIGADO" );
              Sleep ( 1800 );
              exit ( 0 );
         }
         if ( i == 2 ) {
              system ( "cls" );
              return;
         } else {
              textcolor ( LIGHTRED );
              gotoxy ( 37, 20 );
              printf ( "\aOPÇÃO ERRADA" );
              Sleep ( 1800 );
              system ( "cls" );
         }
     } while ( i );
}
int main ( ) {
     int * pch;
     int c;
     int vet [ 5 ] = { 1, 2, 3, 4, 5 };
     inicio ( );
     do {
         system ( "cls" );
         Janela5 ( );
         textbackground ( WHITE );
         textcolor ( LIGHTRED );
         gotoxy ( 25, 3 );
         printf ( "MEMCHR - PESQUISANDO NÚMEROS NUM VETOR" );
         textcolor ( LIGHTBLUE );
         gotoxy ( 28, 7 );
         printf ( "Digite um Número : " );
         textcolor ( LIGHTRED );
         scanf ( "%d", &c );
         fflush ( stdin );
         textcolor ( BLACK );
         gotoxy ( 28, 9 );
         printf ( "Verificando" );
         textcolor ( LIGHTRED );
         printf ( " ..." );
         Sleep ( 1800 );
         pch = ( int* ) memchr ( vet, c, sizeof ( vet ) );
         if ( pch != NULL ) {
              textcolor ( LIGHTBLUE );
              gotoxy ( 18, 9 );
              printf ( "O Número " );
              textcolor ( LIGHTRED );
              printf ( "%d", c );
              textcolor ( LIGHTBLUE );
              printf ( " foi encontrado na posição " );
              textcolor ( LIGHTRED );
              printf ( "%d", pch - vet );
              textcolor ( LIGHTBLUE );
              printf ( " do vetor" );
              Sleep ( 1800 );
              break;
         } else {
              textcolor ( LIGHTBLUE );
              gotoxy ( 18, 9 );
              printf ( "O Número " );
              textcolor ( LIGHTRED );
              printf ( "%d", c );
              textcolor ( LIGHTBLUE );
              printf ( " não foi encontrado no vetor" );
              Sleep ( 1800 );
              textcolor ( LIGHTBLUE );
              gotoxy ( 28, 11 );
              printf ( "\aTente novamente" );
              Sleep ( 1800 );
              continue;
         }
     } while ( 1 );
     Sleep ( 1800 );
     textcolor ( LIGHTRED );
     gotoxy ( 35, 22 );
     printf ( "MUITO OBRIGADO" );
     Sleep ( 1800 );
     return 0;
}



Nenhum comentário:

Postar um comentário

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