L2-033 简单计算器 (25 分)

原题连接
直接上代码

#include<bits/stdc++.h>
using namespace std;
int main() {
    
    
	int cycle;
	cin >> cycle;
	stack<int>s1;
	for (int i = 0; i < cycle;i++) {
    
    
		int temp; cin >> temp;
		s1.push(temp);
	}
	stack<int>s2;
	for (int i = 1; i < cycle;i++) {
    
    
		char temp; cin >> temp;
		s2.push(temp);
	}
	while (!s2.empty()) {
    
    
		char top = s2.top();
		s2.pop();
		int a = s1.top(); s1.pop();  
		int b = s1.top(); s1.pop();
		if (top == '/' && a == 0) {
    
    
			cout << "ERROR: " << b << "/" << a;
				return 0;
		}
		else {
    
    
			if (top == '+') {
    
    
				s1.push(a+b);
			}
			else if (top == '-') {
    
    
				s1.push(b-a);
			}
			else if (top=='*') {
    
    
				s1.push(b*a);
			}
			else if (top=='/') {
    
    
				s1.push(b/a);
			}
		}
	}
	cout << s1.top();
	return 0;
}

猜你喜欢

转载自blog.csdn.net/blastospore/article/details/116032420
今日推荐