Demonstration of the use of structures in C. Inspiration to learn object-oriented programming for a more elegant solution.

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers patients.h Source File

patients.h

00001 // Some functions use the serial monitor so define it here.
00002 Serial pc(USBTX,USBRX);
00003 
00004 // The patient_record structure stores name and vitals for a patient.
00005 struct patient_record {
00006     char firstname[30];
00007     char lastname[30];
00008     int systolic;
00009     int diastolic;
00010     float spO2;
00011 };
00012 
00013 // It is assumed that we will have an array of these patient records.
00014 // We define the maximum number of patients that we will support.
00015 #define MAX_PATIENTS 100
00016 
00017 //Print the values in the structure for a particular patient.
00018 void print_patient_record(struct patient_record *first_patient, int N) {
00019     pc.printf("%s %s \t ID: %d\n",first_patient[N].firstname, first_patient[N].lastname,N);
00020     pc.printf("BP: %d/%d  SpO2: %f\n",first_patient[N].systolic, first_patient[N].diastolic, first_patient[N].spO2);
00021 }   
00022 
00023 //Put patient into record.
00024 void newPatient(struct patient_record *patient,  char *first, char*last)
00025 {
00026     // We can't just set the firstname pointer equal to the
00027     // first pointer; that doesn't actually move the information into
00028     // the structure.  We need to copy each letter of the patient's name
00029     // into the structure.  strcpy does this.
00030     strcpy(patient->firstname, first);
00031     strcpy(patient->lastname,last);
00032 }
00033 
00034 //Fill in vital signs directly.  Note that we use -> when we have a
00035 //pointer to a struct and we want to access one of the members.
00036 void recordBP(struct patient_record *patient, int sys, int dias, float o2) {
00037     patient->systolic=sys;
00038     patient->diastolic=dias;
00039     patient->spO2 = o2;
00040 }
00041 
00042 //Fill in vital signs through the serial monitor (for BP) and the oxygen sensor,
00043 //assumed to be an analog input giving the SpO2 level as a fraction.
00044 void recordBPinteractive(struct patient_record *first_patient, AnalogIn oxygen) {
00045     int N;
00046     int s;
00047     int d;
00048     
00049     pc.printf("Enter the patient number:\n");
00050     pc.scanf("%d",&N);
00051     pc.printf("Enter the systolic BP:\n");
00052     pc.scanf("%d",&s);
00053     pc.printf("Enter the diastolic BP:\n");
00054     pc.scanf("%d",&d);
00055     
00056     //Note that [N] turns first_patient into a struct rather than a
00057     //pointer to a struct (dereferences the pointer).  Therefore, we
00058     //use . to access members.
00059     first_patient[N].systolic=s;
00060     first_patient[N].diastolic=d;
00061     first_patient[N].spO2=oxygen;
00062     
00063     //Print information to confirm
00064     pc.printf("Data entered:\n");
00065     print_patient_record(first_patient,N);
00066 }      
00067 
00068 //Look up the patient number by searching through the array of patients
00069 //and matching the first and last name.  strcmp returns 0 if two strings
00070 //are identical.  Return the patient's position in the array if name is
00071 //found, -1 if it is not found.
00072 int lookup_patient_name(struct patient_record *first_patient,char*first,char*last)
00073 {
00074     for(int k=0; k<MAX_PATIENTS; k++) {
00075         if(strcmp(first_patient[k].lastname,last) == 0) {
00076             if(strcmp(first_patient[k].firstname,first) == 0) {
00077                 return k;
00078             }
00079         }
00080     }
00081     return -1;
00082 }
00083