forked from sahilbansalweb/Hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PostfixToPrefix.cpp
61 lines (48 loc) · 951 Bytes
/
PostfixToPrefix.cpp
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
/*
* Copyright (c) 2021 Aryan Kashyap
*/
#include <bits/stdc++.h>
using namespace std;
bool ValidOperator(char x)
{
switch (x)
{
case '+':
case '-':
case '/':
case '*':
return true;
}
return false;
}
string PostfixToPrefix(string post_exp)
{
stack<string> s;
int length = post_exp.size();
for (int i = 0; i < length; i++)
{
if (ValidOperator(post_exp[i]))
{
string op1 = s.top();
s.pop();
string op2 = s.top();
s.pop();
string temp = post_exp[i] + op2 + op1;
s.push(temp);
}
else
{
s.push(string(1, post_exp[i]));
}
}
return s.top();
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
string post_exp = "HACKT+O*BER/-AK/L-*";
cout << "Prefix : " << PostfixToPrefix(post_exp);
return 0;
}