quinta-feira, 23 de abril de 2015

Ponteiro para função - passando valores


Um estudante avançado em C sabe criar e utilizar ponteiros para todos os tipos de dados( int, char, float, vetor de caracteres), e mais ainda ponteiro para função.Neste exemplo
mostro um dos principais uso de ponteiro para função que é passar uma função como parâmetro para outra.
Trata-se de um recurso poderoso e de grande utilidade em programas mais complexos.
Neste programa colhemos do teclado dois valores do tipo int
por scanf(). na função int Confirma_Num ( const int y, const int x, int ( *Test ) ( const int, const int  ),
int ( *Repete_Num ) ( const int, const int  ) );
e passamos para as funções int Test_Num ( const int y, const int x ); e int Repete_Num ( const int y, const int x );
Observe a chamada da função Confirma_Num ( y, x, Test_Num, Repete_Num ); ela contém na declaração as duas funções.

Veja abaixo imagens do programa em execução:




Veja abaixo o código do programa:

#include <conio.h>
#include <stdio.h>
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 Repete_Num ( const int y , const int x ) {
     system ( "cls" );
     Janela5 ( );
     textcolor ( LIGHTRED );
     gotoxy ( 23 , 3 );
     printf ( "PONTEIRO PARA FUNÇÃO - PASSANDO VALORES" );
     textcolor ( LIGHTBLUE );
     gotoxy ( 20 , 5 );
     printf ( "Função Repete_Num ( const int y, const int x )" );
     textcolor ( LIGHTBLUE );
     gotoxy ( 20 , 7 );
     printf ( "O valor " );
     textcolor ( LIGHTRED );
     printf ( "%d " , y );
     textcolor ( LIGHTBLUE );
     printf ( "e o valor " );
     textcolor ( LIGHTRED );
     printf ( "%d " , x );
     textcolor ( LIGHTBLUE );
     printf ( "também chegaram por aqui" );
     getche ( );
     return 0;
}
int Test_Num ( const int y , const int x ) {
     system ( "cls" );
     Janela5 ( );
     textcolor ( LIGHTRED );
     gotoxy ( 23 , 3 );
     printf ( "PONTEIRO PARA FUNÇÃO - PASSANDO VALORES" );
     textcolor ( LIGHTBLUE );
     gotoxy ( 20 , 5 );
     printf ( "Função Test_Num ( const int y, const int x )" );
     textcolor ( LIGHTBLUE );
     gotoxy ( 20 , 7 );
     printf ( "O valor " );
     textcolor ( LIGHTRED );
     printf ( "%d " , y );
     textcolor ( LIGHTBLUE );
     printf ( "e o valor " );
     textcolor ( LIGHTRED );
     printf ( "%d " , x );
     textcolor ( LIGHTBLUE );
     printf ( "Chegaram por aqui" );
     getche ( );
     return 0;
}
int Confirma_Num ( const int y , const int x ,
         int (*Test) ( const int , const int ) ,
         int (*Repete_Num) ( const int , const int ) ) {
     textcolor ( LIGHTRED );
     gotoxy ( 23 , 3 );
     printf ( "PONTEIRO PARA FUNÇÃO - PASSANDO VALORES" );
     textcolor ( LIGHTBLUE );
     gotoxy ( 23 , 5 );
     printf ( "Digite um número : " );
     textcolor ( LIGHTRED );
     scanf ( "%d" , &y );
     fflush ( stdin );
     textcolor ( LIGHTBLUE );
     gotoxy ( 23 , 7 );
     printf ( "Digite outro número : " );
     textcolor ( LIGHTRED );
     scanf ( "%d" , &x );
     fflush ( stdin );
     if ( ! ( *Test ) ( y , x ) ) {
         textcolor ( LIGHTBLUE );
         gotoxy ( 20 , 11 );
         printf ( "O valor " );
         textcolor ( LIGHTRED );
         printf ( "%d " , y );
         textcolor ( LIGHTBLUE );
         printf ( "e o valor " );
         textcolor ( LIGHTRED );
         printf ( "%d " , x );
         textcolor ( LIGHTBLUE );
         gotoxy ( 20 , 13 );
         printf ( "Estão mesmo na função Test_Num " );
         Sleep ( 1800 );
         textcolor ( YELLOW );
         gotoxy ( 32 , 17 );
         printf ( "PRESSIONE QUALQUER TECLA" );
         getche ( );
         if ( ! ( *Repete_Num ) ( y , x ) )
              textcolor ( LIGHTBLUE );
         gotoxy ( 20 , 11 );
         printf ( "O valor " );
         textcolor ( LIGHTRED );
         printf ( "%d " , y );
         textcolor ( LIGHTBLUE );
         printf ( "e o valor " );
         textcolor ( LIGHTRED );
         printf ( "%d " , x );
         textcolor ( LIGHTBLUE );
         gotoxy ( 20 , 13 );
         printf ( "Estão mesmo na função Repete_Num " );
         Sleep ( 1800 );
         textcolor ( YELLOW );
         gotoxy ( 28 , 17 );
         printf ( "Criado por:" );
         textcolor ( LIGHTRED );
         printf ( " Samuel Lima " );
         textcolor ( LIGHTBLUE );
         gotoxy ( 36 , 23 );
         printf ( "MUITO OBRIGADO" );
         getche ( );
     }
     return ( 0 );
}
int main ( ) {
     system ( "title PONTEIRO PARA FUNÇÃO - PASSANDO VALORES" );
     int y = 0;
     int x = 0;
     Janela5 ( );
     Confirma_Num ( y , x , Test_Num , Repete_Num );
     getche ( );
}

Nenhum comentário:

Postar um comentário

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