代码示例:
#include <stdio.h>
int main()
{
float a,c,result;
char b;
printf("Please enter a formula:");
scanf("%f%c%f",&a,&b,&c);
switch(b){
case '+':
result = a + c;
break;
case '-':
result = a - c;
break;
case '*':
result = a * c;
break;
case '/':
result = a / c;
break;
case '%':
result = (int)a % (int)c;
break;
}
printf("\nThe result is %.1f", result);
return 0;
}
输入示例:
加法示例:
Please enter a formula:147+612
减法示例:
Please enter a formula:843-431
乘法示例:
Please enter a formula:45*81
除法示例:
Please enter a formula:851/13
取余示例:
Please enter a formula:654%4
输出示例:
加法示例:
Please enter a formula:147+612
The result is 759.0
减法示例:
Please enter a formula:843-431
The result is 412.0
乘法示例:
Please enter a formula:45*81
The result is 3645.0
除法示例:
Please enter a formula:851/13
The result is 65.5
取余示例:
Please enter a formula:654%4
The result is 2.0