File tree Expand file tree Collapse file tree 3 files changed +70
-0
lines changed Expand file tree Collapse file tree 3 files changed +70
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments