Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4be19d7

Browse files
fixed the bug
1 parent 62abce2 commit 4be19d7

File tree

1 file changed

+66
-86
lines changed

1 file changed

+66
-86
lines changed
Lines changed: 66 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include<iostream>
22
#include<stack>
3-
#include<algorithm>
43
using namespace std;
54

65
/*Algorithm-
@@ -11,7 +10,7 @@ using namespace std;
1110

1211
bool isOperator(char c)
1312
{
14-
return (!isdigit(c) && isalpha(c));
13+
return (!isdigit(c) && !isalpha(c));
1514
}
1615

1716
int getPriority(char c)
@@ -27,95 +26,76 @@ int getPriority(char c)
2726
return 0;
2827
}
2928

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-
8229

83-
84-
string infixToPrefix(string infix)
30+
string InfixToPostfix(string infix)
8531
{
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+
11492
}
11593

94+
95+
11696
int main()
11797
{
118-
string s = "(A*B+C/D)";
119-
cout<<infixToPostfix(s);
98+
string s= InfixToPostfix("(a-b/c)*(a/k-l)");
99+
cout<<s;
120100
return 0;
121101
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /