[CodeUp] #3127-수식 계산 1(스택)

chrmqgozj·2022년 1월 22일
0

CodeUp

목록 보기
20/48
#include <iostream>
#include <stack>
#include <cstring>
#include <algorithm>
using namespace std;

int main(){
	stack<int> s;
	string in;	
	getline(cin,in);
	
	int temp = 0;
	
	for(int i=0;i<in.size();i++){
		if(in[i]>='0' && in[i]<='9'){
			temp = temp*10+(in[i]-'0');
		}
		else if(in[i] == ' '){
			if(temp!=0){
				s.push(temp);
				// cout << s.top() << "\n";
				temp = 0;	
			}		
		}
		else{
			int x = s.top();
			s.pop();
			int y = s.top();
			s.pop();
			
			if(in[i] == '+'){
				s.push(y+x);
				// cout << s.top() << "\n";
			}
			else if(in[i] == '-'){
				s.push(y-x);
				// cout << s.top() << "\n";
			}
			else{
				s.push(y*x);
				// cout << s.top() << "\n";
			}
		}
	}
	cout << s.top();
	
}

0개의 댓글

관련 채용 정보