Question: Object-Oriented Programming (C++) A postfix calculator application. Part I Initially, do not need to do any error checking assume, the input postfix expression is syntactically
Object-Oriented Programming (C++)
A postfix calculator application.
Part I
Initially, do not need to do any error checking assume, the input postfix expression is syntactically correct.
Input
The input is a string that represents a postfix expression. To simplify the problem, calculator will be restricted to operating on single-digit non-negative integers. Only the following characters are allowed in the string:
- the operators '+', '-'. '*', and '/'
- the digits '0' through '9'
- the space (blank) character ' '
Example input:
2 4 3 * +
Processing
To handle spaces in the input, make one minor addition to the algorithm.
if (ch is a blank)
ignore it
end if
Ask the user to enter a postfix expression on a single line. Either read the line one character at a time or can read the line into a string and then process the string one character at a time. The program should then print the value of the expression. The program should include a loop so that the user can evaluate multiple postfix expressions.
OUTPUT:
Enter postfix expression:
2 3 4 + *
The value of the expression is 14
More expressions (Y or N)? Y
Enter postfix expression:
7 2 /
The value of the expression is 3
More expressions (Y or N)? N
Important Note:
The program should perform integer calculations only. Do not use floating-point (float or double) variables for calculations.
Stack code
The application must use the StackArray class template. For the StackArray class template, the class template definition and implementation into a single file.
Important: Increase the maximum size of the stack to something like 32.
Note on the C++ assert statement
The methods in the StackArray class template generally handle error conditions by returning a boolean value indicating success (true) or failure (false). The peek() method cannot do this because it needs to return a copy of the top value on the stack. In this version of the class template, a C++ assert statement is used to signal that the method cannot return a value because the stack is empty. The assert statement will cause the program to abort if this error occurs. This is the way the assert statement works.
Note on writing applications
This program is to write an application. Discard the sample driver and replace it with an application code file. Do not make any modifications to the Stack interface or the StackArray class template.
Hint:
The stack used in the Calculator will need to hold integer values. Convert the digits in the input from characters into integers. One way to convert a character that is a digit to an integer is:
char token;
int value;
if ( token is an digit )
value = token - '0'; // note that the character in single quotes is a zero
Part II
After the calculator is tested, add error checking. Leave the project from Part I as it is and make a copy of everything in a new folder for Part II of the assignment.
Error checking
Add code to check for the following errors:
- Invalid character in the input expression - If this error occurs, print an error message stating that an invalid character was encountered.
- Stack is empty when the algorithm needs to remove an operand - This happens when the expression has too many operators or when the operators and operands are not ordered correctly. We call this a malformed expression. If this error occurs, print an error message indicating that the expression is malformed.
- When the loop in the algorithm ends, there should be exactly one value left on the stack and it is the value of input expression. If the input contained too many operands, there will be more than one value left on the stack. If you remove what should be the result of the expression, the stack should be empty. If it is not, you should print an error message indicating that the expression is malformed..
In all cases, if an error occurs do not try to continue processing the input string and do not try to print the value of the expression. Just print the error message. Do not end the application - just see if the user wants to try another expression.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
