-
Notifications
You must be signed in to change notification settings - Fork 495
/
Copy pathInfixToPostfix.cpp
106 lines (82 loc) Β· 2.39 KB
/
InfixToPostfix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//---------- Infix expression to Postfix ------------//
//#include <iostream>
#include <bits/stdc++.h>
using namespace std;
// to comparare the precedence of operaters
int precedence_comparision(char symbol){
switch(symbol)
{
case '(':
return 0;
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
default:
return -1;
}
}
// converts infix to postfix expression
void InfixtoPostfix(string infix){
// Using cpp built stack , For stack operation
stack<char> expression_stack;
string result;
for(int i=0;i< infix.length();i++){
char symbol=infix[i];
// check the character
//if it is a operand , add to output
if((symbol>='a' && symbol<='z')||(symbol>='0' && symbol<='9')||(symbol>='A' && symbol<='Z')){
result +=symbol;
}
//if it is " ( ", push in stack
else if(symbol == '('){
expression_stack.push('(');
}
//if it is " ) ", pop and output the string upto symbol '(' .
else if(symbol == ')'){
while(expression_stack.top()!='('){
result += expression_stack.top();
expression_stack.pop();
}
expression_stack.pop();
}
//if it is a operator , check precedence and push or pop accordingly
else{
while(!expression_stack.empty() && precedence_comparision(infix[i])<=precedence_comparision(expression_stack.top())){
result+=expression_stack.top();
expression_stack.pop();
}
expression_stack.push(symbol);
}
}
// pop the remaining expression from the stack
while(!expression_stack.empty()){
result+=expression_stack.top();
expression_stack.pop();
}
cout<<result<<endl;
}
// driver program to test InfixtoPostfix function
/*
expression: a^(b*c-d/(e+f))
a^[b*c-d/(e+f)]
a^[b*c-d/[e+f]]
a^[b*c-d/ef+]
a^[bc*-d/ef+]
a^[bc*-def+/]
a^[bc*def+/-]
abc*def+/-^
*/
// Main Function
int main(){
string expression;
cout<<"Enter expression : ";
getline (cin, expression);
//string expression ="a^(b*c-d/(e+f))";
InfixtoPostfix(expression);
return 0;
}