Skip to content

Commit e289743

Browse files
committed
Postfix-Prefix to Infix
1 parent 711b7d6 commit e289743

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
#include <iostream>
2+
#include <string>
3+
4+
using namespace std;
5+
6+
const int MAX_SIZE = 100;
7+
8+
struct Stack {
9+
int top; // index of top of stack
10+
string elements[MAX_SIZE];
11+
};
12+
13+
bool isEmpty(Stack& stack) {
14+
if (stack.top == -1) {
15+
return true;
16+
}
17+
return false;
18+
}
19+
20+
bool isFull(Stack& stack) {
21+
if (stack.top == MAX_SIZE - 1) {
22+
return true;
23+
}
24+
return false;
25+
}
26+
27+
void push(Stack& stack, string value) {
28+
if (isFull(stack)) {
29+
//cout << "Stack overflow!" << endl;
30+
return;
31+
}
32+
stack.elements[++stack.top] = value;
33+
}
34+
35+
void pop(Stack& stack) {
36+
if (isEmpty(stack)) {
37+
//cout << "Stack underflow!" << endl;
38+
return;
39+
}
40+
stack.elements[stack.top--] = "";
41+
}
42+
43+
string top(Stack& stack) {
44+
if (isEmpty(stack)) {
45+
// cout << "Stack is empty!" << endl;
46+
return "";
47+
}
48+
return stack.elements[stack.top];
49+
}
50+
51+
bool isOperator(char c) {
52+
return (c == '+' || c == '-' || c == '*' || c == '/');
53+
}
54+
55+
string prefixToInfix(string prefixPhrase) {
56+
Stack stack;
57+
stack.top = -1;
58+
59+
int length = prefixPhrase.length();
60+
61+
for (int i = length - 1; i >= 0; i--) {
62+
char currentChar = prefixPhrase[i];
63+
64+
if (isOperator(currentChar)) {
65+
string operand1 = top(stack);
66+
pop(stack);
67+
68+
string operand2 = top(stack);
69+
pop(stack);
70+
71+
string newPhrase = "(" + operand1 + currentChar + operand2 + ")";
72+
push(stack, newPhrase);
73+
} else {
74+
push(stack, string(1, currentChar));
75+
}
76+
}
77+
78+
return top(stack);
79+
}
80+
81+
string postfixToInfix(string postfixPhrase) {
82+
Stack stack;
83+
stack.top = -1;
84+
85+
int length = postfixPhrase.length();
86+
87+
for (int i = 0; i < length; i++) {
88+
char currentChar = postfixPhrase[i];
89+
90+
if (isOperator(currentChar)) {
91+
string operand2 = top(stack);
92+
pop(stack);
93+
94+
string operand1 = top(stack);
95+
pop(stack);
96+
97+
string newPhrase = "(" + operand1 + currentChar + operand2 + ")";
98+
push(stack, newPhrase);
99+
} else {
100+
push(stack, string(1, currentChar));
101+
}
102+
}
103+
return top(stack);
104+
}
105+
106+
int main() {
107+
string prefixPhrase;
108+
cout << "Enter prefix phrase, for example '+*AB-CD' : ";
109+
cin >> prefixPhrase;
110+
string infixPhrase = prefixToInfix(prefixPhrase);
111+
112+
cout << "Prefix to Infix Phrase: " << infixPhrase << endl;
113+
114+
string postfixPhrase;
115+
cout << "Enter postfix phrase, for example'AB*CD-+' : ";
116+
cin >> postfixPhrase;
117+
infixPhrase = postfixToInfix(postfixPhrase);
118+
119+
cout << "Postfix to Infix Phrase: " << infixPhrase << endl;
120+
121+
return 0;
122+
}
123+
124+
/*
125+
Reza Asadi (Github : RezaGooner)
126+
1402/08/25 ~ 2023/11/16
127+
*/
3 MB
Binary file not shown.

0 commit comments

Comments
 (0)