
Patrick B. answered 05/18/21
Math and computer tutor/teacher
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define PATIENT_STR_SIZE (18)
typedef struct _TPatient
{
char socialSecurityNumber[PATIENT_STR_SIZE];
char dob[PATIENT_STR_SIZE];
double balance;
long accountNum;
int status;
} * TPatient;
#define PATIENT_SIZE (sizeof(struct _TPatient))
#define MAX_NUM_PATIENTS (100)
struct _TPatient patients[MAX_NUM_PATIENTS];
typedef int (*PatientCompareCallback) (const void *,const void *);
int ComparePatientByStatus( const void * rec1, const void * rec2)
{
TPatient patientRecPtr1 = (TPatient) rec1;
TPatient patientRecPtr2 = (TPatient) rec2;
return ( patientRecPtr1->status - patientRecPtr2->status);
}
int ComparePatient( const void * rec1, const void * rec2)
{
TPatient patientRecPtr1 = (TPatient) rec1;
TPatient patientRecPtr2 = (TPatient) rec2;
return ( strcmp(patientRecPtr1->socialSecurityNumber,patientRecPtr2->socialSecurityNumber));
}
void PatientSort(TPatient patients,int n,PatientCompareCallback patientCompareFuncPtr )
{
qsort(patients,n,PATIENT_SIZE,patientCompareFuncPtr);
}
Patient_Dump(TPatient patientRec,char * strMsg)
{
if (strMsg!=NULL)
{
printf("**********************************************\n");
printf(strMsg); printf("\n");
}
printf("***********************************************\n");
printf(" social security # >%s< \n",patientRec->socialSecurityNumber);
printf(" dob >%s< \n",patientRec->dob);
printf(" balance >%6.2f< \n",patientRec->balance);
printf(" account # >%ld< \n",patientRec->accountNum);
printf(" status = %d \n",patientRec->status);
}
void DumpPatientList(TPatient patients, int n, char * strMsg)
{
int i;
char strMsgRecNum[55];
if (strMsg!=NULL)
{
printf("************************************************\n");
printf(strMsg); printf("\n");
}
printf("************************************************\n");
for (i=0; i<n; i++)
{
sprintf(strMsgRecNum,"Patient rec # %d of %d",(i+1),n);
Patient_Dump(&patients[i],strMsgRecNum);
}
}
int main()
{
struct _TPatient patients[]= {
{"123-45-6789","01/02/2003",1234.56,24680,8},
{"987-65-4321","04/05/2006",2345.67,13579,6},
{"246-80-1234","07/08/2010",1357.99,80462,4},
{"135-79-4321","09/11/2013",8642.99,97531,2}
};
PatientSort(patients,4,ComparePatientByStatus);
DumpPatientList(patients,4,(char*)"SORTED BY STATUS");
printf("------------------------------------------------------------------\n");
printf("------------------------------------------------------------------\n");
PatientSort(patients,4,ComparePatient);
DumpPatientList(patients,4,(char*)"SORTED BY SOCIAL SECURITY #");
}