quarta-feira, 28 de outubro de 2015

Arquivo - Pesquisando e marcando ocorrências

"Como não sabia que aquilo era impossível
fazer, foi lá e fez"



Veja abaixo imagens do programa em execução:


Arquivo usado pelo programa:


                            Concurso Belas da informática
  
                         Veja na lista as dez finalistas

                   Nome             Altura      Peso      Idade
              
                Miriam Lemos        1.64       51.0        18
                Jaque Arantes        1.60       53.0        18
                Silvia Maciel         1.68       61.0        19
                Lúcia Amorim        1.70       65.0        21
                Michele Castro       1.55       56.0        20
                Solange Souza        1.72       73.0        22
                Daniele Martins      1.75       59.0        20
                Márcia Agostin       1.65       66.0        19
                Marisa Pereira        1.66       65.0        21
                Anabelle Lima        1.69       59.0        19


Veja abaixo o código do programa:


#include <stdio.h>
#include <string.h>
#include <conio.h>
#define SI_ZE 800
#define lin  16
#define col 46

static int __BACKGROUND = BLACK;
static int __FOREGROUND = LIGHTGRAY;

void text_color ( int color ) {
     __FOREGROUND = color;
     SetConsoleTextAttribute ( GetStdHandle ( STD_OUTPUT_HANDLE ) ,
              color + ( __BACKGROUND << 4 ) );
}

void Janela ( ) {
     int l, c;
     for ( l = 1; l <= 30 ; l++ ) {
         for ( c = 1; c < 80 ; c++ ) {
              gotoxy ( c , l );
              if ( l == 2 ) {
                   textattr ( 200 );
                   printf ( " " );
              }
              if ( c == 1 ) {
                   textattr ( 200 );
                   printf ( "  " );
              }
              if ( c == 79 ) {
                   textattr ( 200 );
                   printf ( "  " );
              }
         }
     }
}
int main ( ) {
     textbackground ( LIGHTBLUE );
     Janela ( );
     system ( "title ARQUIVO - PESQUISANDO E MARCANDO OCORRÊNCIAS" );
     int a = 0, g = 0, i, j, k, cont = 0, lenv;
     FILE *arq;
     char *pt;
     pt = ( char * ) malloc ( 20 );
     char Nomes [ lin ] [ col ] = { 0 };
     char **line;
     line = ( char** ) malloc ( SI_ZE * sizeof(char*) );
     if ( ( arq = fopen ( "H:\\Eclipse - Indigo\\PROJETOS\\main\\Release\\"
               "Concurso Belas da informática.txt" , "r" ) ) == NULL ) {
     printf ( "Houve um erro na abertura do arquivo" );
         getche ( );
         exit ( 1 );
     }
     textbackground ( WHITE );
     textcolor ( LIGHTRED );
     gotoxy ( 21 , 3 );
     printf ( "ARQUIVO - PESQUISANDO E MARCANDO OCORRÊNCIAS" );
     for ( i = 0; i < lin ; i++ ) {
         line [ i ] = ( char* ) malloc ( SI_ZE );
         fgets ( line [ i ] , 50 , arq );
     }
     for ( i = 0; i < lin ; i++ ) {
         strcpy ( Nomes [ i ] , line [ i ] );
         a = i;
         textcolor ( LIGHTRED );
         gotoxy ( 13 , a + 6 );
         printf ( "%d" , a );
         textcolor ( LIGHTBLUE );
         gotoxy ( 19 , i + 6 );
         printf ( "%s" , line [ i ] );
     }
     fclose ( arq );
     do {
         textcolor ( LIGHTBLUE );
         gotoxy ( 13 , 24 );
         printf ( "Digite uma palavra para procurar ==> " );
         textcolor ( LIGHTRED );
         scanf ( "%20[^\n]s" , pt );
         fflush ( stdin );
         for ( i = 0; i < lin ; i++ ) {
              for ( j = 0; j < col ; j++ ) {
                   k = 0;
                   while ( line [ i ] [ j + k ] == pt [ k ] && pt [ k ] != '\0'
                            && line [ i ] [ j + k ] != '\0' )
                        k++;
                   if ( pt [ k ] == '\0' ) {
                        ++cont;
                        strcpy ( &line [ i ] [ j ] , pt );
                   }
              }
         }
         if ( cont >= 1 ) {
              textcolor ( LIGHTBLUE );
              gotoxy ( 13 , 26 );
              textbackground ( WHITE );
              printf ( "Encontrado " );
              textcolor ( LIGHTRED );
              printf ( "%d" , cont );
              textcolor ( LIGHTBLUE );
              printf ( " ocorrência ( s ) da palavra " );
              textcolor ( LIGHTRED );
              printf ( "%s" , pt );
              Sleep ( 1800 );
              textcolor ( LIGHTRED );
              gotoxy ( 30 , 28 );
              printf ( "PRESSIONE QUALQUER TECLA" );
         }
         if ( cont == 0 ) {
              textcolor ( LIGHTBLUE );
              gotoxy ( 7 , cont + 26 );
              printf ( "Não há Nenhuma ocorrência da palavra " );
              textcolor ( LIGHTRED );
              printf ( "%s" , pt );
              textcolor ( LIGHTBLUE );
              printf ( " no arquivo, tente outra vez" );
              Sleep ( 1800 );
              textcolor ( LIGHTRED );
              gotoxy ( 30 , cont + 28 );
              printf ( "PRESSIONE QUALQUER TECLA" );
              getche ( );
              gotoxy ( 7 , 26 + cont );
              clreol ( );
              gotoxy ( 30 , 28 );
              clreol ( );
              gotoxy ( 13 , 24 );
              clreol ( );
         }
     } while ( cont == 0 );
     getche ( );
     clrscr ( );
     textcolor ( LIGHTRED );
     gotoxy ( 21 , 4 );
     printf ( "ARQUIVO - PESQUISANDO E MARCANDO OCORRÊNCIAS" );
     textcolor ( LIGHTBLUE );
     gotoxy ( 13 , 6 );
     printf ( "Arquivo imprimido com as " );
     textcolor ( LIGHTRED );
     printf ( "%d" , cont );
     textcolor ( LIGHTBLUE );
     printf ( " ocorrências da palavra" );
     textcolor ( LIGHTRED );
     printf ( " %s" , pt );
     textcolor ( LIGHTBLUE );
     printf ( " marcada" );
     lenv = strlen ( pt );
     for ( i = 0; i < lin ; i++ ) {
         gotoxy ( 17 , i + 8 );
         for ( j = 0; j < col ; j++ ) {
              if ( strcmp ( pt , &line [ i ] [ j ] ) == 0 ) {
                   for ( g = 0; g < lenv ; g++ ) {
                        text_color ( YELLOW );
                        printf ( "%c" , line [ i ] [ j ] );
                        j++;
                   }
              }
              textcolor ( BLACK );
              printf ( "%c" , Nomes [ i ] [ j ] );
         }
     }
     Janela ( );
     textbackground ( WHITE );
     textcolor ( LIGHTBLUE );
     gotoxy ( 17 , 25 );
     Sleep ( 1800 );
     printf ( "Por: " );
     textcolor ( LIGHTMAGENTA );
     printf ( "Samuel Lima" );
     textcolor ( BLACK );
     gotoxy ( 17 , 26 );
     printf ( "sa_sp10@hotmail.com" );
     Sleep ( 1800 );
     textcolor ( LIGHTRED );
     gotoxy ( 37 , 28 );
     printf ( "MUITO OBRIGADO" );
     getche ( );
     exit ( 0 );
     free ( line );
     getche ( );
}