1
1
#include < iostream>
2
2
#include < stack>
3
- #include < algorithm>
4
3
using namespace std ;
5
4
6
5
/* Algorithm-
@@ -11,7 +10,7 @@ using namespace std;
11
10
12
11
bool isOperator (char c)
13
12
{
14
- return (!isdigit (c) && isalpha (c));
13
+ return (!isdigit (c) && ! isalpha (c));
15
14
}
16
15
17
16
int getPriority (char c)
@@ -27,95 +26,76 @@ int getPriority(char c)
27
26
return 0 ;
28
27
}
29
28
30
- string infixToPostfix (string infix)
31
- {
32
-
33
- int l = infix.size ();
34
- stack<char > char_stack;
35
- string output;
36
-
37
- for (int i = 0 ; i < l; i++) {
38
-
39
- // If the scanned character is an
40
- // operand, add it to output.
41
- if (isalpha (infix[i]) || isdigit (infix[i]))
42
- output += infix[i];
43
-
44
- // If the scanned character is an
45
- // ‘(‘, push it to the stack.
46
- else if (infix[i] == ' (' )
47
- char_stack.push (' (' );
48
-
49
- // If the scanned character is an
50
- // ‘)’, pop and output from the stack
51
- // until an ‘(‘ is encountered.
52
- else if (infix[i] == ' )' ) {
53
-
54
- while (char_stack.top () != ' (' ) {
55
- output += char_stack.top ();
56
- char_stack.pop ();
57
- }
58
-
59
- // Remove '(' from the stack
60
- char_stack.pop ();
61
- }
62
-
63
- // Operator found
64
- else {
65
-
66
- if (isOperator (char_stack.top ())) {
67
- while (getPriority (infix[i])
68
- <= getPriority (char_stack.top ())) {
69
- output += char_stack.top ();
70
- char_stack.pop ();
71
- }
72
-
73
- // Push current Operator on stack
74
- char_stack.push (infix[i]);
75
- }
76
- }
77
- }
78
- return output;
79
- }
80
-
81
-
82
29
83
-
84
- string infixToPrefix (string infix)
30
+ string InfixToPostfix (string infix)
85
31
{
86
- /* Reverse String
87
- * Replace ( with ) and vice versa
88
- * Get Postfix
89
- * Reverse Postfix * */
90
- int l = infix.size ();
91
-
92
- // Reverse infix
93
- reverse (infix.begin (), infix.end ());
94
-
95
- // Replace ( with ) and vice versa
96
- for (int i = 0 ; i < l; i++) {
97
-
98
- if (infix[i] == ' (' ) {
99
- infix[i] = ' )' ;
100
- i++;
101
- }
102
- else if (infix[i] == ' )' ) {
103
- infix[i] = ' (' ;
104
- i++;
105
- }
106
- }
107
-
108
- string prefix = infixToPostfix (infix);
109
-
110
- // Reverse postfix
111
- reverse (prefix.begin (), prefix.end ());
112
-
113
- return prefix;
32
+ infix = ' (' + infix + ' )' ;
33
+ int len = infix.size ();
34
+ string output;
35
+
36
+ stack<char >s;
37
+
38
+ for (int i = 0 ; i < len; i++)
39
+ {
40
+ // if operand-add it to the postfix expression
41
+ if (isalpha (infix[i]) || isdigit (infix[i]))
42
+ output += infix[i];
43
+
44
+ // if opening brace then push it to stack
45
+ else if (infix[i]==' (' )
46
+ s.push (infix[i]);
47
+
48
+ else if (infix[i]==' )' )
49
+ {
50
+ while (!s.empty () && s.top () != ' (' )
51
+ {
52
+ output += s.top ();
53
+ s.pop ();
54
+ }
55
+
56
+ // remove ( from stack
57
+ s.pop ();
58
+ }
59
+
60
+ // if operator is found
61
+ else
62
+ {
63
+
64
+ if (isOperator (infix[i]))
65
+ {
66
+ while (!s.empty () && getPriority (infix[i]) <= getPriority (s.top ()))
67
+ {
68
+ output += s.top ();
69
+ s.pop ();
70
+ }
71
+
72
+ // push current lower priority operator on stack
73
+ s.push (infix[i]);
74
+
75
+ }
76
+ }
77
+
78
+ }
79
+
80
+
81
+ // if remaining elements in stack, make it empty
82
+ while (!s.empty ())
83
+ {
84
+ output += s.top ();
85
+ s.pop ();
86
+ }
87
+
88
+
89
+
90
+ return output;
91
+
114
92
}
115
93
94
+
95
+
116
96
int main ()
117
97
{
118
- string s = " (A*B+C/D) " ;
119
- cout<<infixToPostfix (s) ;
98
+ string s= InfixToPostfix ( " (a-b/c)*(a/k-l) " ) ;
99
+ cout<<s ;
120
100
return 0 ;
121
101
}
0 commit comments