-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinfix_evaluation.cpp
More file actions
186 lines (163 loc) · 5.07 KB
/
Copy pathinfix_evaluation.cpp
File metadata and controls
186 lines (163 loc) · 5.07 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
Question : https://youtu.be/cv_kPZnMxdA?list=PL-Jc9J83PIiFj7YSPl2ulcpwy-mwj1SSk
In this problem it is given that you will be provided with an infix expression and you are required to evaluate and print it's value.You don't need to worry about input; it is already managed for you.
But what is even an infix expression?:
An infix expression is a single letter, or an operator, proceeded by one infix string and followed by another infix string.
x
x + y
(x + y) * (a - b) Basically it's just a fancy word for an algebraic expression which we have been studying since sixth or seventh grade. Only differences will be seen while solving these expressions because in this problem we will be considering the given constraints.
However there are also expressions called prefix and postfix expressions regarding which, we will be solving problems as we move further in this module. And those expressions are not in the form of a simple algebraic expression. We will deal with those problems only, not now.
Sample Input: 2 + 6 * 4 / 8 - 3
Sample Output: 2
You can also watch the question video of this problem to understand WHAT part of the question in a clearer way.
*/
// Solution : https://youtu.be/IY0nZLEg6MA?list=PL-Jc9J83PIiFj7YSPl2ulcpwy-mwj1SSk
// Note : Looks very big but is very very simple
#include <iostream>
#include <fstream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <stack>
#include <queue>
#include <deque>
#include <utility>
#include <unordered_map>
#include <set>
#include <map>
#include <unordered_set>
#include <string>
#include <limits.h>
using namespace std;
#define ll long long int
const int mod = 1e9 + 7;
int getPriority(char op)
{
// This function returns the priority of an operator
if (op == '+' || op == '-')
return 1;
else if (op == '*' || op == '/')
return 2;
else
return 0;
}
bool isOperator(char op)
{
if (op == '+' || op == '-' || op == '*' || op == '/' || op == '(' || op == ')')
return true;
else
return false;
}
int solve(int op1, int op2, char op)
{
if (op == '+')
{
return (op1 + op2);
}
else if (op == '-')
{
return (op1 - op2);
}
else if (op == '*')
{
return (op1 * op2);
}
else
{
return (op1 / op2);
}
}
template <typename T>
void printStack(stack<T> s)
{
//For Debugging
while (!s.empty())
{
cout<<s.top()<<",";
s.pop();
}
cout<<"\n";
}
int main()
{
string exp;
getline(cin, exp);
// Lower priority operators will pop the higher or equal priority operators from stack and will get it solved
// Link of What I am trying to say : https://youtu.be/IY0nZLEg6MA?list=PL-Jc9J83PIiFj7YSPl2ulcpwy-mwj1SSk&t=266
/*
Jab bhi koi operator aata hai to wo khud se bare ya barabar priority wale elements ko khud se pahle
solve karwa deta hai
*/
// Closing bracket tab tak pop karwata hai jab tak opening bracket na mil jaye
stack<int> operand;
stack<char> oprtr;
for (int i = 0; i < exp.length(); i++)
{
if (exp[i] == ' ')
continue;
/*DEBUG :
cout<<"Operand : ";
printStack(operand);
cout<<"Operator : ";
printStack(oprtr);
cout<<"\n";*/
char ch = exp[i];
if (!isOperator(ch))
{
//DEBUG : cout<<"\n("<<ch<<","<<ch-'0'<<")"<<"\n";
operand.push(ch - '0');
}
else
{
//DEBUG : cout<<"\n("<<ch<<","<<ch-'0'<<")"<<"\n";
if (oprtr.empty() || ch == '(')
oprtr.push(ch);
else if (ch=='+'||ch=='-'||ch=='*'||ch=='/')
{
while (!oprtr.empty() && oprtr.top()!='(' && getPriority(ch)<=getPriority(oprtr.top()))
{
int op2 = operand.top();
operand.pop();
int op1 = operand.top();
operand.pop();
char op = oprtr.top();
oprtr.pop();
operand.push(solve(op1,op2,op));
}
oprtr.push(ch);
}
else if (ch == ')')
{
// pop all and solve until you find opening bracket '('
while (oprtr.top() != '(')
{
int op2 = operand.top();
operand.pop();
int op1 = operand.top();
operand.pop();
char op = oprtr.top();
oprtr.pop();
operand.push(solve(op1,op2,op));
}
oprtr.pop();//this will pop the (
}
}
}
while (!oprtr.empty())
{
/*DEBUG :cout<<"Operand : ";
printStack(operand);
cout<<"Operator : ";
printStack(oprtr);
cout<<"\n";
*/
int op2 = operand.top();
operand.pop();
int op1 = operand.top();
operand.pop();
char op = oprtr.top();
oprtr.pop();
operand.push(solve(op1,op2,op));
}
cout << operand.top() << "\n";
}