-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfixpostfix.cpp
105 lines (90 loc) · 3.39 KB
/
infixpostfix.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
#include <iostream>
#include <stack>
#include <cctype> // for isdigit function
using namespace std;
// Function to check if a character is an operator
bool isOperator(char c) {
return (c == '+' || c == '-' || c == '*' || c == '/');
}
// Function to get the precedence of an operator
int getPrecedence(char op) {
if (op == '+' || op == '-')
return 1;
else if (op == '*' || op == '/')
return 2;
return 0; // Assuming operators are either '+', '-', '*', or '/'
}
// Function to convert infix expression to postfix
string infixToPostfix(const string& infix) {
stack<char> operatorStack;
string postfix;
for (char c : infix) {
if (isalnum(c)) {
postfix += c; // Operand, add to postfix
} else if (c == '(') {
operatorStack.push(c); // Push '(' to stack
} else if (c == ')') {
// Pop operators from stack and add to postfix until '(' is encountered
while (!operatorStack.empty() && operatorStack.top() != '(') {
postfix += operatorStack.top();
operatorStack.pop();
}
operatorStack.pop(); // Remove '(' from stack
} else if (isOperator(c)) {
// Pop operators from stack and add to postfix while precedence is greater or equal
while (!operatorStack.empty() && getPrecedence(operatorStack.top()) >= getPrecedence(c)) {
postfix += operatorStack.top();
operatorStack.pop();
}
operatorStack.push(c); // Push current operator to stack
}
}
// Pop remaining operators from stack and add to postfix
while (!operatorStack.empty()) {
postfix += operatorStack.top();
operatorStack.pop();
}
return postfix;
}
// Function to evaluate postfix expression
int evaluatePostfix(const string& postfix) {
stack<int> operandStack;
for (char c : postfix) {
if (isdigit(c)) {
operandStack.push(c - '0'); // Convert character to integer and push to stack
} else if (isOperator(c)) {
int operand2 = operandStack.top();
operandStack.pop();
int operand1 = operandStack.top();
operandStack.pop();
// Perform the operation and push the result back to the stack
switch (c) {
case '+':
operandStack.push(operand1 + operand2);
break;
case '-':
operandStack.push(operand1 - operand2);
break;
case '*':
operandStack.push(operand1 * operand2);
break;
case '/':
operandStack.push(operand1 / operand2);
break;
}
}
}
return operandStack.top(); // The final result is at the top of the stack
}
int main() {
string infixExpression;
cout << "Enter the infix expression: ";
cin >> infixExpression;
// Convert infix to postfix
string postfixExpression = infixToPostfix(infixExpression);
cout << "Postfix expression: " << postfixExpression << endl;
// Evaluate the postfix expression
int result = evaluatePostfix(postfixExpression);
cout << "Result after evaluation: " << result << endl;
return 0;
}