输入两个整数,让用户选择1或2,选一调用max函数,选二调用min函数用指向函数的指针变量
#include<stdio.h>int main() //怎样改才正确
{
int max(int, int); //函数声明
int min(int x, int y); //函数声明
int(*p)(int, int);//定义指向函数的指针变量
int a, b, c, n;
printf("输入a,b");
scanf_s("%d %d", &a, &b);
printf("请选择1或2");
scanf_s("%d", &n);//输入1或2
if (n == 1) p = max;//如输入1,使p指向max函数
else if (n == 2) p = min;
c = (*p)(a, b); //此处报错
printf("a=%d,b=%d\n", a, b);
if (n == 1) printf("max=%d\n", c);
else printf("min=%d", c);
return 0;
}
int max(int x, int y)
{
int z;
if (x > y) z = x;
else z = y;
return z;
}
int min(int x, int y)
{
int z;
if (x < y) z = x;
else z = y;
return z;
}