【题意】:输入一个符号(加减乘除) 后边跟上两个操作数。输出结果,如果不是整数,保留两位小数。注意:The result should be rounded to 2 decimal placesIf and only if it is not an integer.需要判断是不是整数,不是全部保留两位小数。
可以对整个字符串分割,或者采用一个char加两个int的输入。
【AC代码】:
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iomanip>
using namespace std;
#define MAX 30
int main()
{
int T = 0;
cin >> T;
getchar();
while (T--)
{
int i = 0, j = 0;
char str[MAX], temp1[MAX], temp2[MAX];
gets(str);
for (i = 2, j = 0; str[i] != ' '; i++)
temp1[j++] = str[i];
temp1[j] = '\0';
for (i++, j = 0; str[i] != ' ' && i < strlen(str); i++)
temp2[j++] = str[i];
temp2[j] = '\0';
int a = atoi(temp1), b = atoi(temp2);
switch(str[0])
{
case '+':
cout << a + b << endl;
break;
case '-':
cout << a - b << endl;
break;
case '*':
cout << a * b << endl;
break;
case '/':
if (a*1.0 / b != a/b)
cout << fixed << setprecision(2) << a*1.0 / b << endl;
else
cout << fixed << setprecision(2) << a / b << endl;
break;
}
}
return 0;
}