-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInfixToPostfix.cpp
More file actions
147 lines (141 loc) · 4.02 KB
/
Copy pathInfixToPostfix.cpp
File metadata and controls
147 lines (141 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <iostream>
#include <string>
using namespace std;
const int Size = 50;
struct Stack
{
string arr[Size];
int top = -1;
};
void push(Stack &stack, string value)
{
if (stack.top == Size - 1)
{
cout << "Stack is full" << value << endl;
return;
}
stack.arr[++stack.top] = value;
}
string pop(Stack &stack)
{
if (stack.top == -1)
{
return " ";
}
return stack.arr[stack.top--];
}
string peek(Stack &stack)
{
if (stack.top == -1)
{
return " ";
}
return stack.arr[stack.top];
}
void display(Stack &stack)
{
if (stack.top == -1)
{
cout << "Stack is empty." << endl;
return;
}
cout << "Stack elements From top to bottom:";
for (int i = stack.top; i >=0 ; i--)
{
cout << stack.arr[i] << " ";
}
cout << endl;
}
/*Order of Evaluation:
Parentheses: Operations within parentheses are evaluated first.
Arithmetic Operators: Multiplication, Division, and Modulus are evaluated before Addition and Subtraction.
Comparison Operators: Operators like <, >, <=, >=, ==, and != are evaluated next.
Logical Operators: Logical AND (&&), Logical OR (||), and Logical NOT (!) are evaluated last. */
int precedence(string op){
if (op == "*" || op == "%"|| op == "/")
{
return 4;
}
else if (op == "+" || op == "-")
{
return 3;
}
else if (op == "<" || op == ">" || op == "<=" || op == ">=" || op == "==" || op =="!=")
{
return 2;
}
else if (op == "&&" || op == "||" || op == "!")
{
return 1;
}
else{
return 0;
}
}
void infixToPostfix(Stack &operatorStack, string &postfixExp, string exp)
{
string c;
string character;
for (size_t i = 0; i < exp.length(); i++)
{
c = exp[i];
if ((exp[i] >= 'a' && exp[i] <= 'z') || (exp[i] >= '0' && exp[i]<= '9'))
{
//push the operands in postfix stack
postfixExp += c;
}
else
{
if (exp[i] == '(' || peek(operatorStack) == "(")
{
//directly push "(" or operator after the opening bracket
push(operatorStack,c);
display(operatorStack);
}
else if (exp[i] == ')')
{
//when closing bracket encounters,Pop remaining operators till the opening bracket
while (operatorStack.top > -1 && peek(operatorStack) != "(") {
character = pop(operatorStack);
postfixExp += character;
}
//remove the opening bracket afterwards
pop(operatorStack);
}
else{
//if the operator is "<=",">=","==","!=","&&","||"
if (((exp[i] =='!'|| exp[i] =='<' || exp[i] =='>') && exp[i+1] == '=') || ((exp[i] =='&' && exp[i+1] == '&') || exp[i] == '|' && exp[i+1] == '|'))
{
c+= exp[i+1];
//skip the other character b/c it is part of the operator
i++;
}
//when precendence of the infix exp oerator <= top of operatorStack
while (operatorStack.top > -1 && precedence(c) <= precedence(peek(operatorStack)))
{
character = pop(operatorStack);
postfixExp += character;
}
//when precendence of the infix exp oerator > top of operatorStack || operatorStack is empty
push(operatorStack, c);
display(operatorStack);
}
}
}
// Pop remaining operators when infix expression ends
while (operatorStack.top > -1) {
character = pop(operatorStack);
postfixExp += character;
}
}
int main(int argc, char const *argv[])
{
Stack operatorStack;
string postfixExp;
string exp;
cout << "Enter the infix expression: ";
getline(cin, exp);
infixToPostfix(operatorStack, postfixExp, exp);
cout<<postfixExp<<endl;
return 0;
}