Question
Write message.cpp and priorityq.cpp. The code in message.cpp and priorityq.cpp doesn't need any input or output statements. Do not modify priorityq.h, message.h, or main.cpp. main.cpp
Write message.cpp and priorityq.cpp. The code in message.cpp and priorityq.cpp doesn't need any input or output statements.
Do not modify priorityq.h, message.h, or main.cpp.
main.cpp
_______________________________________________________________________________
#include iostream #include fstream #include new #include cstddef #include "priorityq.h"
using namespace std;
int main(int argc, char* argv[]) { ifstream inputs; // Input file for commands char op; // Hold operation int n; // Integer input from file PriorityQ* ptr = NULL; // Will point to priority queue object Message msg; // Assembled message Priorities p; // Message priority char c; // Message priority input from file string s; // Message string from file // Output usage message if one input file name is not provided if (argc != 2) { cout "; return 1; } // Attempt to open input file -- terminate if file does not open inputs.open(argv[1]); if (!inputs) { cout
// Process commands from input file getline(inputs, s); // Input comment line cout > op; // Attempt to input first command while (inputs) { switch (op) // Identify and perform operation input from file { case '#': // Echo Comment getline(inputs, s); cout
case 'c': // Constructor cout
case '+': // Enqueue inputs >> c; // Input message priority from file inputs.ignore(100, ' '); // Skip one space getline(inputs,s); // Input rest of line as the message cout Enqueue(msg); } catch (FullPQ) { cout
case '-': // Dequeue cout Dequeue(); cout
cout
case 'x': // Purge inputs >> c; // Input message priority from file cout Purge(HIGH); break; case 'M': ptr->Purge(MEDIUM); break; case 'L': ptr->Purge(LOW); break; } } catch (EmptyPQ) { cout
case '?': // Peek inputs >> n; cout Peek(n).Print(); } catch (InvalidPeekPQ) { cout
case 'f': // Front cout Front().Print(); } catch (EmptyPQ) { cout
cout
case 'r': // Rear cout Rear().Print(); } catch (EmptyPQ) { cout
cout Print(); cout Size()
case 'm': // Make PriorityQ Empty but ready for use cout MakeEmpty(); break; case 'd': // Destructor delete ptr; ptr = NULL; cout
default: // Error cout > op; // Attempt to input next command } return 0; } // End main()
_______________________________________________________________________________
priorityq.h
_______________________________________________________________________________
#ifndef PRIORITYQ_H #define PRIORITYQ_H
#include iostream #include "message.h" using namespace std;
// // Exception classes for PriorityQ // class EmptyPQ { /* No additional code */ }; // Exception class for empty PriorityQ condition
class FullPQ { /* No additional code */ }; // Exception class for full PriorityQ condition
class InvalidPeekPQ { /* No additional code */ }; // Exception class for invalid PriorityQ peek condition
// // Priority Queue Node Structure // struct Node // Linked priority queue node structure { Message data; // Field for storing data in the priority queue node Node* nextPtr; // Points to next priority queue node Node* previousPtr; // Points to previous priority queue node };
// // PriorityQ class declaration // class PriorityQ // Double linked queue of messages sorted by priority { private: Node* frontPtr; // Points to front node of priority queue Node* rearPtr; // Points to rear node of priority queue int count; // Number of values stored in priority queue public: /********** Start of functions you must implement for PriorityQ **************/ // Implement the following nine public functions in the file named priorityq.cpp PriorityQ(); // PriorityQ() // Initializes all private variables to indicate an empty priority queue ~PriorityQ(); //~PriorityQ() // Deallocates all priority queue nodes // No memory leak allowed void MakeEmpty(); // MakeEmpty() // Deallocates all priority queue nodes and returns priority queue to empty ready-to-use state // No memory leak allowed void Enqueue(Message msg); // Enqueue() // Adds value to priority queue in correct position and increments count. // Duplicates are allowed. // Highest priority messages must always be at front of queue // Lowest priority messages must always be at rear of queue // Add AFTER messages of similar priority // If queue is already full, throws FullPQ exception.
void Dequeue(); // Dequeue() // Removes highest priority message from front of priority queue and decrements count. // If queue is empty, throws EmptyPQ exception // No memory leak allowed
void Purge(Priorities p); // Purge() // Removes all messages of priority p from queue leaving all other messages in priority queue // If queue is empty, throws EmptyPQ exception // No memory leak allowed Message Front() const; // Front() // Returns message at front of priority queue // If queue is empty, throws EmptyPQ
Message Rear() const; // Rear() // Returns message at rear of priority queue // If queue is empty, throws EmptyPQ
Message Peek(int n) const; // Peek() // Returns message n positions from front of priority queue // If position n does not exist, throws InvalidPeekPQ bool IsFull() const; // IsFull() // Returns true if queue is full. Returns false otherwise. DOES NOT MODIFY THE PRIORITY QUEUE bool IsEmpty() const; // IsEmpty() // Returns true if queue is empty. Returns false otherwise. DOES NOT MODIFY THE PRIORITY QUEUE int Size() const; // Size() // Returns number of items stored in priority queue. DOES NOT MODIFY THE PRIORITY QUEUE
/*********** End of functions you must implement for PriorityQ ***************/ void Print() const // Print() -- DO NOT MODIFY OR RELOCATE THIS FUNCTION // Prints contents of priority queue without modifying its contents { Node* tempPtr = frontPtr;
// Prints queue nodes Front-to-Rear order cout data.GetMessage() data.Print(); cout nextPtr; } cout
// Prints queue nodes Rear-to-Front order tempPtr = rearPtr; while (tempPtr != NULL) { //cout data.GetMessage() data.Print(); cout previousPtr; } cout
#endif
_______________________________________________________________________________
message.h
_______________________________________________________________________________
#ifndef MESSAGE_H #define MESSAGE_H
#include iostream using namespace std;
// // Define enumerated Priorities type // enum Priorities {UNKNOWN, LOW, MEDIUM, HIGH};
// // Message class declaration // class Message // Models a single message with priority { private: Priorities priority; // Stores message priority string message; // Stores message contents public: /********** Start of functions you must implement for Message **************/ // Implement the following five public functions in the file named message.cpp Message(); // Initializes message to empty string with UNKNOWN priority
void SetPriority(Priorities p); // Sets priority equal to p
void SetMessage(string msg); // Sets message equal to msg Priorities GetPriority() const; // Returns priority value without modification
string GetMessage() const; // Returns message contents without modification /*********** End of functions you must implement for Message ***************/ void Print() const // DO NOT MODIFY OR RELOCATE THIS FUNCTION { cout
#endif
_______________________________________________________________________________
_______________________________________________________________________________
# p04input1.txt -- Test: PriorityQ(), Enqueue(), Dequeue(), ~PriorityQ()
# Test adding messages H to L (add at rear) c + H Cat Food + M Goat Chow + L Dog Walk p d
# Test adding messages L to H (add at front) c + L Dog Walk + M Goat Chow + H Cat Food p d
# Test adding messages (add in between) c + H Cat Food + L Dog Walk + M Goat Chow p d
# Test adding messages with same priorities c + H Cat Food + H Dog Kibble + H Fish Food + H Cat Food p d
# Test adding messages (arbitrary) c + H Cat Food + L Dog Walk + M Goat Chow + H Horse Oats + L Rabbit Food + M Zebra Stripes + H Bird Seed + L Dog Walk p d
# Test Dequeue normal operation c + H Cat Food + L Dog Walk + M Goat Chow p - p - p - p + H Cat Food + L Dog Walk + M Goat Chow + H Horse Oats + L Rabbit Food + M Zebra Stripes + H Bird Seed p - p - p - p - p - p - p - p d
# Test Dequeue error handling c - + H Cat Food + L Dog Walk + M Goat Chow + H Horse Oats + L Rabbit Food + M Zebra Stripes + H Bird Seed p - p - p - p - p - p - p - p - p d
_______________________________________________________________________________
_______________________________________________________________________________
# p04input2.txt -- Test: Size(), Front(), Rear(), MakeEmpty()
# Test Size() c s + H Cat Food s + L Dog Walk s + M Goat Chow p s - p s - p s - p s - p s d
# Test Front() and Rear() c f r + H Cat Food f r + L Dog Walk f r + M Goat Chow f r + H Horse Oats f r + L Rabbit Food f r + M Zebra Stripes f r + H Bird Seed f r p - f r p - f r p - f r p d
# Test MakeEmpty normal operation c + H Cat Food + L Dog Walk + M Goat Chow + H Horse Oats + L Rabbit Food + M Zebra Stripes + H Bird Seed p m p + H Cat Food + L Dog Walk + M Goat Chow p m p d
# Test MakeEmpty error handling c p m p d
_______________________________________________________________________________
_______________________________________________________________________________
# p04input3.txt -- Test: Peek(), Purge()
# Test Peek() normal operation
c + H Cat Food + L Dog Walk + M Goat Chow p ? 0 ? 1 ? 2 p - ? 0 ? 1 p - ? 0 ? 1 ? 2 ? 3 ? 5 p - ? 0 ? 1 ? 2 ? 3 ? 5 d
# Test Peek() error handling c ? 0 ? 1 + H Cat Food + L Dog Walk + M Goat Chow p ? 1 ? 3 ? 5 - p ? 1 ? 2 ? 3 ? 5 p d
# Test Purge(HIGH) normal operation
c p + H Cat Food + L Dog Walk + M Goat Chow + H Horse Oats + L Rabbit Food + M Zebra Stripes + H Bird Seed p s x H p s d
# Test Purge(MEDIUM) normal operation c p + H Cat Food + L Dog Walk + M Goat Chow + H Horse Oats + L Rabbit Food + M Zebra Stripes + H Bird Seed p s x M p s d
# Test Purge(LOW) normal operation c p + H Cat Food + L Dog Walk + M Goat Chow + H Horse Oats + L Rabbit Food + M Zebra Stripes + H Bird Seed p s x L p s d
# Test Purge() normal operation c p + H Cat Food + L Dog Walk + M Goat Chow + H Horse Oats + L Rabbit Food + M Zebra Stripes + H Bird Seed p s x H p s x M p s x L p s + H Cat Food + L Dog Walk + M Goat Chow + H Horse Oats + L Rabbit Food + M Zebra Stripes + H Bird Seed p x L p x M p x H p d
# Test Purge() error handling c p x L p s x M p s x H p s d
_______________________________________________________________________________
Complete the provided partial C++ program that will implement a Priority Queue ADT (abstract data type) in which the internal representation of the priority queue is a double linked series of dynamically allocated nodes. The Priority Queue will be used to store text messages read from an input file. Each message has a priority (H for High, M for Medium, L for Low). When input from the file by the main function, the message and priority information are stored within an object of the type Message. The text message is stored as a string attribute, and the message priority is represented by an attribute with type Priorities, an enumerated type provided. Priority Queue Rules of operation: (1) If the Priority Queue contains messages which have different priority levels, then the highest priority messages must be removed from the FRONT of the Priority Queue before any lower priority messages are removed whenever the Dequeue 0 method is invoked HINT: The priority queue is sorted by Priority Level (highest to lowest) as it is being constructed by Enqueue in much the same way as we discussed with the implementation of a sorted linked list. (2) Given two messages of the same priority level, the message that has been in the Priority Queue for the longest time has the highest priority. (FIFo operation within a priority level)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