In C++ program 2. (70) The local medical clinic has decided to automate its sche
ID: 3737510 • Letter: I
Question
In C++ program
Explanation / Answer
SOurceCode:
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#define PATIENTMAX 100
struct person
{
char FName[50];
char LName[50];
char ID[20];
};
class hospQueue
{
public:
hospQueue (void);
int LastAddPerson(person p);
int BegingAddPerson(person p);
person GetNxtPerson(void);
int RemvDeadPerson(person* p);
void OutPatientList (void);
char DeptName[50];
private:
int NumPatients;
person PatientList[PATIENTMAX];
};
hospQueue::hospQueue ()
{
NumPatients = 0;
}
int hospQueue::LastAddPerson(person p)
{
if (NumPatients >= PATIENTMAX)
{
return 0;
}
else
PatientList[NumPatients] = p; NumPatients++;
return 1;
}
int hospQueue::BegingAddPerson(person p)
{
int i;
if (NumPatients >= PATIENTMAX)
{
return 0;
}
for (i = NumPatients-1; i >= 0; i--)
{
PatientList[i+1] = PatientList[i];
}
PatientList[0] = p;
NumPatients++;
return 1;
}
person hospQueue::GetNxtPerson(void)
{
int i; person p;
if (NumPatients == 0) {
strcpy(p.ID,"");
return p;}
p = PatientList[0];
NumPatients--;
for (i=0; i<NumPatients; i++)
{
PatientList[i] = PatientList[i+1];
}
return p;
}
int hospQueue::RemvDeadPerson(person* p)
{
int i, j, found = 0;
for (i=0; i<NumPatients; i++)
{
if (stricmp(PatientList[i].ID, p->ID) == 0)
{
*p = PatientList[i]; found = 1;
NumPatients--;
for (j=i; j<NumPatients; j++)
{
PatientList[j] = PatientList[j+1];
}
}
}
return found;
}
void hospQueue::OutPatientList (void)
{
int i;
if (NumPatients == 0)
{
cout << "Queue is empty";
}
else
{
for (i=0; i<NumPatients; i++)
{
cout << "" << PatientList[i].FName;
cout << " " << PatientList[i].LName;
cout << " " << PatientList[i].ID;
}
}
}
person IpPerson(void)
{
person p;
cout << "Please enter data for new person First name: ";
cin.getline(p.FName, sizeof(p.FName));
cout << "Last name: ";
cin.getline(p.LName, sizeof(p.LName));
cout << "Social security number: ";
cin.getline(p.ID, sizeof(p.ID));
if (p.FName[0]==0 || p.LName[0]==0 || p.ID[0]==0)
{
strcpy(p.ID,"");
cout << "Error: Data not valid. Operation cancelled.";
getch();
}
return p;
}
void OpPerson(person* p)
{
if (p == NULL || p->ID[0]==0)
{
cout << "No patient";
return;
}
else
cout << "Persondata:";
cout << "First name: " << p->FName;
cout << "Last name: " << p->LName;
cout << "Social security number: " << p->ID;
}
int NumRead()
{
char buff[20];
cin.getline(buff, sizeof(buff));
return atoi(buff);
}
void DptMenu (hospQueue * q)
{
int choice = 0, succ;
person p;
while (choice != 6)
{
clrscr();
cout << "Welcome to department: " << q->DeptName;
cout << "Please enter your choice:";
cout << "1: Add normal patient ";
cout << "2: Add critically ill patient ";
cout << "3: Take out personfor operation ";
cout << "4: Remove dead personfrom hospQueue ";
cout << "5: PatientList hospQueue ";
cout << "6: Change department or exit ";
choice = NumRead();
switch (choice)
{
case 1:
p = IpPatient();
if (p.ID[0])
{
succ = q->LastAddPatient(p);
clrscr();
if (succ)
{
cout << "Patient added:";
}
else
{
cout << "Error: The hospQueue is full. Cannot add patient:";
}
OpPatient(&p);
cout << "Press any key";
getch();
}
break;
case 2:
p = IpPatient();
if (p.ID[0])
{
succ = q->BegingAddPatient(p);
clrscr();
if (succ)
{
cout << "Patient added:";
}
else
{
cout << "Error: The hospQueue is full. Cannot add patient:";
}
OpPatient(&p);
cout << "Press any key";
getch();
}
break;
case 3:
p = q->GetNxtPatient();
clrscr();
if (p.ID[0])
{
cout << "Patient to operate:";
OpPatient(&p);}
else
{
cout << "There is no patient to operate.";
}
cout << "Press any key";
getch();
break;
case 4:
p = IpPatient();
if (p.ID[0])
{
succ = q->RemvDeadPatient(&p);
clrscr();
if (succ)
{
cout << "Patient removed:";
}
else
{
cout << "Error: Cannot find patient:";
}
OpPatient(&p);
cout << "Press any key";
getch();
}
break;
case 5:
clrscr();
q->OutPatientList();
cout << "Press any key";
getch(); break;
}
}
}
void main ()
{
int i, ChoiceMenu = 0;
hospQueue depts[3];
strcpy (depts[0].DeptName, "Heart clinic");
strcpy (depts[1].DeptName, "Lung clinic");
strcpy (depts[2].DeptName, "Plastic surgery");
while (ChoiceMenu != 4)
{
clrscr();
cout << "Welcome to Software City Hospital";
cout << "Please enter your choice:";
for (i = 0; i < 3; i++)
{
cout << "" << (i+1) << ": " << depts[i].DeptName;
}
cout << "4: Exit";
ChoiceMenu = NumRead();
if (ChoiceMenu >= 1 && ChoiceMenu <= 3)
{
DptMenu (depts + (ChoiceMenu-1));
}
}
}