SPOJ ARITH2

来源:互联网 发布:盒子直播平台源码 编辑:程序博客网 时间:2024/05/17 02:39
//http://www.spoj.com/problems/ARITH2/#include <iostream>#include <queue>#include <cstdlib>using namespace std;int main() {int t;cin >> t;queue<string> numbers;queue<string> operations;long result = 0;bool first = true;while (t-- >0) {string item;cin >> item;operations.empty();numbers.empty();result = 0;first = true;while (item != "=") {if (item == "+" ||item == "-" ||item == "*" ||item == "/") {operations.push(item);} else {numbers.push(item);}cin >> item;}if (item == "=") {while (operations.size() != 0) {string op = operations.front();operations.pop();string opdata1, opdata2;long d1, d2;if (first) {opdata1 = numbers.front();numbers.pop();opdata2 = numbers.front();numbers.pop();d1 = atol(opdata1.c_str());d2 = atol(opdata2.c_str());first = false;} else {d1 = result;opdata2 = numbers.front();numbers.pop();d2 = atol(opdata2.c_str());}if (op == "+") {result = d1 + d2;} else if (op == "-") {result = d1 - d2;} else if (op == "*") {result = d1 * d2;} else {result = d1 / d2;}}cout << result << endl;}}}

0 0
原创粉丝点击