Question: Objectives: Students will gain an experience to evaluate arithmetic expressions by stacks Students will gain an experience to handle errors using C + + exceptions
Objectives:
Students will gain an experience to evaluate arithmetic expressions by stacks
Students will gain an experience to handle errors using exceptions
Problem
Task Exercises
Given the following expressions,
use the algorithm learnt in Chapter to convert these expression into the equivalent
fpostfix expression. Illustrate the algorithm step by step in the following table:
Task
Consider simple infix expressions that consist of singledigit operands; the operators,
and ; and the parentheses. Assume that unary operators are illegal and that the
expression contains no embedded spaces. Design and implement a class of infix
calculators. The class should have the following members:
Private members:
a string to store an infix expression for evaluation.
a private member function that checks if an infix expression is well formed.
a private member function that checks for balanced brackets of an expression
using the algorithm studied in Chapter
a private member function that converts an infix expression to a postfix
expression using the algorithm studied in Chapter
a private member function that determines the precedence of an operator
a private member function that evaluates a postfix expression using the algorithm
studied in Chapter
Public members:
a default constructor.
a function that sets the data member by an infix expression. The function must
first check if the infix expression is well formed and balanced with brackets by
calling private member function and before passing it to the data member.
The function returns true if the operation is successfully performed. Otherwise it
returns false to indicate the parameter expression is not valid.a function that evaluates the expression stored in the data member. This function
should not have any parameters. The function should first convert the infix
expression to its postfix form and then evaluate the resulting postfix expression.
Other requirements
Use the linkbased Stack with exceptions.
Write a main function to test your calculators. Your program should allow the
user to evaluate additional expressions until the user wants to end the program.
You should not assume that the expression is wellformed. The user is asked to
reenter an expression if the expression entered is not well formed.
You should not assume that the parentheses in the expression are balanced. The
user is asked to reenter an expression if the expression entered is not balanced.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
