// 函数指针应用的好,可以很轻松的解决许多复杂的问题。
#include <iostream>
#define __in
#define __out
using namespace std;
int Add( __in int a, __in int b )
{
cout << ">>>Add function running..." << endl;
return a + b;
}
int Substract( __in int a, __in int b )
{
cout << ">>>Substract function running..." << endl;
return a - b;
}
int Error( int a, int b )
{
cout << ">>>Input error!" << endl;
return 0;
}
int main()
{
typedef int( * ADD_FUNC )( int, int );
ADD_FUNC lpfun = NULL;
for ( int i = 0; i < 5; i++ )
{
cout << "\n-------------" << endl;
cout << "1. Add" << endl;
cout << "2. Substract" << endl;
cout << "which action do you want? ";
int choice = 0;
cin >> choice;
switch ( choice )
{
case 1: lpfun = Add; break;
case 2: lpfun = Substract; break;
default: lpfun = Error;
}
lpfun( 3, 5 );
}
return 0;
}