-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInToPost.cpp
More file actions
114 lines (100 loc) · 2.45 KB
/
InToPost.cpp
File metadata and controls
114 lines (100 loc) · 2.45 KB
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
107
108
109
110
111
112
113
//Program to convert simple mathematical infix expression to postfix expression
#include<iostream>
#include<stack>
#include<string>
using namespace std;
stack<char> s;
bool isOperator(char ch){
/*
Objective: To check whether a character is a mathematic operator.
Input: ch -> A Character to be tested
Output: None
Return value: True, if ch is an operator.
False, if ch is not an operator.
*/
if(ch=='+' || ch=='-' || ch=='*' || ch=='/' || ch=='^')
return true;
else
return false;
}
int precedence(char opr){
/*
Objective: To calculate precedence of an operator.
Input: opr -> A character containing operator.
Output: None
Return value: An integer that denotes the precedence value of the operator.
Higher the operator's precedence, greater will be the integer value.
Description: Operator Precedence value
+,- 1
*,/ 2
^ 3
*/
if(opr == '+' || opr == '-')
return 1;
if(opr == '*' || opr == '/')
return 2;
if(opr == '^')
return 3;
}
void toPostFix(string exp){
/*
Objective: To convert an infix expression to postfix expression.
Input: exp -> An infix expression
Output: A postfix expression equivalent to the input.
Return value: None
Approach: using stack and functions isOperator(...) and precedence(...)
*/
for(int i=0;i<exp.length();i++){
//opr contains the currently scanned character in the expression
char opr = exp.at(i);
if( opr == '('){
//scanned character is an opening bracket '('
s.push('(');
}
else if( opr == ')'){
//scanned character is an closing bracket ')'
while( !s.empty() && s.top()!='('){
cout<<s.top()<<" ";
s.pop();
}
//pop ')' from stack
s.pop();
}
else if(!isOperator(opr)){
//scanned character is an operand
cout<<opr<<" ";
}
else{
//scanned character is an operator
if( s.empty() )
s.push(opr);
else if( precedence(opr) > precedence(s.top()) )
s.push(opr);
else{
while( !s.empty() && s.top()!='(' && precedence(opr) <= precedence(s.top()) ){
cout<<s.top()<<" ";
s.pop();
}
s.push(opr);
}
}
}
while(!s.empty()){
cout<<s.top();
s.pop();
}
}
int main(){
/*
Objective: Take an infix expression input from console and output the postfix expression to the console.
Input parameters: None
Output: Postfix expression
Approach: using function toPostFix()
*/
string exp;
cout<<"\nEnter an infix expression: ";
getline(cin,exp);
toPostFix(exp);
cout<<endl;
return 0;
}