qfedu-c-level/day9/d12.c

44 lines
715 B
C

// 函数的指针数组
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void fa(int a, int b)
{
printf("%d + %d = %d\n", a, b, a + b);
}
void fb(int a, int b)
{
printf("%d - %d = %d\n", a, b, a - b);
}
void fc(int a, int b)
{
printf("%d * %d = %d\n", a, b, a * b);
}
void fd(int a, int b)
{
printf("%d %% %d = %d\n", a, b, a % b);
}
int main()
{
// 创建一个函数指针数组
// 函数声明: int func(int, int);
void (*funcs[4])(int, int) = {fa, fb};
funcs[2] = fc;
funcs[3] = fd;
srand(time(NULL));
for (int i = 0; i < 4; i++)
{
int x, y;
x = rand() % 100;
y = rand() % 100;
funcs[i](x, y);
}
return 0;
}