See More

//================================================================================================= // Author: Jerome Richards // Assignment: Program 3 Stack and Queue // File: StackQueue.cpp // Instructor: Dr. Reza Sanati // Class: CS 2420-002 // Date Written: 22-September-2015 // // Description: // // Read Stack and Queue commands from a file and execute the appropriate command for the correct // abstract data type - Stack or Queue. // // Print the contents of each abstract data type, stack and queue, when done. //================================================================================================= #include #include #include #include #include #include #include "Stack.h" #include "Queue.h" using namespace std; void displayProlog(); bool openFile(ifstream& inFile); bool readCommand(ifstream& inFile, string& commandLine); void tokenize(string& commandLine, vector& tokens, const char delimeter = ' '); void displayLine(const string& command, const string& adt, const int* value, const char* result); //------------------------------------------------------------------------------------------------- // Function Name: main // // Purpose: start of program // // use support functions to open and read a list of stack and queue commands // from a file // // calls funtions on abstract data types, stack and queue, to process the // appropriate command // // Parameters: none // Returns: exit code // Pre-conditions: none // Post-conditions: none //------------------------------------------------------------------------------------------------- int main() { const string ADT_STACK("Stack"); const string STACK_PUSH("push"); const string STACK_POP("pop"); const string STACK_TOP("top"); const string ADT_QUEUE("Queue"); const string QUEUE_APPEND("append"); const string QUEUE_SERVE("serve"); ifstream inFile; displayProlog(); while (openFile(inFile)) { cout << "Operation ADT Value Result " << endl; cout << "--------- ----- ----------- ---------" << endl; Stack stack; Queue queue; string commandLine; while (!inFile.eof()) { if (readCommand(inFile, commandLine)) { vector tokens; tokenize(commandLine, tokens); if (tokens.size() > 0) { if (_stricmp(STACK_PUSH.c_str(), tokens[0].c_str()) == 0) { if (tokens.size() >= 2) { int value = stoi(tokens[1]); Stack::StackError errorCode = stack.push(value); displayLine(tokens[0], ADT_STACK, &value, stack.errorToString(errorCode)); } } else if (_stricmp(STACK_POP.c_str(), tokens[0].c_str()) == 0) { Stack::StackError errorCode = stack.pop(); displayLine(tokens[0], ADT_STACK, nullptr, stack.errorToString(errorCode)); } else if (_stricmp(QUEUE_APPEND.c_str(), tokens[0].c_str()) == 0) { if (tokens.size() >= 2) { int value = stoi(tokens[1]); Queue::QueueError errorCode = queue.append(value); displayLine(tokens[0], ADT_QUEUE, &value, queue.errorToString(errorCode)); } } else if (_stricmp(QUEUE_SERVE.c_str(), tokens[0].c_str()) == 0) { Queue::QueueError errorCode = queue.serve(); displayLine(tokens[0], ADT_QUEUE, nullptr, queue.errorToString(errorCode)); } } } } inFile.close(); // list Stack values cout << endl; cout << "Stack Values" << endl; cout << "------------" << endl; int value; while (stack.top(value) == Stack::success) { cout << value << endl; stack.pop(); } // list Queue values cout << endl; cout << "Queue Values" << endl; cout << "------------" << endl; while (queue.front(value) == Queue::success) { cout << value << endl; queue.serve(); } // allow user to process again cout << endl; cout << "End of processing. Press ENTER to continue."; cin.get(); displayProlog(); } return 0; } //------------------------------------------------------------------------------------------------- // Function Name: displayProlog // Purpose: Display author name, class, and assignment information // Parameters: none // Returns: none // Pre-conditions: none // Post-conditions: none //------------------------------------------------------------------------------------------------- void displayProlog() { system("cls"); cout << "Author: Jerome Richards" << endl; cout << "Assignment: Program 3 Stack and Queue" << endl; cout << "Instructor: Dr. Reza Sanati" << endl; cout << "Class: CS 2420-002" << endl; cout << "Description: Read Stack and Queue commands from a file and execute the appropriate" << endl; cout << " command for the correct abstract data type - Stack or Queue." << endl; cout << " Print the contents of each abstract data type, stack and queue, when done." << endl; cout << "========================================================================================" << endl; cout << endl << endl; } //------------------------------------------------------------------------------------------------- // Function Name: openFile // // Purpose: open a file for input // allow the user to enter the filename and verify that it exists // // Parameters: ifstream reference (inFile) is the stream to use // Returns: true the file was opened, false no file opened // Pre-conditions: none // Post-conditions: none //------------------------------------------------------------------------------------------------- bool openFile(ifstream& inFile) { bool isOpen = false; char filename[_MAX_PATH]; while (!isOpen) { cout << "Enter filename : "; cin.getline(filename, _MAX_PATH); if (strlen(filename) == 0) { cout << "No filename entered. Press ENTER to exit."; cin.get(); exit(0); } inFile.open(filename); isOpen = inFile.good(); if (!isOpen) { cout << "Error opening file " << filename << endl; inFile.clear(); } } return isOpen; } //------------------------------------------------------------------------------------------------- // Function Name: readCommand // Purpose: reads the next command in the stream // // Parameters: ifstream reference (inFile) used to extract the next integer from // string reference (commandLine) used to hold the command read from the stream // // Returns: true if successful, false otherwise // Pre-conditions: ifstream is opened and ready // Post-conditions: none //------------------------------------------------------------------------------------------------- bool readCommand(ifstream& inFile, string& commandLine) { bool isOk = false; getline(inFile, commandLine); if (!inFile.fail()) { isOk = true; } return isOk; } //------------------------------------------------------------------------------------------------- // Function Name: tokenize // Purpose: uses stringstream to split a command line into tokens // // Parameters: string reference (commandLine) holds the command line to be tokenized // vector reference (tokens) used to hold the tokens // char (delimeter) used to split the command line into tokens // // Returns: void // Pre-conditions: none // Post-conditions: none //------------------------------------------------------------------------------------------------- void tokenize(string& commandLine, vector& tokens, const char delimeter) { istringstream streamCommandLine(commandLine); string token; while (getline(streamCommandLine, token, delimeter)) { tokens.push_back(token); } } //------------------------------------------------------------------------------------------------- // Function Name: displayLine // Purpose: displays the formatted results of the processed line // // Parameters: string reference (command) that was read from the file // string reference (adt) is the abstract data type (stack, queue) // int pointer (value) read from the file, if any // char pointer (error) description from the command results // // Returns: void // Pre-conditions: none // Post-conditions: none //------------------------------------------------------------------------------------------------- void displayLine(const string& command, const string& adt, const int* value, const char* result) { const int COMMAND_WIDTH = 9; const int ADT_WIDTH = 5; const int VALUE_WIDTH = 11; const int RESULT_WIDTH = 9; const int SPACER_WIDTH = 3; cout.setf(ios::left, ios::adjustfield); cout << setw(COMMAND_WIDTH) << command << setw(SPACER_WIDTH) << ' ' << setw(ADT_WIDTH) << adt << setw(SPACER_WIDTH) << ' '; cout.setf(ios::right, ios::adjustfield); cout << setw(VALUE_WIDTH) << (value != nullptr ? *value : ' '); cout.setf(ios::left, ios::adjustfield); cout << setw(SPACER_WIDTH) << ' ' << setw(RESULT_WIDTH) << (result != nullptr ? result : " ") << endl; }