sábado, 29 de dezembro de 2012

Pesquisando Valores Em Vetor e Apagando

Este programa é bem interessante, ele usa a variável size para retornar o tamanho do vetor, e como declarei uma variável ponteiro, "*pi", ficou fácil atribuila ao vetor vet [i], no laço for, e imprimi-la com printf. O programa é simples e fácil de entender, más eu mesmo depois de criá - lo, me supreendi com o ótimo funcionamento e interação com o usuário.

Veja algumas imagens do programa em execução:









Eis o código abaixo:

#include <stdio.h>
#include <conio.h>
void Janela5 ( ) {
     int lin, col;
     col = 0;
     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 ( ) {
     int vet [ 5 ] = { 3, 5, 7, 6, 4 };
     int size = 5; // tamanho do vetor
     int lc, num, a, i, *pi;
     system ( "title PESQUISANDO VALORES EM VETOR E APAGANDO" );
     do {
         system ( "cls" );
         Janela5 ( );
         textcolor ( LIGHTRED );
         gotoxy ( 23, 3 );
         printf ( " PESQUISANDO VALORES EM VETOR E APAGANDO" );
         textcolor ( YELLOW );
         gotoxy ( 33, 5 );
         printf ( "sa_sp10@hotmail.com" );
         textcolor ( LIGHTBLUE );
         gotoxy ( 26, 7 );
         printf ( "Veja abaixo os Elementos do Vetor " );
         textcolor ( LIGHTCYAN );
         gotoxy ( 35, 9 );
         for ( pi = vet, i = 0; i < size; i++, pi++ )
              printf ( " %d ", *pi );
         textcolor ( LIGHTGREEN );
         gotoxy ( 15, 11 );
         printf ( "Digite um Número para pesquisar no Vetor ou 0 para sair  " );
         textcolor ( LIGHTRED );
         gotoxy ( 29, 13 );
         scanf ( "%d", &num );
         fflush ( stdin );
         if ( num == 0 ) {
              textcolor ( LIGHTRED );
              gotoxy ( 28, 13 );
              printf ( "   " );
              gotoxy ( 35, 17 );
              printf ( "MUITO OBRIGADO" );
              Sleep ( 1800 );
              exit ( 0 );
         }
         lc = -1;
         for ( i = 0; i < size; i++ ) {
              if ( vet [ i ] == num ) {
                   lc = i;
                   break;
              }
         }
         if ( lc == -1 ) {
              textcolor ( LIGHTMAGENTA );
              gotoxy ( 23, 15 );
              printf ( "\aO Número %d não foi encontrado no vetor", num );
              getche ( );
         } else {
              textcolor ( LIGHTBLUE );
              gotoxy ( 26, 13 );
              printf ( "O Número %d foi encontrado no vetor", num );
              gotoxy ( 26, 14 );
              printf ( "Deseja apagá - lo agora?" );
              do {
                   textcolor ( LIGHTGREEN );
                   gotoxy ( 24, 16 );
                   printf ( "DIGITE  [ 1 PARA SIM, OU 2 PARA SAIR ]" );
                   textcolor ( LIGHTRED );
                   gotoxy ( 26, 18 );
                   scanf ( "%d", &i );
                   fflush ( stdin );
                   if ( i == 1 ) {
                        for ( i = lc; i <= size - 1; i++ )
                            vet [ i ] = vet [ i + 1 ];
                        vet [ size - 1 ] = 0;
                        size = size - 1;
                        a = 1;
                        textcolor ( LIGHTBLUE );
                        gotoxy ( 26, 18 );
                        printf ( "O Número %d foi apagado do vetor", num );
                        gotoxy ( 26, 19 );
                        printf ( "Estes são os Números que ficaram" );
                        textcolor ( LIGHTCYAN );
                        gotoxy ( 35, 21 );
                        for ( i = 0; i < size; i++ ) {
                            printf ( " %d ", vet [ i ] );
                            a++;
                        }
                        getche ( );
                        break;
                   }
                   if ( i == 2 ) {
                        textcolor ( LIGHTRED );
                        gotoxy ( 33, 20 );
                        printf ( "MUITO OBRIGADO" );
                        Sleep ( 1800 );
                        exit ( 0 );
                   } else {
                        textcolor ( LIGHTBLUE );
                        gotoxy ( 26, 18 );
                        printf ( "\aOPÇÃO ERRADA" );
                        Sleep ( 1800 );
                        gotoxy ( 26, 18 );
                        printf ( "            " );
                   }
              } while ( 1 );
         }
     } while ( 1 );
     getche ( );
}