-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinfix_to_postfix.cpp
More file actions
74 lines (63 loc) · 1.31 KB
/
infix_to_postfix.cpp
File metadata and controls
74 lines (63 loc) · 1.31 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
#include<iostream>
#include<bits/stdc++.h>
#include<cstring>
#include<cctype>
using namespace std;
int prec(char c)
{
if (c=='*' || c=='/')
return 2;
else if (c=='+' || c=='-')
return 1;
else
return -1;
}
void infix_to_postfix(string s)
{
std:: stack <char> st;
int i;
char p;
string a;
int l= s.length();
for(i=0;i<l;i++)
{
if(isdigit(s[i]))
{
a+=s[i];
}
else if (s[i]=='(' )
{
st.push(s[i]);
}
else if (s[i]==')')
{
while(st.top()!='(')
{
p=st.top();
st.pop();
a=a+p;
}
if(!st.empty())
{
st.pop();
}
}
else
{
while(prec(s[i]) < prec(st.top()))
{
char ch = st.top();
st.pop();
a+= ch;
}
st.push(s[i]);
}
}
cout<<a<<endl;
}
int main()
{
string exp = "(((2+3*4/8)+5)*6)";
infix_to_postfix(exp);
return 0;
}