Question

// Write the compiler used: Visual studio // READ BEFORE YOU START: // You are given...

// Write the compiler used: Visual studio

// READ BEFORE YOU START:
// You are given a partially completed program that creates a list of patients, like patients' record.
// Each record has this information: patient's name, doctor's name, critical level of patient, room number.
// The struct 'patientRecord' holds information of one patient. Critical level is enum type.
// An array of structs called 'list' is made to hold the list of patients.
// To begin, you should trace through the given code and understand how it works.
// Please read the instructions above each required function and follow the directions carefully.
// You should not modify any of the given code, the return types, or the parameters, you risk getting compile error.
// You are not allowed to modify main ().
// You can use string library functions.

// WRITE COMMENTS FOR IMPORANT STEPS IN YOUR CODE.


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#pragma warning(disable: 4996) // for Visual Studio Only

#define MAX_PATIENTS 15
#define MAX_NAME_LENGTH 25

typedef enum { very_critical = 0, critical, not_critical } criticalType; // enum type

struct patientRecord {                   // struct for patient details
   char patientName[MAX_NAME_LENGTH];
   char doctorName[MAX_NAME_LENGTH];
   criticalType criticalLevel;
   unsigned int roomNumber;
};

struct patientRecord list[MAX_PATIENTS]; // declare list of patients
int count = 0;                           // the number of patients currently stored in the list (initialized to 0)

// functions already implmented
void flushStdIn();
void executeAction(char);
void save(char* fileName);

// functions that need implementation:
int add(char* patientName_input, char* doctorName_input, char* criticalLevel_input, unsigned int roomNumber_input); // 20 points
void display();               // 10 points
void sort();               // 10 points
void load(char* fileName);   // 10 points

int main()
{
   char* fileName = "Patient_List.txt";
   load(fileName);   // load list of patients from file (if it exists). Initially there will be no file.
   char choice = 'i';       // initialized to a dummy value
   do
   {
       printf("\nEnter your selection:\n");
       printf("\t a: add a new patient\n");
       printf("\t d: display patient list\n");
       printf("\t s: sort patient list by name\n");
       printf("\t q: quit\n");
       choice = getchar();
       flushStdIn();
       executeAction(choice);
   } while (choice != 'q');

   save(fileName); // save list of patients to file (overwrites file, if it exists)
   return 0;
}

// flush out leftover '\n' characters
void flushStdIn()
{
   char c;
   do c = getchar();
   while (c != '\n' && c != EOF);
}

// ask for details from user for the given selection and perform that action
void executeAction(char c)
{
   char patientName_input[MAX_NAME_LENGTH], doctorName_input[MAX_NAME_LENGTH];
   unsigned int roomNumber_input, add_result = 0;
   char criticalLevel_input[20];
   switch (c)
   {
   case 'a':
       // input patient record from user
       printf("\nEnter patient name: ");
       fgets(patientName_input, sizeof(patientName_input), stdin);
       patientName_input[strlen(patientName_input) - 1] = '\0';   // discard the trailing '\n' char
       printf("Enter doctor name: ");
       fgets(doctorName_input, sizeof(doctorName_input), stdin);
       doctorName_input[strlen(doctorName_input) - 1] = '\0';   // discard the trailing '\n' char

       printf("Enter whether patient is 'very critical' or 'critical' or 'not critical': ");
       fgets(criticalLevel_input, sizeof(criticalLevel_input), stdin);
       criticalLevel_input[strlen(criticalLevel_input) - 1] = '\0';   // discard the trailing '\n' char
       printf("Please enter room number: ");
       scanf("%d", &roomNumber_input);
       flushStdIn();

       // add the patient to the list
       add_result = add(patientName_input, doctorName_input, criticalLevel_input, roomNumber_input);
       if (add_result == 0)
           printf("\nPatient is already on the list! \n\n");
       else if (add_result == 1)
           printf("\nPatient successfully added to the list! \n\n");
       else
           printf("\nUnable to add. Patient list is full! \n\n");

       break;
   case 'd': display();   break;
   case 's': sort();       break;

   case 'q': break;
   default: printf("%c is invalid input!\n", c);
   }
}

// Q1 : add (20 points)
// This function is used to add a patient into the list. You can simply add the new patient to the end of list (array of structs).
// Do not allow the patient to be added to the list if it already exists in the list. You can do that by checking patient names already in the list.
// If the patient already exists then return 0 without adding it to the list. If the patient does not exist in the list then go on to add the patient at the end of the list and return 1.
// If patient list is full, then do not add new patientto the list and return 2.
// NOTE: Notice how return type of add() is checked in case 'a' of executeAction()
// NOTE: You must convert the string 'criticalLevel_input' to an enum type and store it in the list because the critical level has enum type (not string type).
// The list should be case sensitive. For instance, 'Roger' and 'roger' should be considered two different names.
// Hint: 'count' holds the number of patients currently in the list
int add(char* patientName_input, char* doctorName_input, char* criticalLevel_input, unsigned int roomNumber_input)
{

   return 0;           // edit this line as needed
}


// Q2 : display (10 points)
// This function displays the patient list with the details (struct elements) of each patient.
// Parse through the list and print the patient details one after the other. See expected output screenshots in question pdf file.
// NOTE: Critical level is stored in the struct as enum type. You need to display 'very critical','critical' or 'not critical'
void display()
{

}

// Q3 : sort (10 points)
// This function is used to sort the list(array of structs) alphabetically by patient name.
// Parse the list and compare the patient names to check which one should appear before the other in the list.
// Sorting should happen within the list. That is, you should not create a new array of structs having sorted patients.
// Hint: One of the string library function can be useful to implement this function because the sorting needs to happen by patient name which is a string.
//       Use a temp struct (already declared) if you need to swap two structs in your logic
void sort()
{
   struct patientRecord patientTemp;   // needed for swapping structs. Not absolutely necessary to use.
   // enter code here


   // display message for user to check the result of sorting.
   printf("\nPatient list sorted! Use display option 'd' to view sorted list.\n");
}

// save() is called at the end of main()
// This function saves the array of structures to file. It is already implemented.
// You should read and understand how this code works. It will help you with 'load()' function.
// save() is called at end of main() to save the patient list to a file.
// The file is saved at the same place as your C file. For VS, the default directory looks like this:
// C:\Users\<username>\Documents\Visual Studio 2017\Projects\Project1\Project1
// You can simply delete the file to 'reset the list' or to avoid loading from it.
void save(char* fileName)
{
   FILE* file;
   int i, criticalLevelValue = 0;
   file = fopen(fileName, "wb");       // open file for writing

   fwrite(&count, sizeof(count), 1, file);       // First, store the number of patients in the list

   // Parse the list and write patient record to file
   //
   for (i = 0; i < count; i++)
   {
       fwrite(list[i].patientName, sizeof(list[i].patientName), 1, file);
       fwrite(list[i].doctorName, sizeof(list[i].doctorName), 1, file);
       // convert enum to a number for storing
       if (list[i].criticalLevel == very_critical)
           criticalLevelValue = 0;       // 0 for very_critical
       else if (list[i].criticalLevel == critical)
           criticalLevelValue = 1;       // 1 for critical
       else
           criticalLevelValue = 2;       // 2 for not_critical

       fwrite(&criticalLevelValue, sizeof(criticalLevelValue), 1, file);
       fwrite(&list[i].roomNumber, sizeof(list[i].roomNumber), 1, file);
   }

   fclose(file);           // close the file after writing
}

// Q4: load (10 points)
// This function is called in the beginning of main().
// This function reads the patient list from the saved file and builds the array of structures 'list'.
// In the first run of the program, there will be no saved file because save() is called at the end of program.
// So at the begining of this function, write code to open the file and check if it exists. If file does not exist, then return from the function.
// (See expected output of add() in homework question file. It displays "Patient_List.txt not found" because the file did not exist initially.)
// If the file exists, then parse the patient list to read the patient details from the file.
// Use the save function given above as an example of how to write this function. Notice the order in which the struct elements are saved in save()
// You need to use the same order to read the list back.
// NOTE: The saved file is not exactly readable because all elements of the struct are not string or char type.
//       So you need to implement load() similar to how save() is implemented. Only then the 'list' will be loaded correctly.
//       You can simply delete the file to 'reset the list' or to avoid loading from it.
void load(char* fileName)
{

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

int add(char *patientName_input, char *doctorName_input, char *criticalLevel_input, unsigned int roomNumber_input){
   if(count >= MAX_PATIENTS)
       return 2; // Size of array exceeds the maximum size.
   else{  
       int i;
       for(i=0; i<count; i++){
           if(list[i].patientName == patientName_input){
               return 1;
               // Name already exists.
           }
       }
       // Name doesnot exists in the list; insert it.
       list[count].patientName = patientName_input;
       list[count].doctorName = doctorName_input;
       list[count].criticalLevel = (criticalType) criticalLevel_input;
       list[count].roomNumber = roomNumber_input;
       count += 1;
       return 0;
   }
}

void display(){
   int i;
   for(i=0; i<count; i++){
       printf("Patient Name: %s\n", list[i].patientName);
       printf("Doctor Name: %s\n", list[i].doctorName);
       printf("CriticalLevel: %d\n", list[i].criticalLevel);
       // %d will autocast the enum values to integer values.
       printf("Room Number: %u\n", roomNumber);
       printf("\n"); // For proper formattings.
   }
}

void sort(){
   struct patientRecord patientTemp;
   int i;
   for(i=1; i<count; i++)
      for(j=0; j<count-i; j++){
         if(strcmp(list[j].patientName, list[j+1].patientName)>0){
            patientTemp = list[j];
            list[j] = list[j+1];
            list[j+1] = patientTemp;
            // Direct structure comparisons are possible in modern compilers and in C18 standard.
         }
      }   print("\nPatient list sorted! Use display option 'd' to view sorted list.\n");
}

void load(char *fileNamee){
   int i, criticalLevelValue=0;
    file = fopen(fileName, fileNamee);
    // Changed parameter for avoiding confusions.
   
    fread(&count, sizeof(count), 1, file);       /

    for(i = 0; i < count; i++){
        fread(list[i].patientName, sizeof(list[i].patientName), 1, file);
        fread(list[i].doctorName, sizeof(list[i].doctorName), 1, file);
     
        if (list[i].criticalLevel == very_critical)
           criticalLevelValue = 0;     
        else if (list[i].criticalLevel == critical)
           criticalLevelValue = 1;     
        else
           criticalLevelValue = 2;     

        fread(&criticalLevelValue, sizeof(criticalLevelValue), 1, file);
        fread(&list[i].roomNumber, sizeof(list[i].roomNumber), 1, file);
   }

   fclose(file);
}

Add a comment
Know the answer?
Add Answer to:
// Write the compiler used: Visual studio // READ BEFORE YOU START: // You are given...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • // READ BEFORE YOU START: // You are given a partially completed program that creates a...

    // READ BEFORE YOU START: // You are given a partially completed program that creates a list of students for a school. // Each student has the corresponding information: name, gender, class, standard, and roll_number. // To begin, you should trace through the given code and understand how it works. // Please read the instructions above each required function and follow the directions carefully. // If you modify any of the given code, the return types, or the parameters, you...

  • I JUST NEED HELP WITH DISPLAY PART! please help! thanks in advance // This function saves...

    I JUST NEED HELP WITH DISPLAY PART! please help! thanks in advance // This function saves the array of structures to file. It is already implemented for you. // You should understand how this code works so that you know how to use it for future assignments. void save(char* fileName) { FILE* file; int i; file = fopen(fileName, "wb"); fwrite(&count, sizeof(count), 1, file); for (i = 0; i < count; i++) { fwrite(list[i].name, sizeof(list[i].name), 1, file); fwrite(list[i].class_standing, sizeof(list[i].class_standing), 1, file);...

  • Hello! I'm posting this program that is partially completed if someone can help me out, I...

    Hello! I'm posting this program that is partially completed if someone can help me out, I will give you a good rating! Thanks, // You are given a partially completed program that creates a list of employees, like employees' record. // Each record has this information: employee's name, supervisors's name, department of the employee, room number. // The struct 'employeeRecord' holds information of one employee. Department is enum type. // An array of structs called 'list' is made to hold...

  • // C code // If you modify any of the given code, the return types, or...

    // C code // If you modify any of the given code, the return types, or the parameters, you risk getting compile error. // Yyou are not allowed to modify main (). // You can use string library functions. #include <stdio.h> #include <stdlib.h> #include <string.h> #pragma warning(disable: 4996) // for Visual Studio #define MAX_NAME 30 // global linked list 'list' contains the list of patients struct patientList {    struct patient *patient;    struct patientList *next; } *list = NULL;  ...

  • // CSE240 Spring 2019 HW 7 & 8 // Write your name here // Write the...

    // CSE240 Spring 2019 HW 7 & 8 // Write your name here // Write the compiler used: Visual studio or gcc // READ BEFORE YOU START: // You are given a partially completed program that creates a linked list of patient information. // The global linked list 'list' is a list of patients with each node being struct 'patientList'. // 'patientList' consists of struct 'patient' which has: patient name, room number, and a linked list of 'doctors'. // The...

  • Need help for C program. Thx #include <stdio.h> #include <string.h> #include <ctype.h> // READ BEFORE YOU...

    Need help for C program. Thx #include <stdio.h> #include <string.h> #include <ctype.h> // READ BEFORE YOU START: // This homework is built on homework 06. The given program is an updated version of hw06 solution. It begins by displaying a menu to the user // with the add() function from the last homework, as well as some new options: add an actor/actress to a movie, display a list of movies for // an actor/actress, delete all movies, display all movies,...

  • Hardware Inventory – Write a database to keep track of tools, their cost and number. Your...

    Hardware Inventory – Write a database to keep track of tools, their cost and number. Your program should initialize hardware.dat to 100 empty records, let the user input a record number, tool name, cost and number of that tool. Your program should let you delete and edit records in the database. The next run of the program must start with the data from the last session. This is my program so far, when I run the program I keep getting...

  • Need this in C The starter code is long, if you know how to do it...

    Need this in C The starter code is long, if you know how to do it in other way please do. Do the best you can please. Here's the starter code: // ----------------------------------------------------------------------- // monsterdb.c // ----------------------------------------------------------------------- #include #include #include // ----------------------------------------------------------------------- // Some defines #define NAME_MAX 64 #define BUFFER_MAX 256 // ----------------------------------------------------------------------- // Structs typedef struct { char name[NAME_MAX]; int hp; int attackPower; int armor; } Character; typedef struct { int size; Character *list; } CharacterContainer; // ----------------------------------------------------------------------- //...

  • ASSIGNMENT DUE DATE GOT PUSHED BACK TO LATE THIS WEEK. PLEASE READ COMMENTS AND CODE BEFORE...

    ASSIGNMENT DUE DATE GOT PUSHED BACK TO LATE THIS WEEK. PLEASE READ COMMENTS AND CODE BEFORE ANSWERING CODING SECTIONS HW07 #Q1-Q5 HW08 #Q1-Q2 // READ BEFORE YOU START: // Please read the given Word document for the project description with an illustrartive diagram. // You are given a partially completed program that creates a list of students for a school. // Each student has the corresponding information: name, standard, and a linked list of absents. // Please read the instructions...

  • IN C ONLY As mentioned earlier there are two changes we are going to make from...

    IN C ONLY As mentioned earlier there are two changes we are going to make from lab 5, The file you read into data structures can be any length. studentInfo array will be stored in another struct called studentList that will contain the Student pointer and current length of the list. Sometimes data can be used in structs that correlate between variables so it's convenient to store the data in the same struct. Instead of tracking a length variable all...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT