Answered step by step
Verified Expert Solution
Question
1 Approved Answer
You will write a program to evaluate and display a prefix expression using a stack, not recursion. C + + Data Abstration Soluction. It is
You will write a program to evaluate and display a prefix expression using a stack, not recursion. C Data Abstration Soluction.
It is easy to evaluate a prefix expression starting at the end, saving each operand until you encounter an operator, which will operate on the two operands you last encountered saved
You will use the stack ADT provided with the assignment. This ADT was covered in Chapter The files are attached to this assignment.
The expression will contain single integer values and only binary operators
Your main should ask the user to enter a valid prefix expression. You can assume they will enter a valid prefix expression that follows the restrictions. Your main should pass the expression to a function that evaluates and returns the calculated value. The main should display a message for the user.
Ex If the user enters the result should be
If the user enters the result should be
Write your program using a sentinel controlled while loop to continue to read and evaluate prefix expressions until the user enters the sentinel You pick the sentinel value.
C Data Abstration Soluction.
Sample input:
###########ArrayStack.cpp
@file ArrayStack.cpp
#include For assert
#include "ArrayStack.h Header file
template
ArrayStack::ArrayStack : top
end default constructor
Copy constructor and destructor are supplied by the compiler
template
bool ArrayStack::isEmpty const
return top ;
end isEmpty
template
bool ArrayStack::pushconst ItemType& newEntry
bool result false;
if top MAXSTACK Does stack have room for newEntry?
top;
itemstop 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
assertisEmpty; Enforce precondition
Stack is not empty; return top
ItemType value;
if isEmpty
value itemstop;
return value;
end peek
End of implementation file.
###########ArrayStack.cpp
##########ArrayStack.h
@file ArrayStack.h
#ifndef ARRAYSTACK
#define ARRAYSTACK
#include "StackInterface.h
const int MAXSTACK ;
template
class ArrayStack : public StackInterface
private:
ItemType itemsMAXSTACK; Array of stack items
int top; Index to top of stack
public:
ArrayStack; Default constructor
bool isEmpty const;
bool pushconst ItemType& newEntry;
bool pop;
ItemType peek const;
; end ArrayStack
#include "ArrayStack.cpp
#endif
##########ArrayStack.h
#########StackInterface.h
@file StackInterface.h
#ifndef STACKINTERFACE
#define STACKINTERFACE
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 ;
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 pushconst ItemType& newEntry;
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;
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 ;
Destroys object and frees memory allocated by object.
virtual ~StackInterface
; end StackInterface
#endif
#########StackInterface.h
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