source code# Include <iostream.h>
# Include <conio.h># Include <string.h>
# <stdlib.h>
/ / Define the maximum number of Patients in a queue# Define MAXPATIENTS 100/ / Define the data structure for patient
struct patient
{
FirstName char [50];
LastName char [50];
char ID [20];
};/ / Define class for the queue
class queue
{
public:
queue (void);
int AddPatientAtEnd (patient P);
int AddPatientAtBeginning (patient P);
patient GetNextPatient (void);
int RemoveDeadPatient (patient * p);
OutputList void (void);
DepartmentName char [50];
private:
NumberOfPatients int;
List of patient [MAXPATIENTS];
};/ / Declare member functions for queuequeue :: queue ()
{
/ / Constructor
NumberOfPatients = 0;
}int queue :: AddPatientAtEnd (patient P)
{
/ / Adds a normal patient to the end of the queue.
/ / Returns 1 if successful, 0 if the queue is full.
if (NumberOfPatients> = MAXPATIENTS)
{
/ / Queue is full
return 0;
}
/ / Put in new patient
else
List [NumberOfPatients] = p; NumberOfPatients + +;
return 1;
}int queue :: AddPatientAtBeginning (patient P)
{
/ / Adds a critically ill patient to the beginning of the queue.
/ / Returns 1 if successful, 0 if the queue is full.
int i;
if (NumberOfPatients> = MAXPATIENTS)
{
/ / Queue is full
return 0;
}/ / Move all Hasta one position back in the queue
for (i = NumberOfPatients-1, i> = 0; i-)
{
List [i +1] = List [i];
}
/ / Put in new patient
List [0] = p; NumberOfPatients + +;
return 1;
}patient queue :: GetNextPatient (void)
{
/ / Gets the patient that is first in the queue.
/ / Returns patient with no ID if the queue is emptyint i; patient p;
if (NumberOfPatients == 0) {
/ / Queue is empty
strcpy (p.ID, "");
return p;}
/ / Get the first patient
p = List [0];
/ / Move all remaining Hasta forward one position in the queue
NumberOfPatients-;
for (i = 0; i <NumberOfPatients; i + +)
{
List [i] = List [i +1];
}
/ / Return patient
return p;
}int queue :: RemoveDeadPatient (patient * p)
{
/ / Removes a patient from the queue.
/ / Returns 1 if successful, 0 if not found patient
int i, j, found = 0;
/ / Search for patient
for (i = 0; i <NumberOfPatients; i + +)
{
if (stricmp (List [i]. ID, p-> ID) == 0)
{/ / Patient found in the queue
* P = List [i]; found = 1;
/ / Move all following, Hasta forward one position in the queue
NumberOfPatients-;
for (j = i; j <NumberOfPatients; j + +)
{
List [j] = List [j +1];
}
}
}
return found;
}void queue :: OutputList (void)
{
/ / Queue entire lists 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;
}
}
}/ / Declare functions used by main:patient InputPatient (void)
{
/ / This function asks user for patient data.
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));
/ / Check if valid data
if (p.FirstName [0] == 0 | | p.LastName [0] == 0 | | p.ID [0] == 0)
{
/ / Rejected
strcpy (p.ID, "");
cout << "Error: Data not valid. Operation canceled. ";
getch ();
}
return p;
}void OutputPatient (patient * p)
{
/ / This function outputs data to the screen patient
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;
}ReadNumber int ()
{
/ / This function reads an integer number from the keyboard.
/ / Because it is used with cin >> input does not work properly!
char buffer [20];
cin.getline (buffer, sizeof (buffer));
return atoi (buffer);
}void DepartmentMenu (queue * q)
{
/ / This function defines the user interface with menu for one
department
int choice = 0, success, patient p;
while (choice! = 6)
{
/ / Clear screen
clrscr ();
/ / Print the menu
cout << "Welcome to the department: "<< q-> DepartmentName;
cout << "Please enter your choice: ";
cout << "
1: Add a 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 the queue ";
cout << "
6: Change department or exit
";
/ / Get user choice
choice = ReadNumber ();
/ / Do action indicated
switch (choice)
{
case 1: / / Add the normal patient
p = InputPatient ();
if (p.ID [0])
{
success = q-> AddPatientAtEnd (p);
clrscr ();
if (success)
{
cout << "
Patient added:";}
else
{
/ / Error
cout << "Error: The queue is full. Can not add patient: ";
}
OutputPatient (& p);
cout << "Press any key ";
getch ();
}
break;case 2: / / Add critically ill patient
p = InputPatient ();
if (p.ID [0])
{
success = q-> AddPatientAtBeginning (p);
clrscr ();
if (success)
{
cout << "
Patient added:";
}
else
{/ / Error
cout << "Error: The queue is full. Can not add
patient ";
}OutputPatient (& p);
cout << "Press any key ";
getch ();
}
break;case 3: / / Take out patient for operation
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: / / Remove dead patient from the queue
p = InputPatient ();
if (p.ID [0])
{
success = q-> RemoveDeadPatient (& p);
clrscr ();
if (success)
{
cout << "
Patient removed:";
}
else
{
/ / Error
cout << "Error: Can not find patient:";
}
OutputPatient (& p);
cout << "
Press any key ";getch ();
}
break;case 5: / / List queue
clrscr ();
q-> OutputList ();
cout << "Press any key ";
getch (); break;
}
}
}/ / Main function defining queues and main menu
void main ()
{
int i, MenuChoice = 0;
/ / Define three queues
Departments queue [3];
/ / Set the department names
strcpy (Departments [0]. DepartmentName, "Heart Clinic");
strcpy (Departments [1]. DepartmentName, "Lung Clinic");
strcpy (Departments [2]. DepartmentName, "Plastic surgery");while (MenuChoice! = 4)
{
/ / Clear screen
clrscr ();
/ / Print the menu
cout << "Welcome to Software City Hospital ";
cout << "Please enter your choice:
";
for (i = 0, i <3; i + +)
{
/ / Write menu item for department i
cout << "
"<< (I +1) <<": "<< Departments [i]. DepartmentName;
}
cout << "
4: Exit
";
/ / Get user choice
MenuChoice = ReadNumber ();
/ / Is it a department name?
if (MenuChoice> = 1 && MenuChoice <= 3)
{
/ / Now for the department submenu
/ / (Using pointer Arithmetics here :)
DepartmentMenu (Departments + (MenuChoice-1));
}
}
}
# Include <conio.h># Include <string.h>
# <stdlib.h>
/ / Define the maximum number of Patients in a queue# Define MAXPATIENTS 100/ / Define the data structure for patient
struct patient
{
FirstName char [50];
LastName char [50];
char ID [20];
};/ / Define class for the queue
class queue
{
public:
queue (void);
int AddPatientAtEnd (patient P);
int AddPatientAtBeginning (patient P);
patient GetNextPatient (void);
int RemoveDeadPatient (patient * p);
OutputList void (void);
DepartmentName char [50];
private:
NumberOfPatients int;
List of patient [MAXPATIENTS];
};/ / Declare member functions for queuequeue :: queue ()
{
/ / Constructor
NumberOfPatients = 0;
}int queue :: AddPatientAtEnd (patient P)
{
/ / Adds a normal patient to the end of the queue.
/ / Returns 1 if successful, 0 if the queue is full.
if (NumberOfPatients> = MAXPATIENTS)
{
/ / Queue is full
return 0;
}
/ / Put in new patient
else
List [NumberOfPatients] = p; NumberOfPatients + +;
return 1;
}int queue :: AddPatientAtBeginning (patient P)
{
/ / Adds a critically ill patient to the beginning of the queue.
/ / Returns 1 if successful, 0 if the queue is full.
int i;
if (NumberOfPatients> = MAXPATIENTS)
{
/ / Queue is full
return 0;
}/ / Move all Hasta one position back in the queue
for (i = NumberOfPatients-1, i> = 0; i-)
{
List [i +1] = List [i];
}
/ / Put in new patient
List [0] = p; NumberOfPatients + +;
return 1;
}patient queue :: GetNextPatient (void)
{
/ / Gets the patient that is first in the queue.
/ / Returns patient with no ID if the queue is emptyint i; patient p;
if (NumberOfPatients == 0) {
/ / Queue is empty
strcpy (p.ID, "");
return p;}
/ / Get the first patient
p = List [0];
/ / Move all remaining Hasta forward one position in the queue
NumberOfPatients-;
for (i = 0; i <NumberOfPatients; i + +)
{
List [i] = List [i +1];
}
/ / Return patient
return p;
}int queue :: RemoveDeadPatient (patient * p)
{
/ / Removes a patient from the queue.
/ / Returns 1 if successful, 0 if not found patient
int i, j, found = 0;
/ / Search for patient
for (i = 0; i <NumberOfPatients; i + +)
{
if (stricmp (List [i]. ID, p-> ID) == 0)
{/ / Patient found in the queue
* P = List [i]; found = 1;
/ / Move all following, Hasta forward one position in the queue
NumberOfPatients-;
for (j = i; j <NumberOfPatients; j + +)
{
List [j] = List [j +1];
}
}
}
return found;
}void queue :: OutputList (void)
{
/ / Queue entire lists 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;
}
}
}/ / Declare functions used by main:patient InputPatient (void)
{
/ / This function asks user for patient data.
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));
/ / Check if valid data
if (p.FirstName [0] == 0 | | p.LastName [0] == 0 | | p.ID [0] == 0)
{
/ / Rejected
strcpy (p.ID, "");
cout << "Error: Data not valid. Operation canceled. ";
getch ();
}
return p;
}void OutputPatient (patient * p)
{
/ / This function outputs data to the screen patient
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;
}ReadNumber int ()
{
/ / This function reads an integer number from the keyboard.
/ / Because it is used with cin >> input does not work properly!
char buffer [20];
cin.getline (buffer, sizeof (buffer));
return atoi (buffer);
}void DepartmentMenu (queue * q)
{
/ / This function defines the user interface with menu for one
department
int choice = 0, success, patient p;
while (choice! = 6)
{
/ / Clear screen
clrscr ();
/ / Print the menu
cout << "Welcome to the department: "<< q-> DepartmentName;
cout << "Please enter your choice: ";
cout << "
1: Add a 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 the queue ";
cout << "
6: Change department or exit
";
/ / Get user choice
choice = ReadNumber ();
/ / Do action indicated
switch (choice)
{
case 1: / / Add the normal patient
p = InputPatient ();
if (p.ID [0])
{
success = q-> AddPatientAtEnd (p);
clrscr ();
if (success)
{
cout << "
Patient added:";}
else
{
/ / Error
cout << "Error: The queue is full. Can not add patient: ";
}
OutputPatient (& p);
cout << "Press any key ";
getch ();
}
break;case 2: / / Add critically ill patient
p = InputPatient ();
if (p.ID [0])
{
success = q-> AddPatientAtBeginning (p);
clrscr ();
if (success)
{
cout << "
Patient added:";
}
else
{/ / Error
cout << "Error: The queue is full. Can not add
patient ";
}OutputPatient (& p);
cout << "Press any key ";
getch ();
}
break;case 3: / / Take out patient for operation
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: / / Remove dead patient from the queue
p = InputPatient ();
if (p.ID [0])
{
success = q-> RemoveDeadPatient (& p);
clrscr ();
if (success)
{
cout << "
Patient removed:";
}
else
{
/ / Error
cout << "Error: Can not find patient:";
}
OutputPatient (& p);
cout << "
Press any key ";getch ();
}
break;case 5: / / List queue
clrscr ();
q-> OutputList ();
cout << "Press any key ";
getch (); break;
}
}
}/ / Main function defining queues and main menu
void main ()
{
int i, MenuChoice = 0;
/ / Define three queues
Departments queue [3];
/ / Set the department names
strcpy (Departments [0]. DepartmentName, "Heart Clinic");
strcpy (Departments [1]. DepartmentName, "Lung Clinic");
strcpy (Departments [2]. DepartmentName, "Plastic surgery");while (MenuChoice! = 4)
{
/ / Clear screen
clrscr ();
/ / Print the menu
cout << "Welcome to Software City Hospital ";
cout << "Please enter your choice:
";
for (i = 0, i <3; i + +)
{
/ / Write menu item for department i
cout << "
"<< (I +1) <<": "<< Departments [i]. DepartmentName;
}
cout << "
4: Exit
";
/ / Get user choice
MenuChoice = ReadNumber ();
/ / Is it a department name?
if (MenuChoice> = 1 && MenuChoice <= 3)
{
/ / Now for the department submenu
/ / (Using pointer Arithmetics here :)
DepartmentMenu (Departments + (MenuChoice-1));
}
}
}
{ 0 comments... read them below or add one }
Post a Comment
Enter your comments here