This lab will simulate a hospital waiting room with a patient’s name and priorit
ID: 3860717 • Letter: T
Question
This lab will simulate a hospital waiting room with a patient’s name and priority number. This will be possible by using a priority queue in C++ that stores the name of a patient and a non-negative integer priority. The queue will allow us to insert a patient’s name, to remove the name of the patient with the lowest priority, and to decrease a patient’s priority.
The public member functions, described below, are required:
priorityqueue() - The default constructor creates an empty priority queue.
void push(int key, int priority) - This function inserts a new element with a given key and priority. The key must not appear in the queue and the priority must not be negative. You can use assert statements to check these and other explicitly stated preconditions.
void pop() - This function removes the element with smallest priority. No value is returned. It is an error to pop from an empty priority queue.
int topPriority() const - Returns the element of least priority.
int topKey() const - This “top” function gives the key of the element of least priority.
void reducePriority(int key, int newpriority) - This function lowers the priority of a specified element. It is an error if the key is not present in the priority queue, if the new priority is negative, or if it is greater than the current priority.
int getPriority(int key) - This function returns the priority of the element with the specified key. It returns 1 if the key is not present in the priority queue.
bool isEmpty() const - This function tells us if the priority queue is empty.
bool isPresent(int key) const - This function returns true if there is an element in the queue with the specified key.
void clear() - This function empties the priority queue.
int size() const - This is a function that returns the number of elements in the queue.
Write the functions above in a specification file, priorityqueue.h . Then, apply the functions in the implementation file, priorityqueue.cpp. Lastly, write a main function to demonstrate that the functions work correctly.
Explanation / Answer
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#define MAXMIUMPATIENTS 20
struct patient
{
char PFirstName[25];
char PLastName[25};
char ID[20];
};
{
public:
queue (void);
int AddPatAtEnd (patient p);
int AddPatAtBegin(patient p);
patient GetNextPatient (void);
int RemoveDeadPatient (patient * p);
void OutputList (void);
char DepartmentName[50];
private:
int NumberOfPatients;
patient List[MAXMIUMPATIENTS];
};
queue::queue ()
{
NumberOfPatients = 0;
}
int queue::AddPatientAtEnd (patient p)
{
if (NumberOfPatients >= MAXMIUMPATIENTS)
{
return 0;
}
else
List[NumberOfPatients] = p; NumberOfPatients++;
return 1;
}
int queue::AddPatAtBegin (patient p)
{
int i;
if (NumberOfPatients >= MAXMIUMPATIENTS)
{
return 0;
}
for (i = NumberOfPatients-1; i >= 0; i--)
{
List[i+1] = List[i];
}
List[0] = p; NumberOfPatients++;
return 1;
}
patient queue::GetNextPatient (void)
{
int i; patient p;
if (NumberOfPatients == 0) {
strcpy(p.ID,"");
return p;}
p = List[0];
NumberOfPatients--;
for (i=0; i<NumberOfPatients; i++)
{
List[i] = List[i+1];
}
return p;
}
void queue::OutputList (void)
{
// lists entire queue on screen
int i;
if (NumberOfPatients == 0)
{
cout << "
Queue is empty";
}
else
{
for (i=0; i<NumberOfPatients; i++)
{
cout << "
" << List[i].FirstName;
cout << " " << List[i].LastName;
cout << " " << List[i].ID;
}
}
}
patient InputPatient (void)
{
patient p;
cout << "
Please enter data for new patient
First name: ";
cin.getline(p.FirstName, sizeof(p.FirstName));
cout << "
Last name: ";
cin.getline(p.LastName, sizeof(p.LastName));
cout << "
Social security number: ";
cin.getline(p.ID, sizeof(p.ID));
if (p.FirstName[0]==0 || p.LastName[0]==0 || p.ID[0]==0)
{
strcpy(p.ID,"");
cout << "
Error: Data not valid. Operation cancelled.";
getch();
}
return p;
}
void OutputPatient (patient * p)
{
if (p == NULL || p->ID[0]==0)
{
cout << "
No patient";
return;
}
else
cout << "
Patient data:";
cout << "
First name: " << p->FirstName;
cout << "
Last name: " << p->LastName;
cout << "
Social security number: " << p->ID;
}
int ReadNumber()
{
char buffer[20];
cin.getline(buffer, sizeof(buffer));
return atoi(buffer);
}
void DepartmentMenu (queue * q)
{
department
int choice = 0, success; patient p;
while (choice != 6)
{
clrscr();
cout << "
Welcome to department: " << q->DepartmentName;
cout << "
Please enter your choice:";
cout << "
1: Add normal patient";
cout << "
2: Add critically ill patient";
cout << "
3: Take out patient for operation";
cout << "
4: Remove dead patient from queue";
cout << "
5: List queue";
cout << "
6: Change department or exit
";
choice = ReadNumber();
switch (choice)
{
case 1:
p = InputPatient();
if (p.ID[0])
{
success = q->AddPatientAtEnd(p);
clrscr();
if (success)
{
cout << "
Patient added:
";
}
else
{
// error
cout << "
Error: The queue is full. Cannot add patient:";
}
OutputPatient(&p);
cout << "
Press any key";
getch();
}
break;
case 2:
p = InputPatient();
if (p.ID[0])
{
success = q->AddPatientAtBeginning(p);
clrscr();
if (success)
{
cout << "
Patient added:
";
}
else
{
cout << "
Error: The queue is full. Cannot add
patient:";
}
OutputPatient(&p);
cout << "
Press any key";
getch();
}
break;
case 3:
p = q->GetNextPatient();
clrscr();
if (p.ID[0])
{
cout << "
Patient to operate:
";
OutputPatient(&p);}
else
{
cout << "
There is no patient to operate.";
}
cout << "
Press any key";
getch();
break;
case 4:
p = InputPatient();
if (p.ID[0])
{
success = q->RemoveDeadPatient(&p);
clrscr();
if (success)
{
cout << "
Patient removed:
";
}
else
{
cout << "
Error: Cannot find patient:
";
}
OutputPatient(&p);
cout << "
Press any key";
getch();
}
break;
case 5:
clrscr();
q->OutputList();
cout << "
Press any key";
getch(); break;
}
}
}
void main ()
{
int i, MenuChoice = 0;
queue departments[3];
strcpy (departments[0].DepartmentName, "Heart clinic");
strcpy (departments[1].DepartmentName, "Lung clinic");
strcpy (departments[2].DepartmentName, "Plastic surgery");
while (MenuChoice != 4)
{
clrscr();
cout << "
Welcome to Software City Hospital";
cout << "
Please enter your choice:
";
for (i = 0; i < 3; i++)
{
cout << "
" << (i+1) << ": " << departments[i].DepartmentName;
}
cout << "
4: Exit
";
MenuChoice = ReadNumber();
if (MenuChoice >= 1 && MenuChoice <= 3)
{
DepartmentMenu (departments + (MenuChoice-1));
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.