点击(此处)折叠或打开
-
#include<stdio.h>
-
-
double calculator (double a,double b,char operation)//定义计算器需要的变量,a b 以及操作符“+” “-” “*” “/”
-
{
-
if ( operation=='+')//如果操作符为“+”,那么进行如下计算
-
return a+b; //返回到a+b,就是计算出a+b
-
-
else if(operation=='-')
-
return a-b;
-
-
else if(operation=='*')
-
return a*b;
-
-
else if(operation=='/')
-
{
-
if(b==0) //除法中,除数不能为0,在这里要特定一下
-
{
-
printf("除数不能为0\n");
-
return 0;
-
}
-
else
-
{
-
return a/b;
-
}
-
}
-
else
-
{
-
printf("bad operation...\n");
-
return 0;
-
}
-
}
-
-
int main()//调用函数
-
{
-
double a,b;
-
char operation;
-
scanf("%lf%c%lf",&a,&operation,&b);//在键盘上任意获取两个变量与一个操作符
-
printf("%.2lf\n",calculator(a,b, operation));//结果保留了两位小数
-
return 0;
- }
