关于函数指针求助
#include <stdio.h>#include <stdlib.h>
float sum(float, float);
float sub(float, float);
float mul(float, float);
float divi(float, float);
void func(float (*call)(float, float), float, float, char);
void func(float (*call)(float, float), float num1, float num2, char op)
{
switch(op)
{
case '*' : printf("%.2f %c %.2f = %.2f ", num1, op, num2, call(num1, num2)); break;
case '/' : printf("%.2f %c %.2f = %.2f ", num1, op, num2, call(num1, num2)); break;
case '+' : printf("%.2f %c %.2f = %.2f ", num1, op, num2, call(num1, num2)); break;
case '-' : printf("%.2f %c %.2f = %.2f ", num1, op, num2, call(num1, num2)); break;
}
}
float sum(float num1, float num2)
{
return num1 + num2;
}
float sub(float num1, float num2)
{
return num1 - num2;
}
float mul(float num1, float num2)
{
return num1 * num2;
}
float divi(float num1, float num2)
{
if(num2 >= 0.000001 && num2 <= 0.000001)
{
exit(1);
}
else
{
return num1 / num2;
}
}
int main(void)
{
float num1, num2;
printf("请输入两个实数:");
scanf("%f%f", &num1, &num2);
char op;
printf("\n请输入选择(+-*/):");
op = getchar();
op = getchar();
switch(op)
{
case '*' : func(mul, num1, num2, op);
case '/' : func(divi, num1, num2, op);
case '+' : func(sum, num1, num2, op);
case '-' : func(sub, num1, num2, op);
}
return 0;
}
为什么传入一个函数地址但函数指针访问了四个函数?
页:
[1]