Problem
https://www.acmicpc.net/problem/1935
1935번: 후위 표기식2
첫째 줄에 피연산자의 개수(1 ≤ N ≤ 26) 가 주어진다. 그리고 둘째 줄에는 후위 표기식이 주어진다. (여기서 피연산자는 A~Z의 영대문자이며, A부터 순서대로 N개의 영대문자만이 사용되며, 길이
www.acmicpc.net
Solve
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
class sol
{
int N;
string str;
int number[30];
double answer;
stack<double> q;
queue<char> s;
public:
sol(const int& cnt, const string& s)
{
N = cnt;
str = s;
}
void input_data()
{
int n;
for (int i = 0; i < N; i++)
{
cin >> n;
number[i] = n;
}
}
void solution()
{
for (int i = 0; i < str.length();)
{
while (str[i]>='A' && str[i] <='Z')
{
//char cha = str[i++];
int index = str[i++] - 'A';
q.push(number[index]);
}
while (str[i]<'A' || str[i] > 'Z')
{
s.push(str[i++]);
if (i==str.length())
{
break;
}
}
double first;
double second;
double result = q.top();
q.pop();
while (!s.empty())
{
first = result;
//q.pop();
second = q.top();
q.pop();
char c = s.front();
s.pop();
switch (c)
{
case '*':
result = second * first; break;
case '/':
result = second / first; break;
case '+':
result = second + first; break;
case '-':
result = second - first; break;
default:
break;
}
}
q.push(result);
//answer = result;
}
cout << fixed; // 출력 시 소수점 자리 표기
cout.precision(2);
cout << q.top();
}
};
int main()
{
int N;
cin >> N;
string str;
cin >> str;
sol S(N, str);
S.input_data();
S.solution();
return 0;
}
'C++ > 백준 문제풀이' 카테고리의 다른 글
[줄 세우기 - 백준] / vector, iterator(반복자) (0) | 2023.05.16 |
---|---|
[막대기 - 백준] / stack, 자료구 (1) | 2023.05.16 |
C++ [이중 우선순위 큐-백준] / map, key값으로 찾고 value로 중복 갯수 확인 , 반복자 이 (0) | 2023.04.27 |
C++ [비밀번호 찾기_백준] / #map #자료구조 (0) | 2023.04.07 |
C++ [키로거_백준] / #stack #자료구조 (0) | 2023.04.07 |