Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For this project, you will complete the provided partial C++ program that utilizes classes to implement a stack. The MiniStack class is an array-based implementation

For this project, you will complete the provided partial C++ program that utilizes classes to implement a

stack. The MiniStack class is an array-based implementation of the Stack Abstract Data Type (ADT)

where the array size is limited to five elements of type char. MiniStack objects are used by BigStack to

create a doubly-linked-node, stack-of-MiniStacks implementation of the Stack ADT. So with this project,

you practice implementing both an array-based stack (MiniStack) and a linked-node stack (BigStack).

All i need is MiniStack.cpp and BigStack.cpp files.

***********************************************main.cpp*********************************************************************

#include

#include

#include

#include

#include "ministack.h"

#include "bigstack.h"

using namespace std;

void Bar(int n); // Prototype for Bar function

int main(int argc, char* argv[])

{

ifstream inputs; // Input file for commands

char op, ch; // Hold operation and optional char input

BigStack* bStackPtr = NULL; // Will point to BigStack object

MiniStack* mStackPtr = NULL; // Will point to MiniStack object

string comment; // Stores comment read from input file

// Output usage message if one input file name is not provided

if (argc != 2)

{

cout << "Usage: program03 ";

return 1;

}

// Attempt to open input file -- terminate if file does not open

inputs.open(argv[1]);

if (!inputs)

{

cout << "Error - unable to open input file" << endl;

return 1;

}

Bar(60); // Output long bar

// Process comment line from input file

getline(inputs, comment); // Input file header comment

cout << endl << comment << endl << endl; // Output file header comment

// Process commands from input file

inputs >> op; // Attempt to input first command

while (inputs)

{

switch (op)

{

case '#': // Test file comment

getline(inputs, comment); // Input and echo the comment appearing in the test file

cout << '#' << comment << endl;

break;

case '~': // Print Bar

Bar(40); // Output short bar

break;

case 'c': // Constructor

inputs >> op;

try

{

if (op == 'B')

{

cout << "BigStack::BigStack() -- Status = ";

bStackPtr = new BigStack;

cout << "Completed" << endl;

}

else if (op == 'M')

{

cout << "MiniStack::MiniStack() -- Status = ";

mStackPtr = new MiniStack;

cout << "Completed" << endl;

}

}

catch ( std::bad_alloc )

{

cout << "Failed : Terminating now..." << endl;

return 1;

}

break;

case '+': // Push

inputs >> ch;

try

{

if (bStackPtr != NULL)

{

cout << "BigStack::Push('" << ch << "') -- Status = ";

}

else if (mStackPtr != NULL)

{

cout << "MiniStack::Push('" << ch << "') -- Status = ";

}

if ( (bStackPtr != NULL) && (!bStackPtr->IsFull()) )

{

bStackPtr->Push(ch);

cout << "Completed" << endl;

}

else if ( (mStackPtr != NULL) )

{

try

{

mStackPtr->Push(ch);

cout << "Completed" << endl;

}

catch ( MiniStackFull )

{

cout << "Failed -- MiniStackFull Exception" << endl;

}

}

}

catch ( ... )

{

cout << "Failed due to exception" << endl;

}

break;

case '-': // Pop

try

{

if (bStackPtr != NULL)

{

cout << "BigStack::Pop() -- Status = ";

}

else if (mStackPtr != NULL)

{

cout << "MiniStack::Pop() -- Status = ";

}

if ( (bStackPtr != NULL) && (!bStackPtr->IsEmpty()) )

{

bStackPtr->Pop();

cout << "Completed" << endl;

}

else if ( (mStackPtr != NULL) )

{

try

{

mStackPtr->Pop();

cout << "Completed" << endl;

}

catch ( MiniStackEmpty )

{

cout << "Failed -- MiniStackEmpty Exception" << endl;

}

}

else

{

cout << "Failed" << endl;

}

}

catch ( ... )

{

cout << "Failed due to exception" << endl;

}

break;

case 't': // Top

try

{

if (bStackPtr != NULL)

{

cout << "BigStack::Top() -- Status = ";

}

else if (mStackPtr != NULL)

{

cout << "MiniStack::Top() -- Status = ";

}

if ( (bStackPtr != NULL) && (!bStackPtr->IsEmpty()) )

{

ch = bStackPtr->Top();

cout << "Completed, Value = '" << ch << "'" << endl;

}

else if ( (mStackPtr != NULL) )

{

try

{

ch = mStackPtr->Top();

cout << "Completed, Value = '" << ch << "'" << endl;

}

catch ( MiniStackEmpty )

{

cout << "Failed -- MiniStackEmpty Exception" << endl;

}

}

else

{

cout << "Failed" << endl;

}

}

catch ( ... )

{

cout << "Failed due to exception" << endl;

}

break;

case 'e': // IsEmpty

try

{

if (bStackPtr != NULL)

{

cout << "BigStack::IsEmpty() -- Status = ";

}

else if (mStackPtr != NULL)

{

cout << "MiniStack::IsEmpty() -- Status = ";

}

if (bStackPtr != NULL)

{

if (bStackPtr->IsEmpty())

cout << "Empty" << endl;

else

cout << "Not Empty" << endl;

}

else if (mStackPtr != NULL)

{

if (mStackPtr->IsEmpty())

cout << "Empty" << endl;

else

cout << "Not Empty" << endl;

}

else

{

cout << "Failed" << endl;

}

}

catch ( ... )

{

cout << "Failed due to exception" << endl;

}

break;

case 'f': // IsFull

try

{

if (bStackPtr != NULL)

{

cout << "BigStack::IsFull() -- Status = ";

}

else if (mStackPtr != NULL)

{

cout << "MiniStack::IsFull() -- Status = ";

}

if (bStackPtr != NULL)

{

if (bStackPtr->IsFull())

cout << "Full" << endl;

else

cout << "Not Full" << endl;

}

else if (mStackPtr != NULL)

{

if (mStackPtr->IsFull())

cout << "Full" << endl;

else

cout << "Not Full" << endl;

}

else

{

cout << "Failed" << endl;

}

}

catch ( ... )

{

cout << "Failed due to exception" << endl;

}

break;

case 'x': // MakeEmpty

try

{

if (bStackPtr != NULL)

{

cout << "BigStack::MakeEmpty() -- Status = ";

}

else if (mStackPtr != NULL)

{

cout << "MiniStack::MakeEmpty() -- Status = ";

}

if (bStackPtr != NULL)

{;

bStackPtr->MakeEmpty();

cout << "Completed" << endl;

}

else if (mStackPtr != NULL)

{

mStackPtr->MakeEmpty();

cout << "Completed" << endl;

}

else

{

cout << "Failed" << endl;

}

}

catch ( ... )

{

cout << "Failed due to exception" << endl;

}

break;

case 'p': // Print

try

{

if (bStackPtr != NULL)

{

cout << "BigStack::Print() -- ";

bStackPtr->Print();

}

else if (mStackPtr != NULL)

{

cout << "MiniStack::FwdPrint() -- ";

mStackPtr->FwdPrint();

cout << endl;

}

}

catch ( ... )

{

cout << "Failed due to exception" << endl;

}

break;

case 'd': // Destructor

try

{

if (bStackPtr != NULL)

{

cout << "BigStack::~BigStack() -- Status = ";

delete bStackPtr;

bStackPtr = NULL;

cout << "Completed" << endl;

}

else if (mStackPtr != NULL)

{

cout << "MiniStack::~MiniStack() -- Status = ";

delete mStackPtr;

mStackPtr = NULL;

cout << "Completed" << endl;

}

}

catch ( ... )

{

cout << "Failed due to exception" << endl;

}

break;

default: // Error

cout << "Error - unrecognized operation : Terminating now..." << endl;

return 1;

break;

}

inputs >> op; // Attempt to input next command

}

cout << endl;

Bar(60); // Output long bar

return 0;

} // End main()

void Bar(int n)

// Bar() -- prints horizontal bar

{

for(int k = 0; k < n; k++)

cout << '#';

cout << endl;

} // End Bar()

***************************************************ministack.h***********************************************************

#include

using namespace std;

#ifndef MINISTACK_H

#define MINISTACK_H

class MiniStackFull { /* No Code - Empty Exeception Class */ };

class MiniStackEmpty { /* No Code - Empty Exeception Class */ };

const int MINI_STACK_SIZE = 5; // Fixed number of elements in stack

// Container code responsible for throwing exceptions to signal error conditions

class MiniStack // Represents an array implementation of the Stack ADT

{

private:

int top; // Index of topmost value stored in MiniStack

char* stackPtr; // Pointer to ministack array of MINI_STACK_SIZE chars

public:

MiniStack(); // Default constructor creates an empty MiniStack object

void Push(char ch); // Adds element to top of ministack if stack is not full;

// throws MiniStackFull if stack is full

void Pop(); // Removes element from top of ministack if stack is not empty

// throws MiniStackEmpty if stack is empty

void MakeEmpty(); // Empties ministack

char Top(); // Returns copy of value stored at top of ministack if stack is not empty;

// throws MiniStackEmpty if stack is empty

bool IsFull() const; // Returns true if ministack is full; false otherwise

bool IsEmpty() const; // Returns true if ministack empty; false otherwise

~MiniStack(); // Destructor deallocates ministack array

void FwdPrint() const // Prints ministack contents, top to bottom

{

cout << "( ";

for(int k = top; k >= 0; k--)

cout << stackPtr[k] << ' ';

cout << ")";

} // End MiniStack::Print()

void RevPrint() const // Prints ministack contents, top to bottom

{

cout << "( ";

for(int k = 0; k <= top; k++)

cout << stackPtr[k] << ' ';

cout << ")";

} // End MiniStack::Print()

};

#endif

**********************************************************bigstack.h*************************************************

#include

using namespace std;

#ifndef BIGSTACK_H

#define BIGSTACK_H

#include "ministack.h"

struct ListNode // Description of a ListNode struct

{

MiniStack* stackPtr; // Pointer to a MiniStack object

ListNode* nextPtr; // Pointer to next ListNode

ListNode* previousPtr; // Pointer to previous ListNode

};

// Client code responsible for all error handling

class BigStack // Represents a double-linked node implementation of the Stack ADT

{

private:

ListNode* headPtr; // Pointer to head of double-linked list of nodes representing bigstack

public:

BigStack(); // Default constructor initializes empty bigstack object (no ListNodes)

void Push(char ch); // Adds element to top of bigstack assuming stack not full (creates ListNodes only as needed)

void Pop(); // Removes element from top of stack assuming bigstack not empty (deallocating only as needed)

char Top(); // Returns copy of top value assuming bigstack not empty

void MakeEmpty(); // Empties bigstack without leaking memory

bool IsFull() const; // Returns true if bigstack full; false otherwise

bool IsEmpty() const; // Returns true if bigstack empty; false otherwise

~BigStack(); // Destructor deallocates all dynamic memory associated with bigstack object including

// all list nodes

void Print() const // Prints stack contents, top to bottom and bottom to top

{

ListNode* tempPtr = headPtr;

// Forward print

cout << "Top [ ";

while (tempPtr != NULL)

{

tempPtr->stackPtr->FwdPrint();

if (tempPtr->nextPtr == NULL)

break;

tempPtr = tempPtr->nextPtr;

}

cout << " ] Bottom [ ";

// Reverse print

while (tempPtr != NULL)

{

tempPtr->stackPtr->RevPrint();

tempPtr = tempPtr->previousPtr;

}

cout << "] Top" << endl;

} // End BigStack::Print()

};

#endif

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

Databases Illuminated

Authors: Catherine M Ricardo, Susan D Urban

3rd Edition

1284056945, 9781284056945

More Books

Students also viewed these Databases questions

Question

In Exercises 18, solve each equation or inequality. 14 - 5x -6

Answered: 1 week ago

Question

=+6. Whether they'd talk to others about the ad.

Answered: 1 week ago