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 62abce2

Browse files
program to convert infix to prefix made
1 parent 7e6e62e commit 62abce2

File tree

1 file changed

+85
-53
lines changed

1 file changed

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

56
/*Algorithm-
@@ -26,64 +27,95 @@ int getPriority(char c)
2627
return 0;
2728
}
2829

29-
30-
string InfixToPostfix(string infix)
30+
string infixToPostfix(string infix)
3131
{
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;
8179
}
80+
81+
8282

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+
}
83115

84116
int main()
85117
{
86-
string s= InfixToPostfix("A*B+C/D");
87-
cout<<s;
118+
string s = "(A*B+C/D)";
119+
cout<<infixToPostfix(s);
88120
return 0;
89121
}

0 commit comments

Comments
(0)

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