๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๋ฌธ์ œํ’€์ด๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป

[c++] Infix expression์„ Postfix expression์œผ๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ

by hyerong 2023. 9. 24.

๋ฌธ์ œ

 

Infix expression์„ Postfix expression์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.


 

 

๋ฌธ์ œ ํฌ์ธํŠธ 

1) stack class๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋Š”๊ฐ€

2) stack์˜ ๊ธฐ๋ณธ ์„ฑ์งˆ(push, pop, top_element ๋“ฑ)์„ ์•Œ๊ณ  ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋Š”๊ฐ€

3) bool ํƒ€์ž… + ์กฐ๊ฑด๋ฌธ์ด ๋“ค์–ด๊ฐ„ ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋Š”๊ฐ€ 

4) infix ์™€ postfix๋ฅผ ์ดํ•ดํ•˜๋Š”๊ฐ€ 

5) mainํ•จ์ˆ˜ ์™ธ ๋‹ค๋ฅธ ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ  ์ด๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š”๊ฐ€ 

6) ์‚ฌ์šฉ์ž ์ง€์ • ์šฐ์„ ์ˆœ์œ„๋ฅผ ๋งŒ๋“ค์–ด ์ด๋ฅผ ๋ฐ˜์˜ํ•˜๋Š” ํ•จ์ˆ˜ ์กฐ๊ฑด๋ฌธ์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋Š”๊ฐ€ 

 

 

#include <iostream>
#include <stack>
#define SIZE 100
#define EOS '$'
using namespace std;

class op_stack {
char s[SIZE];
int top;

public:
op_stack();
void push(char x);
char pop();
bool empty();
char top_element();
};
// ์ดˆ๊ธฐํ™”
op_stack:: op_stack() {
top = 0 ;
}

void op_stack:: push(char x) {
s[top] = x;
top++;
}

char op_stack::pop() {
top--;
return(s[top]);
}

bool op_stack::empty() {
return(top == 0);
}

char op_stack::top_element(){
return(s[top-1]);
}

// operand์ธ์ง€ ์•„๋‹Œ์ง€ ํ™•์ธ
bool is_operand(char ch){
if(((ch == '(') || (ch == ')')) || (ch == '+') || (ch == '-') ||
(ch == '*') || (ch == '/') || (ch == '%') || (ch == '$'))
return true;
else
return false;
}

// ์šฐ์„ ์ˆœ์œ„ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜ 0>1>2 -> ๋†’์„์ˆ˜๋ก ๋น ๋ฆ„
// ์ˆซ์ž๊ฐ€ ๋†’์€๊ฒƒ๋ถ€ํ„ฐ ํŒ
int get_precedence(char op) {
if((op == '$') || (op == '(') || (op == ')') ) return (0);
if ((op == '+') || (op == '-')) return (1);
if((op == '*') || (op == '/') || (op == '%')) return(2);

return (-1);
}


// infix -> postfix ํ•จ์ˆ˜
string in_to_post(string input) {
string output = "";
op_stack stack1;

stack1.push(EOS);

for(int i=0; i<input.size(); i++) {
char ch = input[i];

// ํ˜„์žฌ ๋ฌธ์ž๊ฐ€ ๋ฌธ์ž์—ด์ธ ๊ฒฝ์šฐ - output๋ฌธ์ž์—ด์— ์ถ”๊ฐ€
if(!is_operand(ch)) {
output = output + ch;
}
// ๋ถ€ํ˜ธ ์ค‘ ์—ฌ๋Š” ๊ด„ํ˜ธ ์Šคํƒ์— ๋„ฃ๊ธฐ
else if(ch == '(') {
stack1.push(ch);
}
// ๋ถ€ํ˜ธ ์ค‘ ๋‹ซ๋Š” ๊ด„ํ˜ธ
// -> ์Šคํƒ์ด ๋น„์–ด์žˆ์ง€์•Š๊ณ  ํƒ‘ ์š”์†Œ๊ฐ€ ์—ฌ๋Š” ๊ด„ํ˜ธ๊ฐ€ ์•„๋‹ˆ๋ฉด ๋ฌธ์ž์—ด์— ์ถ”๊ฐ€ํ•˜๊ธฐ
else if(ch == ')') {
while(!stack1.empty() && stack1.top_element() != '(') {
output = output + stack1.pop();
}
// -> ์Šคํƒ์ด ๋น„์–ด์žˆ์ง€์•Š๊ณ  ํƒ‘ ์š”์†Œ๊ฐ€ ์—ฌ๋Š” ๊ด„ํ˜ธ๋ฉด ์Šคํƒ1์—์„œ ๋นผ๊ธฐ
if (!stack1.empty() && stack1.top_element() == '(') {
//cout << stack1.pop(); //์ถœ๋ ฅ ํ™•์ธ์šฉ
stack1.pop();
}
}
// ๋ฌธ์ž์—ด๋„ ์•„๋‹ˆ๊ณ  ๊ด„ํ˜ธ๋„ ์•„๋‹ˆ๊ณ  ์ผ๋ฐ˜ ๋ถ€ํ˜ธ์ผ๋•Œ - ์Šคํƒ์ด ๋น„์–ด์žˆ์ง€์•Š์€ ์ƒํƒœ์—์„œ ์šฐ์„ ์ˆœ์œ„๋ฅผ ํ™•์ธํ•˜๊ณ  ์Šคํƒ์— ์ถ”๊ฐ€
else {
while (!stack1.empty() && get_precedence(ch) <= get_precedence(stack1.top_element())){
output = output + stack1.pop();
}
stack1.push(ch);
}
}

// ๋๋‚ ๋•Œ๊นŒ์ง€ ์ถœ๋ ฅ
while(stack1.top_element()!=EOS){
output += stack1.pop();
}

//cout << output ;
// output ๋„˜๊ฒจ์ฃผ๊ธฐ
return output;
}


int main() {

string infix;

cout << "input an infix expression to convert : ";
cin >> infix;

cout << "postfix : " << in_to_post(infix) << endl;

cout << endl;
return 0;

}