-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp5InfixToPostfix.cpp
More file actions
98 lines (85 loc) · 2.8 KB
/
p5InfixToPostfix.cpp
File metadata and controls
98 lines (85 loc) · 2.8 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
// Design, Develop and Implement a Program for converting an Infix Expression to Postfix Expression. Program should support for both parenthesized and free parenthesized expressions with the operators: +, -, *, /, %(Remainder), ^(Power) and alphanumeric operands.
#include <iostream>
#include <stack>
#include <string>
#include <cctype>
using namespace std;
// Function to return precedence of operators
int precedence(char op)
{
if (op == '+' || op == '-')
return 1;
if (op == '*' || op == '/' || op == '%')
return 2;
if (op == '^')
return 3;
return 0;
}
// Function to check if the character is an operand (alphanumeric)
bool is_operand(char c)
{
return isalnum(c); // Operand is an alphanumeric character
}
// Function to convert infix expression to postfix
string infix_to_postfix(const string &expression)
{
stack<char> operators; // Stack to store operators
string postfix = ""; // Resulting postfix expression
for (int i = 0; i < expression.length(); i++)
{
char current = expression[i];
// If the character is an operand, add it to the result
if (is_operand(current))
{
postfix += current;
}
// If the character is '(', push it to the stack
else if (current == '(')
{
operators.push(current);
}
// If the character is ')', pop from stack to result until '(' is found
else if (current == ')')
{
while (!operators.empty() && operators.top() != '(')
{
postfix += operators.top();
operators.pop();
}
if (!operators.empty())
{
operators.pop(); // Remove '(' from stack
}
}
// If the character is an operator, handle precedence and stack operations
else if (current == '+' || current == '-' || current == '*' || current == '/' || current == '%' || current == '^')
{
while (!operators.empty() && precedence(operators.top()) >= precedence(current))
{
postfix += operators.top();
operators.pop();
}
operators.push(current);
}
}
// Pop all remaining operators in the stack
while (!operators.empty())
{
postfix += operators.top();
operators.pop();
}
return postfix;
}
int main()
{
cout << "ABHISHEK SINGH 2315272/2435222" << endl;
string infix_expression;
// Input the infix expression
cout << "Enter the infix expression: ";
getline(cin, infix_expression);
// Convert to postfix expression
string postfix_expression = infix_to_postfix(infix_expression);
// Output the result
cout << "Postfix expression: " << postfix_expression << endl;
return 0;
}