#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#define random(NumberValues) ((int)(((double)rand())*NumberValues)/(RAND_MAX+1.0))
#define iterations 6
#define test
#define testf
#define repeatable //测试的开关
int sum( int x, int y );
int product( int x, int y );
int difference( int x, int y );
int main()
{
int funsel = 0;
int a = 10, b = 5;
int result = 10;
int( *pFun[] )( int, int ) = { sum,product,difference };
#ifdef repeatable
srand( 1 );
#else
srand( (unsigned int) time( NULL ) );
#endif
int element_count = sizeof( pFun ) / sizeof( pFun[0] );
for( int i = 0; i < iterations; i++ )
{
funsel = random( element_count );
if( funsel > element_count - 1 )
{
printf( "\nInvalid array index = %d", funsel );
exit( 1 );
}
#ifdef test
printf( "\nRandom index = %d", funsel );
#endif // test
result = pFun[funsel]( a, b );
printf( "\nresult = %d", result );
}
_getch();
return 0;
}
int sum( int x, int y )
{
#ifdef testf
printf( "\nfunction sum called args %d and %d.", x, y );
#endif // testf
return x + y;
}
int product( int x, int y )
{
#ifdef testf
printf( "\nfunction product called args %d and %d.", x, y );
#endif // testf
return x*y;
}
int difference( int x, int y )
{
#ifdef testf
printf( "\nfunction difference called args %d and %d.", x, y );
#endif // testf
return x - y;
}
测试预编译
最新推荐文章于 2022-10-31 13:19:09 发布