Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MUST CREATE TWO CLASS BOTH CALCULATOR AND STACK CLASS, CAN NOT USE THE STACK LIB

ID: 3592568 • Letter: M

Question

MUST CREATE TWO CLASS BOTH CALCULATOR AND STACK CLASS, CAN NOT USE THE STACK LIBRARY CLASS.

Write a program in c++ that reads an infix expression, converts it to a postfix expression, evaluates the postfix expression, and prints out the answer. You must define and implement your own Stack class and a Calculator class (feel free to add additional classes). Your Stack class supports standard basic stack operations and you can implement it with an array or a linked list. You should create a class template Stack in C++ . Your Calculator class stores an infix expression and it should provide operations to return an equivalent postfix expression as well as an integer result of the expression. The Calculator class uses a character stack to convert infix expression to postfix expression (storing operators) and then uses an integer stack to evaluate the postfix expression (storing operands). Your application (main) should perform the following: Ask the user to input an infix expression. Examples of an infix expressions might be (you may assume that tokens are always separated by one space): 17 / ( 2 + 3 ) - 13 5 * 2 ^ 3 Create and use a Calculator object to perform conversion and evaluation Obtain and print out the postfix expression (examples above) 17 2 3 + / 13 - 5 2 3 ^ * Obtain and print out the answer of the evaluation (examples above) -10 40 Your program must be able to work with int operands and it must recognize binary operators +, - , *, / , %, and ^. In addition, it must recognize the parenthesis: ( ). You can assume that the user will input a valid infix expression. Your program will continue to process expressions until the user enters 0 as an expression (use a sentinel loop)

Explanation / Answer

#include #include #include using namespace std; /* The program will evaluate a postfix expression that contains digits and operators. The program tries to simulate the microprocessor execution stack or evaluation of expression. */ //The class performing the evaluation class Evaluation { public: int st[50]; int top; char str[50]; Evaluation() { top = -1; } //function to push the item void push(int item) { top++; st[top] = item; } //function to pop an item int pop() { int item = st[top]; top--; return item; } //function to perform the operation depending on the operator. int operation(int a,int b,char opr) { switch(opr) { case '+':return a+b; case '-':return a-b; case '*':return a*b; case '/':return a/b; default: return 0; } } int calculatePostfix(); }; //This is the function that calculates the result of postfix expression. int Evaluation::calculatePostfix() { int index = 0; while(str[index]!='#') { if(isdigit(str[index])) { push(str[index]-'0'); } else { int x = pop(); int y = pop(); int result = operation(x,y,str[index]); push(result); } index++; } return pop(); } /* main function that reads the postfix expression and that prints the result. The input expression should be ending with a number An example input expression would be: 123*+ Its output will be 7. */ int main() { void clrscr(); Evaluation eval; cout > eval.str; int result = eval.calculatePostfix(); cout
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote