Question
Develop c++ program to convert infix to postfix and evaluate the postfix expression: 1. Must use stack class created in different file(.h file and .cpp
Develop c++ program to convert infix to postfix and evaluate the postfix expression:
1. Must use stack class created in different file(.h file and .cpp file) and NOT #include
2.Priority =
*, /
+, -
(
3) psudocode to implement:
Initialize an empty stack (for operators)
While not_finished parsing the expression
{obtain the next input token in the expression;
switch (token)
{case : break; //space
case (: push; break;
case ): pop & display popped element until ( is encountered;
//do not display the (
break;
case +, -, *, /, %: //( and ) are not considered as operators
if empty_stack or priority(token) > priority(stack_top)
push;
else
pop & display popped element until empty_stack or until
priority(token)<=priority(stack_top)
push
break;
case operand: display;
break;
} }
pop & display the rest of the stack elements; //do not display the (
}
here is what i have so far but not working:
//charstack.h
#pragma once #define CAPACITY 128 typedef int StackElement; class Charstack { StackElement stackArray[CAPACITY]; int stack_size; public: Charstack(); bool empty(); void push(StackElement item); StackElement pop(); StackElement top(); void display(); };
//charstack.cpp
#include "stdafx.h" #include using namespace std; #include "Charstack.h" #define CAPACITY 128 typedef int StackElement;
Charstack::Charstack() { stack_size = 0; } bool Charstack::empty() { if (stack_size == 0) return true; else return false; } StackElement Charstack::top() { if (stack_size >= 0) return (stackArray[stack_size]); else { cout << "Stack is empty "; } }
void Charstack::push(StackElement item) { if (stack_size == CAPACITY) cout << "stack is full "; else { stackArray[stack_size] = item; stack_size++; } } StackElement Charstack::pop() { stack_size--; if (stack_size >= 0) return stackArray[stack_size]; else return '\0'; } void Charstack::display() { for (int i = stack_size - 1; i >= 0; i--) cout << stackArray[i] << " "; }
//main program
#include "stdafx.h" #include using namespace std; #include #include"Charstack.h"
//double compute(string postfix);//to convert string infixToPostFix(string exp); bool Operator(char x); int Priority(char oper); int rightParenthense(char oper); int higherPrecedence(char oper1, char oper2);
int main() { string expression = "1*3+4/3"; string postFix;
cout << "infix expression is: " << expression << " ";
postFix = infixToPostFix(expression);
cout << "Postfix expression is: " << postFix << " ";
//cout << "Compute answer for the postfix expression is " << compute(postFix(infixExp)) << " ";
return 0; }
string infixToPostFix(string expression) { string postfix_exp = ""; Charstack opStack;//char stack
for (int i = 0; i < expression.length(); i++) { if (expression[i] == ' ')continue;
else if (Operator(expression[i])) { while (!opStack.empty() && opStack.top() != '(' && higherPrecedence(opStack.top(), expression[i])) { postfix_exp += opStack.top(); opStack.pop(); } opStack.push(expression[i]); }
else if (Operator(expression[i])) { postfix_exp += expression[i]; }
else if (expression[i] == '(') { opStack.push(expression[i]); }
else if (expression[i] == ')') { while (!opStack.empty() && opStack.top() != '(') { postfix_exp += opStack.top(); opStack.pop(); } opStack.pop(); } } while (!opStack.empty()) { postfix_exp += opStack.top(); opStack.pop(); }
return postfix_exp; }
//double compute(string postfix) //{
//}
bool Operator(char x) { if (x == '+' || x == '-' || x == '*' || x == '/' || x == '$') return true;
return false;
}
int Priority(char oper) { int preced = -1; switch (oper) { case '+': case '-': return preced = 1; break; case '*': case '/': return preced = 2; break; case '(': return preced = 3; break; } }
int rightParenthense(char oper) { if (oper == '(') return true; return false;
}
int higherPrecedence(char oper1, char oper2) { int op1Priority = Priority(oper1); int op2Priority = Priority(oper2);
if (op1Priority == op2Priority) { if (rightParenthense(op1Priority))return false; else return true; }
else if (op1Priority > op2Priority) return true;
else return false;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started