Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 4e5ab32

Browse files
authoredOct 30, 2017
Merge pull request #91 from ashish4321/master
adding infix to postfix conversion using stack
2 parents c19e622 + 0705843 commit 4e5ab32

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed
 

‎.DS_Store

6 KB
Binary file not shown.

‎Structures/.DS_Store

6 KB
Binary file not shown.

‎Structures/infixtopostfix.c++

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <stack>
4+
using namespace std;
5+
6+
int precedence(char c)
7+
{
8+
if(c=='^')
9+
return 3;
10+
else if(c=='*' || c=='/')
11+
return 2;
12+
else if(c=='+' || c=='-')infixtopostfix.c++
13+
return 1;
14+
else
15+
return -1;
16+
}
17+
18+
void converttopostfix(string s)
19+
{
20+
stack<char> st;
21+
st.push('N');
22+
int l=s.length();
23+
string ns;
24+
for(int i=0;i<l;i++)
25+
{
26+
if((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z'))
27+
ns=ns+s[i];
28+
else if(s[i] == '(')
29+
st.push('(');
30+
else if(s[i] == ')')
31+
{
32+
while(st.top() != 'N' && st.top() != '(')
33+
{
34+
char c=st.top();
35+
st.pop();
36+
ns=ns+c;
37+
}
38+
if(st.top() == '(')
39+
{
40+
char c=st.top();
41+
st.pop();
42+
}
43+
}
44+
else
45+
{
46+
while(st.top() != 'N' && (precedence(s[i])<=precedence(st.top())))
47+
{
48+
char c=st.top();
49+
st.pop();
50+
ns=ns+c;
51+
}
52+
st.push(s[i]);
53+
}
54+
}
55+
while(st.top() != 'N')
56+
{
57+
char c=st.top();
58+
st.pop();
59+
ns=ns+c;
60+
}
61+
62+
cout<<ns<<endl;
63+
}
64+
65+
int main()
66+
{ string s;
67+
cin>>s;
68+
converttopostfix(s);
69+
return 0;
70+
}

0 commit comments

Comments
 (0)
Please sign in to comment.