Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ help: I'm having trouble with this assignment. The places that are marked // TODO is where code should be filled in. Filename: myStack.cpp #include

C++ help: I'm having trouble with this assignment. The places that are marked // TODO is where code should be filled in.

Filename: myStack.cpp

#include

#include "myStack.h"

using namespace std;

/*

* Constructor

* Usage: myStack(maxSz);

* -------------------------

* A new stack variable is initialized. The initialized

* stack is made empty. maxSz is used to determine the

* maximum number of character that can be held in the

* stack.

*/

myStack::myStack(int maxSz) {

// TODO

}

/* Destructor

* Usage: delete ptr

* -----------------------

* This frees all memory associated with the stack.

*/

myStack::~myStack() {

// TODO

}

/*

* Functions: push, pop

* Usage: s1.push(element); element = s1.pop();

* --------------------------------------------

* These are the fundamental stack operations that add an element to

* the top of the stack and remove an element from the top of the stack.

* A call to pop on an empty stack or to push on a full stack

* is an error. Make use of isEmpty()/isFull() (see below)

* to avoid these errors.

*/

void myStack::push(int element) {

// TODO

}

int myStack::pop() {

// TODO

}

/*

* Functions: isEmpty, isFull

* Usage: if (s1.isEmpty()) ...

* -----------------------------------

* These return a true value if the stack is empty

* or full (respectively).

*/

bool myStack::isEmpty() const {

// TODO

}

bool myStack::isFull() const {

// TODO

}

---------------------------------------------------------------

Filename: stackTest.cpp

#include

#include "myStack.h"

using namespace std;

void postfixTest() {

myStack operandStack(100);

cout << "Please enter the operands (integers 1~9) and operators (+, -, *, /) one by one..." << endl;

cout << "and enter '=' to indicate the end of the expression and to output the result." << endl;

while(1){

char inputHolder;

cin >> inputHolder;

// TODO

if(inputHolder == '=')

{

int result = operandStack.pop();

if(operandStack.isEmpty())

cout << "The entered post-fix expression results in " << result << endl;

else

{

cout << "The entered post-fix expression was not a legal one" << endl;

return;

}

}

else if (inputHolder >= '1' && inputHolder <= '9')

{

int thisOperand = inputHolder - '0';

if (!operandStack.isFull())

operandStack.push(thisOperand);

else

{

cout << "Error: The stack is being overloaded" << endl;

return;

}

}

else

{

int operand_1, operand_2;

if (!operandStack.isEmpty())

operand_2 = operandStack.pop();

else

{

cout << "Error: There are not enough operands" << endl;

return;

}

if (!operandStack.isEmpty())

operand_1 = operandStack.pop();

else

{

cout << "Error: There are not enough operands" << endl;

return;

}

switch(inputHolder)

{

case '+':

operandStack.push(operand_1 + operand_2);

break;

case '-':

operandStack.push(operand_1 - operand_2);

break

case '*':

operandStack.push(operand_1 * operand_2)

break;

case '/':

operandStack.push(operand_1/operand_2)

break;

default:

count << "A invalid character has been detected" << endl;

return;

}

}

}

}

int main()

{

cout << "Testing the basic functions of the stack..." << endl;

cout << "Please enter the max capacity of the testStack: ";

int testSize;

cin >> testSize;

myStack testStack(testSize);

cout << "Testing..." << endl;

while(1) {

cout << "Please enter 'p' for push, 'o' for pop, 'e' for exit: ";

char userChoice;

cin >> userChoice;

if(userChoice == 'e')

break;

switch (userChoice)

{

case 'p':

if(!testStack.isFull())

{

cout << "Please enter the integer you would like to push: ";

int userInt;

cin >> userInt;

testStack.push(userInt);

}

else

cout << "Nothing has been pushed in. The stack is full!" << endl;

break;

case 'o':

if(!testStack.isEmpty())

cout << testStack.pop() << " has been popped out" << endl;

else

cout << "Nothing has been popped out. The stack is empty!" << endl;

break;

default:

cout << "Illegal user-input character. Please try again." << endl;

}

}

cout << "Now, start to use a stack to evaluate postfix expressions..." << endl;

postfixTest();

return 0;

}

-----------------------------------------------------

Filename: myStack.h

#ifndef _MYSTACK_H_

#define _MYSTACK_H_

class myStack {

public:

myStack(int maxSz);

~myStack();

void push(int element);

int pop();

bool isEmpty() const;

bool isFull() const;

private:

int *contents; /*Dynamic initiate (C++ keyword new) the holder array*/

int top; /*Index in the array of the top element*/

int maxSize; /*Max number of elements could be in this stack*/

};

#endif

Output example:

Testing the basic functions of the stack... Please enter the max capacity of the testStack: 3 Testing... Please enter 'p' for push, 'o' for pop, 'e' for exit: p Please enter the integer you would like to push: 5 Please enter 'p' for push, 'o' for pop, 'e' for exit: p Please enter the integer you would like to push: 7

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Theory Icdt 99 7th International Conference Jerusalem Israel January 10 12 1999 Proceedings Lncs 1540

Authors: Catriel Beeri ,Peter Buneman

1st Edition

3540654526, 978-3540654520

More Books

Students also viewed these Databases questions

Question

Find the derivative. f(x) 8 3 4 mix X O 4 x32 4 x32 3 -4x - x2

Answered: 1 week ago

Question

b. Explain how you initially felt about the communication.

Answered: 1 week ago