Answered step by step
Verified Expert Solution
Question
1 Approved Answer
/******************************************************** /********************************************************* /********************************************************* /*********************************************************** #include #include #include StackType.h using namespace std; int main() { ifstream inFile; // file containing operations ofstream outFile; // file containing
/********************************************************
/*********************************************************
/*********************************************************
/***********************************************************
#include#include #include "StackType.h" using namespace std; int main() { ifstream inFile; // file containing operations ofstream outFile; // file containing output string inFileName; // input file external name string outFileName; // output file external name string outputLabel; string command; // operation to be executed int item; StackType stack(5); int numCommands; // Prompt for file names, read file names, and prepare files cout << "Enter name of input command file; press return." << endl; cin >> inFileName; inFile.open(inFileName.c_str()); cout << "Enter name of output file; press return." << endl; cin >> outFileName; outFile.open(outFileName.c_str()); cout << "Enter name of test run; press return." << endl; cin >> outputLabel; outFile << outputLabel << endl; inFile >> command; numCommands = 0; while (command != "Quit") { try { if (command == "Push") { inFile >> item; stack.Push(item); } else if (command == "Pop") stack.Pop(); else if (command == "Top") { item = stack.Top(); outFile<< "Top element is " << item << endl; } else if (command == "IsEmpty") if (stack.IsEmpty()) outFile << "Stack is empty." << endl; else outFile << "Stack is not empty." << endl; else if (command == "IsFull") if (stack.IsFull()) outFile << "Stack is full." << endl; else outFile << "Stack is not full." << endl; else cout << "Command not recognized." << endl; } catch (FullStack) { outFile << "FullStack exception thrown." << endl; } catch (EmptyStack) { outFile << "EmtpyStack exception thrown." << endl; } numCommands++; cout << " Command number " << numCommands << " completed." << endl; inFile >> command; }; cout << "Testing completed." << endl; inFile.close(); outFile.close(); return 0; } ****stack.h
// The class definition for StackType using templates class FullStack // Exception class thrown by Push when stack is full. {}; class EmptyStack // Exception class thrown by Pop and Top when stack is emtpy. {}; templateclass StackType { public: StackType(); // Class constructor. StackType(int max); // Parameterized constructor. ~StackType(); // Destructor bool IsFull() const; // Function: Determines whether the stack is full. // Pre: Stack has been initialized. // Post: Function value = (stack is full) bool IsEmpty() const; // Function: Determines whether the stack is empty. // Pre: Stack has been initialized. // Post: Function value = (stack is empty) void Push(ItemType item); // Function: Adds newItem to the top of the stack. // Pre: Stack has been initialized. // Post: If (stack is full), FullStack exception is thrown; // otherwise, newItem is at the top of the stack. void Pop(); // Function: Removes top item from the stack. // Pre: Stack has been initialized. // Post: If (stack is empty), EmptyStack exception is thrown; // otherwise, top element has been removed from stack. ItemType Top(); // Function: Returns a copy of top item on the stack. // Pre: Stack has been initialized. // Post: If (stack is empty), EmptyStack exception is thrown; // otherwise, top element has been removed from stack. private: int top; int maxStack; ItemType* items; }; // The function definitions for class StackType. template StackType ::StackType(int max) { maxStack = max; top = -1; items = new ItemType[maxStack]; } template StackType ::StackType() { maxStack = 500; top = -1; items = new ItemType[maxStack]; } template bool StackType ::IsEmpty() const { return (top == -1); } template bool StackType ::IsFull() const { return (top == maxStack-1); } template void StackType ::Push(ItemType newItem) { if (IsFull()) throw FullStack(); top++; items[top] = newItem; } template void StackType ::Pop() { if( IsEmpty() ) throw EmptyStack(); top--; } template ItemType StackType ::Top() { if (IsEmpty()) throw EmptyStack(); return items[top]; } template StackType ::~StackType() { delete [] items; }
********* implement using Implement and test a string version of the the StackType. Write a driver main function to conduct- currently uses numbers
IsEmpty Push 5 Push 7 Push 6 Push 9 Top Pop Top Pop Top Pop Top Pop Push 2 Push 3 Push 3 Push 4 Top Pop Top Pop Top Pop Top Pop Push 5 Pop Push 3 Push 7 Pop Top Pop IsEmpty Push 2 IsEmpty
c++
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