-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstackConversions.cpp
More file actions
215 lines (197 loc) · 5.52 KB
/
stackConversions.cpp
File metadata and controls
215 lines (197 loc) · 5.52 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include <iostream>
#include <string>
#include <stack>
using namespace std;
//check the priority of of the operator
int prior(char s){
if(s=='^'){
return 3;
}else if(s=='*' || s=='/'){
return 2;
}
else if(s=='+' || s=='-'){
return 1;
}else{
return -1;
}
}
// Function to reverse the string
string reverse(string s){
int i = 0;
int j = s.length() - 1;
while (i < j) {
swap(s[i], s[j]);
i++;
j--;
}
return s;
}
string infixToPostFix(string s){
//store the operand
stack<char> st;
//store the answer
string ans;
for(int i=0;i<s.length();i++){
//if it is an operand
if(s[i]>='a' && s[i]<='z' || s[i]>='A' && s[i]<='Z' || s[i]>=0 && s[i]<=9){
ans.push_back(s[i]);
}
//if operand in an opening brackets
else if(s[i]=='('){
st.push('(');
}
//if operand in an closing bracket then pop from the stack and add to the answer till you you get the opening brackets
else if(s[i]==')'){
while(st.top()!='('){
ans.push_back(st.top());
st.pop();
}
st.pop();
}
else{
//now you get an operator
//pop from stack untill the upcoming operator has lesser priority then the top operator present at the top of the stack otherwise just store it in stack means push it
while(!st.empty() && prior(s[i])<=prior(st.top())){
ans.push_back(st.top());
st.pop();
}
st.push(s[i]);
}
}
//means there is no element left on now just push the left element in the ans
while(!st.empty()){
ans.push_back(st.top());
st.pop();
}
return ans;
}
// Function to convert infix to prefix
string infixToPrefix(string s) {
s = reverse(s); // Reverse the input string
stack<char> st;
string ans = "";
// Step 1: Replace '(' with ')' and vice versa
for (int i = 0; i < s.length(); i++) {
if (s[i] == '(') {
s[i] = ')';
} else if (s[i] == ')') {
s[i] = '(';
}
}
// Step 2: Convert to prefix (using a stack for operators)
for (int i = 0; i < s.length(); i++) {
char a = s[i];
// If the character is an operand, add it to the result
if ((a >= 'a' && a <= 'z') || (a >= 'A' && a <= 'Z') || (a >= '0' && a <= '9')) {
ans.push_back(a);
}
// If it's '(', push it to the stack
else if (a == '(') {
st.push(a);
}
// If it's ')', pop from the stack until '(' is found
else if (a == ')') {
while (!st.empty() && st.top() != '(') {
ans.push_back(st.top());
st.pop();
}
st.pop(); // Pop '('
}
// If it's an operator, handle precedence
else {
while (!st.empty() && prior(a) <= prior(st.top())) {
ans.push_back(st.top());
st.pop();
}
st.push(a);
}
}
// Step 3: Pop all remaining operators from the stack
while (!st.empty()) {
ans.push_back(st.top());
st.pop();
}
// Step 4: Reverse the result to get the final prefix expression
s = reverse(ans);
return s;
}
string postfixToInfix(string s) {
stack<string> st;
for (int i = 0; i < s.length(); i++) {
if ((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z') || (s[i] >= '0' && s[i] <= '9')) {
string ans = string(1, s[i]); // Convert character to string
st.push(ans);
} else {
string t1 = st.top();
st.pop();
string t2 = st.top();
st.pop();
string ans = '(' + t2 + s[i] + t1 + ')'; // Combine in infix format
st.push(ans);
}
}
return st.top();
}
string prefixToInfix(string s) {
stack<string> st;
int i = s.length()-1;
while(i>=0){
if ((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z') || (s[i] >= '0' && s[i] <= '9')) {
string ans = string(1, s[i]); // Convert character to string
st.push(ans);
} else {
string t1 = st.top();
st.pop();
string t2 = st.top();
st.pop();
string ans = '(' + t1 + s[i] + t2 + ')'; // Combine in infix format
st.push(ans);
}
i--;
}
return st.top();
}
string postfixToPrefix(string s) {
stack<string> st;
int i = 0;
for(int i=0;i<s.length();i++ ) {
if ((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z') || (s[i] >= '0' && s[i] <= '9')) {
string ans = string(1, s[i]);
st.push(ans);
} else {
string t1 = st.top();
st.pop();
string t2 = st.top();
st.pop();
string ans = string(1, s[i]) + t2 + t1; // Concatenate operator with operands
st.push(ans);
}
}
return st.top();
}
string prefixToPostfix(string s){
stack<string> st;
int i = 0;
for(int i=s.length()-1;i>=0;i-- ) {
if ((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z') || (s[i] >= '0' && s[i] <= '9')) {
string ans = string(1, s[i]);
st.push(ans);
} else {
string t1 = st.top();
st.pop();
string t2 = st.top();
st.pop();
string ans = t1 + t2 + string(1, s[i]) ; // Concatenate operator with operands
st.push(ans);
}
}
return st.top();
}
int main(){
cout<<"Stack conversions"<<endl;
string s = "*-A/BC-/AKL";
string ans = prefixToPostfix(s);
cout<<"Infix string = "<<s<<endl;
cout<<"PostFix string = "<<ans<<endl;
return 0;
}