Question
You will demonstrate your understanding of stacks in this exercise. Only 1 program is to be completed. All files require Javadoc documentation comments at the
You will demonstrate your understanding of stacks in this exercise. Only 1 program is to be completed. All files require Javadoc documentation comments at the top to include a description of the program, the @file tag, and an @author tag with your name. Class specification files include complete Javadoc documentation comments for all public members. All code in c++
Write a function that uses an ArrayStack to evaluate a simple arithmetic expression given in string format. The function is to be implemented within the given client. The ArrayStack code will be written in class.
The main() function queries the end user for a string arithmetic expression. The main() function calls the eval() function to evaluate and return the result of the arithmetic expression passed as a parameter. Lastly, the main() function displays the result to the end user. Any number of expressions may be evaluated. The main() function is completed. You are responsible for the implementation of the eval() function.
The eval() function parameter will be a string consisting of a properly formed arithmetic expression using only the values 0 through 9 and + (addition), - (subtraction), * (multiplication), / (division), and/or ^ (exponentiation) operators. The expression will not include decimals, negative values, or values above 9. The expression will not include parenthesis but may include spaces, which should be skipped.
The eval() function needs to use two ArrayStacks: one to store the numeric values and the other to store the operators. When a number is encountered in the string, push it on the values stack. When an operator is encountered in the string, push it on the operators stack if it has a higher operator precedence than the operator currently on the top of the stack. Otherwise, pop an operator off the operator stack, pop two numbers off the values stack, and push the result of the computation onto the values stack. This should be repeated until the operator at the top of the operators stack has a lower precedence than the current operator. Once this point is reached, push the current operator on the operators stack.
Once the end of the arithmetic expression is reached, clear the stacks by popping off an operator from the operators stack, popping two numbers off the values stack, perform the computation and push the result onto the values stack. Continue until the operators stack is empty. The final result will be the remaining value on the values stack. Pop off this value and return it as the result of the eval() function.
You must use an ArrayStack to complete this exercise. Your name should be added to the Javadoc comment block at the top of the client.
Example:
My code:
----Arithemtic.cpp--------------------------------------------------
#include #include #include "ArrayStack.h"
float eval(std::string);
//The following main function should not be modified int main(void) { std::string expression = "Continue"; do{ std::cout
float eval(std::string expr) { //To be implemented //Any number of support functions may also be added std::cout
----ArrayStack.cpp-----------------------------
#include "ArrayStack.h" #include
template ArrayStack::ArrayStack() : top(-1) {
}
//ArrayStack::ArrayStack(const ArrayStack& orig) { // //} // //ArrayStack::~ArrayStack() { // //}
template bool ArrayStack::isEmpty() const { return (top
template bool ArrayStack::pop() { bool result = false; if (!isEmpty()) { top--; result = true; } return result; }
template bool ArrayStack::push(const T& anItem) { bool result = false; if (top
template T ArrayStack::peek() const { assert(!isEmpty()); return items[top]; }
---StackADT.cpp---------------------------------
#ifndef STACKADT_H #define STACKADT_H
template class StackADT { public: /** * This is the Empty method * @returns bool */ virtual bool isEmpty() const = 0; /** * This is the peek method * @returns T */ virtual T peek() const = 0; /** * This is the pop method * @returns bool */ virtual bool pop() = 0; /** * This is the push method * @param anItem * @returns bool */ virtual bool push(const T& anItem) = 0; private: }; #endif /* STACKADT_H */
----ArracyStack.h-------------------------------------
#ifndef ARRAYSTACK_H #define ARRAYSTACK_H
#include "StackADT.h"
int const SIZE = 10;
template class ArrayStack : public StackADT { public: ArrayStack(); //ArrayStack(const ArrayStack& orig); //virtual ~ArrayStack(); //interface methods bool isEmpty() const; T peek() const; bool pop(); bool push(const T& anItem); //other methods private: //static int const SIZE = 10; int top; T items[SIZE]; //TODO make size a constant }; #include "ArrayStack.cpp" #endif /* ARRAYSTACK_H */
---------------------------------
Please help!!!!!!
Enter an arithmetic expression on a single line ('q' to end) 5+8 5+8 = 13 Enter an arithmetic expression on a single line ('q' to end) 2+1-6 2+1-6 =-3 Enter an arithmetic expression on a single line ('q' to end) 5-4/2 5-4/2 = 3 Enter an arithmetic expression on a single line ('q' to end) 1+92-4 1+9*2-4 = 15 Enter an arithmetic expression on a single line ('q' to end)2-1+5 2/2 2-1+5*2/2 = 6 Enter an arithmetic expression on a single line ('q' to end)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