1
1
#include < iostream>
2
2
#include < stack>
3
+ #include < algorithm>
3
4
using namespace std ;
4
5
5
6
/* Algorithm-
@@ -26,64 +27,95 @@ int getPriority(char c)
26
27
return 0 ;
27
28
}
28
29
29
-
30
- string InfixToPostfix (string infix)
30
+ string infixToPostfix (string infix)
31
31
{
32
- infix = ' (' + infix + ' )' ;
33
- string output=" " ;
34
- int len = infix.size ();
35
- stack<char >s;
36
-
37
- for (int i = 0 ; i < len; i++)
38
- {
39
- // if operand-add it to the postfix expression
40
- if (isalpha (infix[i]) || isdigit (infix[i]))
41
- output += infix[i];
42
-
43
- // if opening brace then push it to stack
44
- else if (infix[i]==' (' )
45
- s.push (infix[i]);
46
-
47
- else if (infix[i]==' )' )
48
- {
49
- while (s.top ()!=' (' )
50
- {
51
- output += s.top ();
52
- s.pop ();
53
- }
54
-
55
- // remove ( from stack
56
- s.pop ();
57
- }
58
-
59
- // if operator is found
60
- else
61
- {
62
-
63
- if (isOperator (s.top ()))
64
- {
65
- while ( getPriority (infix[i]) <= getPriority (s.top ()))
66
- {
67
- output += s.top ();
68
- s.pop ();
69
- }
70
-
71
- // push current lower priority operator on stack
72
- s.push (infix[i]);
73
-
74
- }
75
- }
76
-
77
- }
78
-
79
- return output;
80
-
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;
81
79
}
80
+
81
+
82
82
83
+
84
+ string infixToPrefix (string infix)
85
+ {
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;
114
+ }
83
115
84
116
int main ()
85
117
{
86
- string s= InfixToPostfix ( " A*B+C/D" ) ;
87
- cout<<s ;
118
+ string s = " ( A*B+C/D) " ;
119
+ cout<<infixToPostfix (s) ;
88
120
return 0 ;
89
121
}
0 commit comments