2020年团体程序设计天梯赛 L2-1 简单计算器

思路:

水题,略过

Tip:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

stack<int> s1;
stack<char> s2;

int main() {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        int tmp;
        cin >> tmp;
        s1.push(tmp);
    }
    for (int i = 1; i <= n - 1; i++) {
        char tmp;
        cin >> tmp;
        s2.push(tmp);
    }
    while (!s2.empty()) {
        int n1 = s1.top();
        s1.pop();
        int n2 = s1.top();
        s1.pop();
        char s = s2.top();
        s2.pop();
        int n3;
        if (s == '+') {
            n3 = n2 + n1;
        } else if (s == '-') {
            n3 = n2 - n1;
        } else if (s == '*') {
            n3 = n2 * n1;
        } else if (s == '/') {
            if (n1 == 0) {
                cout << "ERROR: " << n2 << "/" << n1 << endl;
                return 0;
            }
            n3 = n2 / n1;
        }
        s1.push(n3);
    }
    cout << s1.top() << endl;
    return 0;
}

  

posted @ 2020-11-29 15:40  Whiteying  阅读(237)  评论(0编辑  收藏  举报