Question: Object-Oriented Programming (C++) A postfix calculator application. Part I Initially, do not any error checking - assume, the input postfix expression is syntactically correct. Input
Object-Oriented Programming (C++)
A postfix calculator application.
Part I
Initially, do not 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, the 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 perform 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 WRITEE 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.
CODES TO GET STARTED:
stackdriveer.cpp
#include
#include
#include "ArrayStack.h"
using namespace std;
int main()
{
ArrayStack
string items[5] = {"one", "two", "three", "four", "five"};
for (int i = 0; i < 5; i++)
{
cout<<"Pushing item \"" << items[i] << "\""<< std::endl;
stack.push(items[i]);
}
cout << endl;
while (!stack.isEmpty())
{
cout << "Top item is \"" << stack.peek() << "\"" << endl;
cout << "Removing top item" << endl;
stack.pop();
}
cout << endl;
cout << "Empty: " << stack.isEmpty() << endl;
} // end driver
stackInterface.h
/** @file StackInterface.h */
#ifndef STACK_INTERFACE_
#define STACK_INTERFACE_
template
class StackInterface
{
public:
/** Sees whether this stack is empty.
@return True if the stack is empty, or false if not. */
virtual bool isEmpty() const = 0;
/** Adds a new entry to the top of this stack.
@post If the operation was successful, newEntry is at the top of the stack.
@param newEntry The object to be added as a new entry.
@return True if the addition is successful or false if not. */
virtual bool push(const ItemType& newEntry) = 0;
/** Removes the top of this stack.
@post If the operation was successful, the top of the stack
has been removed.
@return True if the removal is successful or false if not. */
virtual bool pop() = 0;
/** Returns the top of this stack.
@pre The stack is not empty.
@post The top of the stack has been returned, and
the stack is unchanged.
@return The top of the stack. */
virtual ItemType peek() const = 0;
/** Destroys object and frees memory allocated by object. */
virtual ~StackInterface() { }
}; // end StackInterface
#endif
ArrayStack.h
#ifndef ARRAY_STACK_ #define ARRAY_STACK_ #include "StackInterface.h" const int MAX_STACK = 5; templateclass ArrayStack : public StackInterface { private: ItemType items[MAX_STACK]; // Array of stack items int top; // Index to top of stack public: ArrayStack(); // Default constructor bool isEmpty() const; bool push(const ItemType& newEntry); bool pop(); ItemType peek() const; }; // end ArrayStack /** Listing 7-1 @file ArrayStack.cpp */ #include // For assert template ArrayStack ::ArrayStack() : top(-1) { } // end default constructor // Copy constructor and destructor are supplied by the compiler template bool ArrayStack ::isEmpty() const { return top < 0; } // end isEmpty template bool ArrayStack ::push(const ItemType& newEntry) { bool result = false; if (top < MAX_STACK - 1) // Does stack have room for newEntry? { top++; items[top] = newEntry; result = true; } // end if return result; } // end push template bool ArrayStack ::pop() { bool result = false; if (!isEmpty()) { top--; result = true; } // end if return result; } // end pop template ItemType ArrayStack ::peek() const { assert(!isEmpty()); // Enforce precondition // Stack is not empty; return top return items[top]; } // end peek // End of implementation file. #endif
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
