#include<stdio.h>
#include<stdlib.h>
float add(float a,float b)
{
return a+b;
}
float sub(float a,float b)
{
return a-b;
}
float mul(float a,float b)
{
return a*b;
}
float div2(float a,float b)
{
return a/b;
}
int main(void)
{
float(*operate[])(float,float)={add,sub,mul,div2};
float number_a,number_b;
float result;
int oprate;
printf("input the operator!1 is +,2 is -,3 is *,4 is /\n");
scanf("%d",&oprate);
fflush(stdin);
while(oprate>4||oprate<1)
{
printf("input error!input the operator!1 is +,2 is -,3 is *,4 is /\n");
scanf("%d",&oprate);
fflush(stdin);
}
fflush(stdin);
printf("Input the number_a:");
scanf("%f",&number_a);
fflush(stdin);
printf("Input the number_b:");
scanf("%f",&number_b);
fflush(stdin);
printf("the result is %.3f\n",result=operate[oprate-1](number_a,number_b));
return 0;
}
嗯,转移表是利用C强大的函数指针来搞定的,一直不懂,看了无数的例子,敲了一个建议的加减乘除运算表才有点感觉,必用switch方便多了.尤其是一大串函数的返回值和赋值是相同的时候...