#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int add ( int a, int b)
{
return a + b;
}
int sub ( int a, int b)
{
return a - b;
}
int mul ( int a, int b)
{
return a * b;
}
int divide ( int a, int b)
{
if ( 0 == b)
return - 1 ;
return a / b;
}
int main ( void )
{
int ( * fun[ 4 ] ) ( int , int ) = { add, sub, mul, divide } ;
char * buf[ ] = { "add" , "sub" , "mul" , "divide" } ;
int n = sizeof ( fun) / sizeof ( int ( * ) ( int , int ) ) ;
printf ( "n=%d\n" , n) ;
char cmd[ 100 ] ;
int i = 0 ;
while ( 1 )
{
printf ( "plz input cmd:\n" ) ;
scanf ( "%s" , cmd) ;
if ( strncmp ( cmd, "exit" , 4 ) == 0 )
return 0 ;
for ( i = 0 ; i < n; i++ )
{
if ( strcmp ( cmd, buf[ i] ) == 0 )
{
printf ( "--%d--\n" , fun[ i] ( 18 , 6 ) ) ;
break ;
}
}
}
return 0 ;
}
测试结果
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int add ( int x, int y)
{
return x + y;
}
int sub ( int x, int y)
{
return x - y;
}
int mul ( int x, int y)
{
return x * y;
}
float divi ( int x, int y)
{
return x / ( float ) y;
}
int getint ( int x, int y)
{
return x / y;
}
int mod ( int x, int y)
{
return x % y;
}
int main ( int argc, const char * argv[ ] )
{
int ( * ip) ( int x, int y) = NULL ;
float ( * fp) ( int x, int y) = NULL ;
int x, y;
if ( 3 != argc)
{
fprintf ( stderr , "Usage %s <num1> <num2>!\n" , argv[ 0 ] ) ;
return - 1 ;
}
x = atoi ( argv[ 1 ] ) ;
y = atoi ( argv[ 2 ] ) ;
ip = add;
printf ( "%d + %d = %d\n" , x, y, ip ( x, y) ) ;
ip = sub;
printf ( "%d - %d = %d\n" , x, y, ip ( x, y) ) ;
ip = mul;
printf ( "%d * %d = %d\n" , x, y, ip ( x, y) ) ;
fp = divi;
printf ( "%d / %d = %f\n" , x, y, fp ( x, y) ) ;
ip = mod;
printf ( "%d %% %d = %d\n" , x, y, ip ( x, y) ) ;
ip = getint;
printf ( "%d / %d = %d\n" , x, y, ip ( x, y) ) ;
return 0 ;
}
#include <stdio.h>
void fun ( int x) ;
void ( * fun2 ( int x, void ( * ) ( int ) ) ) ( int ) ;
int main ( int argc, const char * argv[ ] )
{
void ( * p) ( int ) = NULL ;
void ( * ( * funp) ( int x, void ( * ) ( int ) ) ) ( int ) = fun2;
int x = 5 ;
p = funp ( x, fun) ;
p ( x) ;
return 0 ;
}
void fun ( int x)
{
printf ( "2x = %d\n" , 2 * x) ;
}
void ( * fun2 ( int x, void ( * funp) ( int ) ) ) ( int )
{
printf ( "x = %d\n" , x) ;
funp ( x) ;
return funp;
}
#include <stdio.h>
#include <string.h>
int add ( int x, int y) ;
int sub ( int x, int y) ;
int mul ( int x, int y) ;
float div ( int x, int y) ;
int getint ( int x, int y) ;
int rem ( int x, int y) ;
int main ( )
{
int x, y, i;
int ( * funp[ 5 ] ) ( int , int ) = { add, sub, mul, getint, rem} ;
float ( * div_p) ( int , int ) = div;
char oprt[ 5 ] = { '+' , '-' , '*' , '/' , '%' } ;
if ( 2 != scanf ( "%d%d" , & x, & y) )
return - 1 ;
for ( i = 0 ; i < 5 ; i ++ )
printf ( "%d %c %d = %d\n" , x, oprt[ i] , y, funp[ i] ( x, y) ) ;
printf ( "%d /%d = %f\n" , x, y, div_p ( x, y) ) ;
return 0 ;
}
int add ( int x, int y)
{
return x + y;
}
int sub ( int x, int y)
{
return x - y;
}
int mul ( int x, int y)
{
return x * y;
}
float div ( int x, int y)
{
return x / ( float ) y;
}
int getint ( int x, int y)
{
return x / y;
}
int rem ( int x, int y)
{
return x % y;
}