https://www.acmicpc.net/problem/1935
후위표기식이란?
스택 활용
출력 형식 보기
#include <stdio.h>
#include <stack>
#include <string.h>
#include <cmath>
using namespace std;
int main(){
int n;
scanf("%d", &n);
char expression[110];
scanf("%s", expression);
int len = strlen(expression);
double num[30];
for (int i = 0; i < n; i++)
scanf("%lf", &num[i]);
stack <double> s;
for (int i = 0; i < len; i++) {
if (expression[i] == '+') {
double n1 = s.top();
s.pop();
double n2 = s.top();
s.pop();
s.push(n2 + n1);
}
else if (expression[i] == '-') {
double n1 = s.top();
s.pop();
double n2 = s.top();
s.pop();
s.push(n2 - n1);
}
else if (expression[i] == '*') {
double n1 = s.top();
s.pop();
double n2 = s.top();
s.pop();
s.push(n2 * n1);
}
else if (expression[i] == '/') {
double n1 = s.top();
s.pop();
double n2 = s.top();
s.pop();
s.push(n2 / n1);
}
else {
int index = expression[i] - 65;
s.push(num[index]);
}
}
printf("%0.2lf", round(s.top() * 100) / 100);
return 0;
}