1935번: 후위 표기식2
첫째 줄에 피연산자의 개수(1 ≤ N ≤ 26) 가 주어진다. 그리고 둘째 줄에는 후위 표기식이 주어진다. (여기서 피연산자는 A~Z의 영대문자이며, A부터 순서대로 N개의 영대문자만이 사용되며, 길이
www.acmicpc.net
후위 표기식에 대해 알아보자,,
후위표기식 (Postfix expression) 계산법 (tistory.com)
후위표기식 (Postfix expression) 계산법
수학에서 사용되는 사칙연산에는 여러가지 규칙이 있다. 예를들어 곱하기와 나누기는 다른 연산보다 우선시 되어 하고, 괄호가 있는 경우에는 모든 연산의 최우선이 된다. 이러한 규칙들로 인
siyoon210.tistory.com
후위표기식 만들기(스택)
후위표기식 만들기(스택) 중위표기식이 입력되면 후위표기식으로 변환하는 프로그램을 작성하라. 중위표기식은 우리가 흔히 쓰은 표현식입니다. 즉 3+5 와 같이 연산자가 피연산자 사이에 있으
woochan-autobiography.tistory.com
후위표기식은 피연산자를 먼저 다 쓰고, 그 다음에 피연산자가 나오는 형태
ex) 4+3 은 4 3 +
4*7+2 은 4 7 2 + *
3+5*2/(7-2) 는 352*72-/+
숫자가 나오면 스택에 넣고
연산자가 나오면 스택에서 숫자 2개를 꺼내서 계산을 진행해준 뒤 다시 스택에 넣으면 되겠다
처음으로 생겼던 문제는
input이 이렇게 들어오니까
ABC*+DE/-
연산자인 경우, 피연산자인 경우를 나눠야 하는 데
typeid(str[i]).name() 사용하려다가 다 char 나와서 실패
typeid(str[i] -'0').name() 사용하려다가 다 int 나옴
#include <stack>
#include <string>
#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;
float cal(float a, float b, char o){
if(o == '+') return a+b;
else if(o == '-') return a-b;
else if(o == '*') return a*b;
else if(o == '/') return a/b;
}
int main() {
stack<float> s;
string str;
float n, m;
vector<int> v;
cin >> n;
cin >> str;
for(int i=0; i<n; i++){
cin >> m;
v.push_back(m);
}
int j=0;
for(int i=0; i<str.length(); i++){
//피연산자
if(str[i] >= 'A' && str[i] <= 'Z'){
s.push(v[j]);
cout << "push " << v[j] << " ";
j++;
}
//연산자
else {
m = s.top();
s.pop();
n = s.top();
s.pop();
int q = cal(n, m, str[i]);
s.push(cal(n, m, str[i]));
cout << "push " << q << " ";
}
}
// cout << s.top() ;
printf("%.2f\n", s.top());
}
문제점이 안보여서 이 문제만 2시간 째 끄는 중인데
출력이 잘나오는 데 백준은 계속 틀렸다함
#include <stack>
#include <string>
#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;
float cal(float a, float b, char o){
if(o == '+') return a+b;
else if(o == '-') return a-b;
else if(o == '*') return a*b;
else if(o == '/') return a/b;
}
int main() {
stack<float> s;
string str;
float n, m;
vector<float> v(26);
cin >> n;
cin >> str;
for(int i=0; i<n; i++){
cin >> v[i];
}
for(int i=0; i<str.length(); i++){
//피연산자
if(str[i] >= 'A' && str[i] <= 'Z'){
s.push(v[str[i]-'A']);
cout << "push " << v[str[i]-'A'] << " ";
}
//연산자
else {
if(!s.empty()){
m = s.top();
s.pop();
n = s.top();
s.pop();
float q = cal(n, m, str[i]);
s.push(q);
}
}
}
printf("%.2f", s.top());
}
??????????????????
float에서 double로 바꿔주니까 맞았대
....
최종 코드는
#include <stack>
#include <string>
#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;
double cal(double a, double b, char o){
if(o == '+') return a+b;
else if(o == '-') return a-b;
else if(o == '*') return a*b;
else if(o == '/') return a/b;
}
int main() {
stack<double> s;
string str;
double n, m;
vector<double> v(26);
cin >> n;
cin >> str;
for(int i=0; i<n; i++){
cin >> v[i];
}
for(int i=0; i<str.length(); i++){
//피연산자
if(str[i] >= 'A' && str[i] <= 'Z'){
s.push(v[str[i]-'A']);
}
//연산자
else {
if(!s.empty()){
m = s.top();
s.pop();
n = s.top();
s.pop();
double q = cal(n, m, str[i]);
s.push(q);
}
}
}
printf("%.2f", s.top());
}
...^^
'‡ CODING TEST STUDY ‡ > º 백준' 카테고리의 다른 글
[백준 1874번 C++] 스택 수열 (0) | 2023.09.09 |
---|---|
[백준 1406번 C++] 에디터 (0) | 2023.09.09 |
[백준 10773번 C++] 제로 (0) | 2023.09.09 |
[백준 9012번 C++] 괄호 (0) | 2023.09.09 |
[백준 10828번 C++] 스택 (0) | 2023.09.08 |