We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 2b97ead commit 94d30c7Copy full SHA for 94d30c7
C++/Postfix_to_Prefix_Conversion.cpp
@@ -0,0 +1,44 @@
1
+#include <bits/stdc++.h>
2
+using namespace std;
3
+bool isOperator(char x)
4
+{
5
+ switch (x) {
6
+ case '+':
7
+ case '-':
8
+ case '/':
9
+ case '*':
10
+ return true;
11
+ }
12
+ return false;
13
+}
14
+string postToPre(string post_exp)
15
16
+ stack<string> s;
17
+ int length = post_exp.size();
18
+ for (int i = 0; i < length; i++) {
19
+ if (isOperator(post_exp[i])) {
20
+ string op1 = s.top();
21
+ s.pop();
22
+ string op2 = s.top();
23
24
+ string temp = post_exp[i] + op2 + op1;
25
+ s.push(temp);
26
27
+ else {
28
+ s.push(string(1, post_exp[i]));
29
30
31
+
32
+ string ans = "";
33
+ while (!s.empty()) {
34
+ ans += s.top();
35
36
37
+ return ans;
38
39
+int main()
40
41
+ string post_exp = "ABC/-AK/L-*";
42
+ cout << "Prefix : " << postToPre(post_exp);
43
+ return 0;
44
0 commit comments