sexta-feira, 30 de novembro de 2012

Pesquisando string em Vetor

Pesquisar string dentro de um vetor é tarefa muito fácil usando a função "strcmp", da biblioteca string.h;
Esta função faz comparações entre as strings cadastradas no vetor e uma digitada para procura,
retornando 0 se encontra uma string igual. Então defini a constante ENCONTROU para "true", ou "1",
e "NAOENCONTROU" para "false", ou "0". O programa é muito simples, más usei as funções textcolor e gotoxy
para dar um charme perfeito na interface do DOS.
Usei minha IDE padrão, a saber o eclipse, e por aqui não mostrou nenhum warnning.

Então copie cole e compile:

#include <stdio.h>
#include <conio2.h>
#define ENCONTROU 1
#define NAOENCONTROU 0
int main ( );
void Cadastra_Pesquisa_Carros ( ) {
    char Carros [ 5 ] [ 11 ];
    int i = 0, item, a, cadastrar;
    char nome_Carro [ 5 ];
    for ( i = 0; i < 5; i++ ) {
         system ( "cls" );
         textcolor ( LIGHTRED );
         gotoxy ( 25, 3 );
         printf ( "PROGRAMA PESQUISANDO STRING EM VETOR" );
         textcolor ( LIGHTBLUE );
         gotoxy ( 15, 5 );
         printf ( "Digite o nome de um Carro para inserir no vetor %d - ",
                 i + 1 );
         textcolor ( LIGHTMAGENTA );
         scanf ( "%s", Carros [ i ] );
    }
    textcolor ( LIGHTCYAN );
    gotoxy ( 20, 7 );
    printf ( "Estes foram os Carros cadastrados: " );
    textcolor ( YELLOW );
    gotoxy ( 15, 9 );
    for ( i = 0; i < 5; i++ ) {
         printf ( "\n\t\t%s", Carros [ i ] );
    }
    getche ( );
    do {
         system ( "cls" );
         textcolor ( LIGHTRED );
         gotoxy ( 25, 3 );
         printf ( "PROGRAMA PESQUISANDO STRING EM VETOR" );
         textcolor ( LIGHTBLUE );
         gotoxy ( 15, 5 );
         printf ( "Digite o nome de um Carro para procurar no vetor: " );
         textcolor ( LIGHTMAGENTA );
         scanf ( "%s", nome_Carro );
         fflush ( stdin );
         item = NAOENCONTROU;
         for ( i = 0; i < 5; i++ ) {
             a = strcmp ( &Carros [ i ] [ 0 ], nome_Carro );
             if ( a == 0 ) {
                 textcolor ( YELLOW );
                 gotoxy ( 15, 7 );
                 printf ( "O Carro foi encontrado!" );
                 Sleep ( 1800 );
                 system ( "cls" );
                 textcolor ( LIGHTRED );
                 gotoxy ( 25, 3 );
                 printf ( "PROGRAMA PESQUISANDO STRING EM VETOR" );
                 textcolor ( LIGHTBLUE );
                 gotoxy ( 15, 5 );
                 printf ( "Deseja cadastrar outros Carros?" );
                 textcolor ( LIGHTBLUE );
                 gotoxy ( 15, 6 );
                 printf ( "digite [ 1 ] Para Sim ou [ 2 ] para sair  " );
                 textcolor ( LIGHTMAGENTA );
                 scanf ( "%d", &cadastrar );
                 fflush ( stdin );
                 if ( cadastrar == 1 ) {
                     Sleep ( 800 );
                     main ( );
                 }
                 if ( cadastrar == 2 ) {
                     textcolor ( LIGHTCYAN );
                     gotoxy ( 25, 8 );
                     printf ( "O ROCCO AGRADECE" );
                     Sleep ( 1800 );
                     exit ( 0 );
                 }
                 item = ENCONTROU;
                 break;
             }
         }
         if ( item == NAOENCONTROU )
             textcolor ( YELLOW );
         gotoxy ( 15, 7 );
         printf ( "Desculpe, não temos este Carro!" );
         Sleep ( 1800 );
         Sleep ( 800 );
    } while ( 1 );
    getche ( );
}
int main ( ) {
    system ( "title PROGRAMA PESQUISANDO STRING EM VETOR" );
    Cadastra_Pesquisa_Carros ( );
}

Nenhum comentário:

Postar um comentário

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