|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<stdio.h>
float add(float ,float );
float sub(float ,float );
float cheng(float ,float );
float chu(float ,float );
float calc( float (*fp)(float ,float ),float ,float );
float (*select(char op))(float ,float );
float add(float num1,float num2)
{
return num1 + num2;
}
float sub(float num1,float num2)
{
return num1 - num2;
}
float cheng(float num1,float num2)
{
return num1 * num2;
}
float chu(float num1,float num2)
{
if( num2 != 0 )
{
return num1 / num2;
}else
{
printf("除数为0,无法计算");
}
}
float calc( float (*fp)(float ,float ),float num1,float num2)
{
return (*fp)(num1,num2);
}
float (*select(char op))(float num1,float num2)
{
switch(op)
{
case '+': return add;
case '-': return sub;
case '*': return cheng;
case '/': return chu;
}
}
void main()
{
float num1,num2;
char op;
float (*fp)(float,float);
printf("请输入两个数(1+3)\n");
scanf("%d%c%d",&num1,&op,&num2);
fp = select(op);
printf("%d %c %d = %d\n", num1, op, num2, calc(fp, num1, num2));
}
|
|