Hello, I need help with this c++ program. Arithmetic.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 << "Enter an arithmetic expression on a single line ('q' to end): "; getline(std::cin, expression); if (expression[0] != 'q') { float result = eval(expression); std::cout << expression << " = " << result << std::endl; } } while (expression[0] != 'q'); return 0; } float eval(std::string expr) { // To be implemented // Any number of support functions may also be added std::cout << "eval function not implemented" << std::endl; return 0; } ArrayStack.cpp #include #include "ArrayStack.h" #include "StackException.h" template ArrayStack::ArrayStack() : top(-1) { // top = -1; } //ArrayStack::ArrayStack(const ArrayStack& orig) { //} // //ArrayStack::~ArrayStack() { //} template bool ArrayStack::isEmpty() const { return (top < 0); } template T ArrayStack::peek() const { //If isEmpty(), what do I return? //use precondition, if it fails program halts assert(!isEmpty()); return items[top]; } template bool ArrayStack::pop() { bool result = false; // if (!isEmpty()) { // top--; // result = true; // } // return result; if (!isEmpty()) top--; else throw StackException("Pop called on an empty stack"); return result; } template bool ArrayStack::push(const T& newItem) { bool result = false; if (top < SIZE -1) { top++; items[top] = newItem; result = true; } else { throw StackException("Push called on a full stack"); } return result; } ArrayStack.h #ifndef ARRAYSTACK_H #define ARRAYSTACK_H #include "StackADT.h" const int SIZE = 50; template class ArrayStack : StackADT { public: ArrayStack(); // ArrayStack(const ArrayStack& orig); // virtual ~ArrayStack(); //interface methods bool isEmpty() const; bool push(const T& newItem); bool pop(); T peek() const; private: // static const int SIZE = 50; T items[SIZE]; int top; }; #include "ArrayStack.cpp" #endif /* ARRAYSTACK_H */ Main.cpp #include #include #include "ArrayStack.h" #include "StackException.h" #include using namespace std; /* * */ int main() { ArrayStack intStack; cout << "Beginning " << boolalpha << intStack.isEmpty() << endl; for (int i=1; i<=5; i++) { // try { intStack.push(i); // } catch (StackException e) { // cout << e.what() << endl; // } } cout << "After push " << boolalpha << intStack.isEmpty() << endl;    //check copy constructor ArrayStack numStack(intStack); cout << "Copied item: " << numStack.peek() << endl; intStack.pop(); cout << "Copied item: " << intStack.peek() << endl; cout << "Copied item: " << numStack.peek() << endl; cout << "Contents: "; int sum = 0; while (!intStack.isEmpty()) { cout << intStack.peek() << " "; sum += intStack.peek(); try { intStack.pop(); } catch (StackException e) { cout << e.what() << endl; } } cout << endl << "Sum: " << sum << endl; ArrayStack strStack; strStack.pop(); cout << "Did a problem occur with pop? " << endl; // intStack.peek(); // cout << "Did a problem occur with peek? " << endl; return 0; } StackADT.h #ifndef STACKADT_H #define STACKADT_H template class StackADT { public: virtual bool isEmpty() const = 0; virtual bool push(const T& newItem) = 0; virtual bool pop() = 0; virtual T peek() const = 0; }; #endif /* STACKADT_H */ StackException.cpp #include #include "StackException.h" StackException::StackException(const std::string& msg) : logic_error (msg) { } StackException.h #ifndef PUSHEXCEPTION_H #define PUSHEXCEPTION_H #include #include class StackException : public std::logic_error { public: StackException(const std::string& msg = ""); private: }; #endif /* PUSHEXCEPTION_H */

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Hello, I need help with this c++ program.

Arithmetic.cpp

#include <cstdlib>
#include <iostream>
#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 << "Enter an arithmetic expression on a single line ('q' to end): ";
getline(std::cin, expression);
if (expression[0] != 'q') {
float result = eval(expression);
std::cout << expression << " = " << result << std::endl;
}
} while (expression[0] != 'q');

return 0;
}

float eval(std::string expr)
{ // To be implemented
// Any number of support functions may also be added
std::cout << "eval function not implemented" << std::endl;
return 0;
}

ArrayStack.cpp

#include <cassert>
#include "ArrayStack.h"
#include "StackException.h"

template <class T>
ArrayStack<T>::ArrayStack() : top(-1) {
// top = -1;
}

//ArrayStack::ArrayStack(const ArrayStack& orig) {
//}
//
//ArrayStack::~ArrayStack() {
//}

template <class T>
bool ArrayStack<T>::isEmpty() const {
return (top < 0);
}
template <class T>
T ArrayStack<T>::peek() const {
//If isEmpty(), what do I return?
//use precondition, if it fails program halts
assert(!isEmpty());
return items[top];
}
template <class T>
bool ArrayStack<T>::pop() {
bool result = false;
// if (!isEmpty()) {
// top--;
// result = true;
// }
// return result;
if (!isEmpty())
top--;
else
throw StackException("Pop called on an empty stack");
return result;
}
template <class T>
bool ArrayStack<T>::push(const T& newItem) {
bool result = false;
if (top < SIZE -1) {
top++;
items[top] = newItem;
result = true;
} else {
throw StackException("Push called on a full stack");
}
return result;
}

ArrayStack.h

#ifndef ARRAYSTACK_H
#define ARRAYSTACK_H

#include "StackADT.h"

const int SIZE = 50;

template <class T>
class ArrayStack : StackADT<T> {
public:
ArrayStack();
// ArrayStack(const ArrayStack& orig);
// virtual ~ArrayStack();

//interface methods
bool isEmpty() const;
bool push(const T& newItem);
bool pop();
T peek() const;

private:
// static const int SIZE = 50;
T items[SIZE];
int top;
};

#include "ArrayStack.cpp"
#endif /* ARRAYSTACK_H */

Main.cpp

#include <cstdlib>
#include <iostream>
#include "ArrayStack.h"
#include "StackException.h"
#include <stdexcept>
using namespace std;

/*
*
*/
int main() {

ArrayStack<int> intStack;
cout << "Beginning " << boolalpha << intStack.isEmpty() << endl;

for (int i=1; i<=5; i++) {
// try {
intStack.push(i);
// } catch (StackException e) {
// cout << e.what() << endl;
// }
}
cout << "After push " << boolalpha << intStack.isEmpty() << endl;
  
//check copy constructor
ArrayStack<int> numStack(intStack);
cout << "Copied item: " << numStack.peek() << endl;
intStack.pop();
cout << "Copied item: " << intStack.peek() << endl;
cout << "Copied item: " << numStack.peek() << endl;

cout << "Contents: ";
int sum = 0;
while (!intStack.isEmpty()) {
cout << intStack.peek() << " ";
sum += intStack.peek();
try {
intStack.pop();
} catch (StackException e) {
cout << e.what() << endl;
}
}
cout << endl << "Sum: " << sum << endl;

ArrayStack<string> strStack;
strStack.pop();
cout << "Did a problem occur with pop? " << endl;
// intStack.peek();
// cout << "Did a problem occur with peek? " << endl;

return 0;
}

StackADT.h

#ifndef STACKADT_H
#define STACKADT_H

template <class T>
class StackADT {
public:
virtual bool isEmpty() const = 0;
virtual bool push(const T& newItem) = 0;
virtual bool pop() = 0;
virtual T peek() const = 0;
};

#endif /* STACKADT_H */

StackException.cpp

#include <string>
#include "StackException.h"

StackException::StackException(const std::string& msg) : logic_error (msg) {

}

StackException.h

#ifndef PUSHEXCEPTION_H
#define PUSHEXCEPTION_H

#include <stdexcept>
#include <string>

class StackException : public std::logic_error {
public:
StackException(const std::string& msg = "");

private:

};

#endif /* PUSHEXCEPTION_H */

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.
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 is complete; add Javadoc documentation to either
the StackADT.h or the ArrayStack.h that describes the code written there. The ArrayStack is a template. Therefore,
ArrayStack.cpp is not in the project directory but is in the Files directory and can be accessed from there. The files
directory also contains main.cpp which was used to test the functions in ArrayStack class, if you want to take a look at
it.
The main() function for this project is in Arithmetic.cpp; it 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
O 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.
Transcribed Image Text: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. 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 is complete; add Javadoc documentation to either the StackADT.h or the ArrayStack.h that describes the code written there. The ArrayStack is a template. Therefore, ArrayStack.cpp is not in the project directory but is in the Files directory and can be accessed from there. The files directory also contains main.cpp which was used to test the functions in ArrayStack class, if you want to take a look at it. The main() function for this project is in Arithmetic.cpp; it 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 O 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 run:
Enter an arithmetic expression on a single line ('g' to end) : 5+8
5+8 = 13
Enter an arithmetic expression on a single line ('g' 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 ('g' to end): l+9*2-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) : q
Transcribed Image Text: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 run: Enter an arithmetic expression on a single line ('g' to end) : 5+8 5+8 = 13 Enter an arithmetic expression on a single line ('g' 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 ('g' to end): l+9*2-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) : q
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY