POJ 2696 计算表达式的值
- 时间限制:
- 1000ms
- 内存限制:
- 65536kB
- 描述
- 有些语言中表达式的运算符使用字符串表示,例如用mul代表*,用div代表/,用add代表+,用sub代表-,用mod代表%。
- 输入
- 第一行为表达式的个数n。其余n行每行一个表达式,表达式由两个整数及其中间的运算符字符串表示。
- 输出
- 输出为n行,每行是对应表达式的值。注意,此处要求的所有运算均为整数运算。
- 样例输入
-
5345 mul 1223945 div 12321 add 343340 sub 211377 mod 27
- 样例输出
-
4209032866412926
(1)、源代码:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int n, a, b;
string str;
cin >> n;
while(n-- > 0)
{
cin >> a >> str >> b;
if(str == "mul")
cout << a*b << endl;
else if(str == "div")
cout << a / b << endl;
else if(str == "add")
cout << a + b << endl;
else if(str == "sub")
cout << a - b << endl;
else if(str == "mod")
cout << a % b << endl;
}
return 0;
}
(2)、解题思路:略
(3)、可能出错:略