// TODO: PROGRAM NAME // // This program was written by INSERT-NAME-HERE zXXXXXXX // on INSERT-DATE-HERE // // TODO: Program description #include #include #include #include //////////////////////////////////////////////////////////////////////////////// /////////////////////////// Constants /////////////////////////////// //////////////////////////////////////////////////////////////////////////////// // provided constants #define MAX_NAME_LEN 20 #define INVALID_PET_TYPE -1 #define CAPACITY 10 // all different types of pets that can be found in rooms enum pet_type { CAT, DOG, RABBIT, PARROT, }; // TODO: Put your additional #defines/enums here! //////////////////////////////////////////////////////////////////////////////// ////////////////////////// USER DEFINED TYPES //////////////////////////////// //////////////////////////////////////////////////////////////////////////////// // finance of the salon struct financial_summary { int total_cared; double total_profit; }; // salon overview struct salon { char salon_name[MAX_NAME_LEN]; struct financial_summary summary; double base_cost; struct pet_room *rooms; struct salon *next; }; // room that can be found in salon struct pet_room { char room_name[MAX_NAME_LEN]; enum pet_type pet_type; int num_pets; struct pet_room *next; }; // TODO: Put any additional structs here! //////////////////////////////////////////////////////////////////////////////// ////////////////////////// FUNCTION PROTOTYPES //////////////////////////////// //////////////////////////////////////////////////////////////////////////////// struct salon *create_salon(char salon_name[MAX_NAME_LEN], double base_cost); struct pet_room *create_room(char room_name[MAX_NAME_LEN], enum pet_type pet_type); void print_one_room(int position, struct pet_room *room); void print_pet_type(enum pet_type pet_type); void print_salon_stats(struct salon *salon, int selected); void print_total_stats(double profit, int pets); void print_added_pets(int num_cats, int num_dogs, int num_rabbits, int num_parrots); void scan_name(char string[MAX_NAME_LEN]); enum pet_type scan_pet_type(); // You won't need to be calling the below functions. They are used to help // you implement some of the provided helper functions! int scan_token(char *buffer, int buffer_size); enum pet_type string_to_pet_type(char *pet_type); // TODO: Put your function prototypes here! //////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// int main(void) { printf("Welcome to 1091 CS Pet Salon manager! =^.^=\n"); // TODO: Start 1.2 here! printf("\nAll pet salons closed! =^.^=\n"); return 0; } //////////////////////////////////////////////////////////////////////////////// ///////////////////////////// FUNCTIONS ////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// // TODO: what does this function do? // // Parameters: // TODO: explain what your parameters are here! // Returns: // TODO: explain what your function returns here! struct salon *create_salon(char salon_name[MAX_NAME_LEN], double base_cost) { // TODO: STAGE 1.1 // TODO: malloc, initialise, and return a new salon. // hint: you will have to replace NULL in this return statement. return NULL; } // TODO: what does this function do? // // Paramters: // TODO: explain what your parameters are here! // Returns: // TODO: explain what your function returns here! struct pet_room *create_room(char room_name[MAX_NAME_LEN], enum pet_type pet_type) { // STAGE 1.1 // TODO: malloc, initialise, and return a new room. // hint: you will have to replace NULL in this return statement. return NULL; } // TODO: Put your additional functions here! //////////////////////////////////////////////////////////////////////////////// /////////////////////////// PROVIDED FUNCTIONS /////////////////////////////// //////////////////////////////////////////////////////////////////////////////// // Prints a single room, displaying name, position, pet_type, number of pets and // cost of care for that room // // Parameters: // position - the position of the room in the salon (the first // room will be room 1, second will be 2, etc) // room - a pointer to the struct pet_room // // Returns: // None void print_one_room(int position, struct pet_room *room) { printf("/-------------------------------\\\n"); printf("Room name: %s\n", room->room_name); printf(" Room position: %d\n", position); printf(" Pet type: "); print_pet_type(room->pet_type); printf("\n Num of pets in room: %d/10\n", room->num_pets); printf("\\-------------- | --------------/\n"); printf(" V\n"); } // Print the statistics of one salon // // Parameters: // salon - a pointer to the struct salon // selected - an integer indicating if the salon is 'selected' or not // Returns: // None // void print_salon_stats(struct salon *salon, int selected) { printf("/-------------------------------\\\n"); printf("Stats from salon: %s", salon->salon_name); if (selected == 1) { printf(" (selected)"); } printf("\n Salon's base cost: %.2lf\n", salon->base_cost); printf(" Total pets cared for: %d\n", salon->summary.total_cared); printf(" Total profit from cared pets: %.2lf\n", salon->summary.total_profit); printf("\\-------------- | --------------/\n"); printf(" V\n"); } // Print the statistics of all the salons combined // // Parameters: // profit - amount of money the salons has made // pets - number of pets that have been cared for by the salons // Returns: // None // void print_total_stats(double profit, int pets) { printf("/-------------------------------\\\n"); printf("Combined stats from all salons!\n"); printf(" Total pets cared for: %d\n", pets); printf(" Total profit from cared pets: %.2lf\n", profit); printf("\\-------------------------------/\n"); } // Print type of pet as a string // // Parameters: // pet_type - the type of pet // Returns: // None // void print_pet_type(enum pet_type pet_type) { if (pet_type == CAT) { printf("cat"); } else if (pet_type == DOG) { printf("dog"); } else if (pet_type == RABBIT) { printf("rabbit"); } else if (pet_type == PARROT) { printf("parrot"); } } // Print the amount of pets being added to the salon // // Parameters: // num_cats - number of additional cats added // num_dogs - number of additional dogs added // num_rabbits - number of additional rabbits added // num_parrots - number of additional parrots added // Returns: // None // void print_added_pets(int num_cats, int num_dogs, int num_rabbits, int num_parrots) { printf("/-------------------------------\\\n"); printf(" Cats added: %d\n", num_cats); printf(" Dogs added: %d\n", num_dogs); printf(" Rabbits added: %d\n", num_rabbits); printf(" Parrots added: %d\n", num_parrots); printf("\\-------------------------------/\n"); } // Scan for a name // // Parameters: // string - the string to be scanned // Returns: // None // void scan_name(char string[MAX_NAME_LEN]) { scan_token(string, MAX_NAME_LEN); } // Scan an enum pet_type // // Parameters: // None // Returns: // enum pet_type variable // enum pet_type scan_pet_type() { char pet_type[MAX_NAME_LEN]; scan_token(pet_type, MAX_NAME_LEN); return string_to_pet_type(pet_type); } //////////////////////////////////////////////////////////////////////////////// ////////////////////// ADDITIONAL PROVIDED FUNCTIONS /////////////////////////// //////////////////////////////////////////////////////////////////////////////// // You don't need to use any of these, or understand how they work! // We use them to implement some of the provided helper functions. // Scan a token into the provided buffer of buffer_size // // Parameters: // buffer - the buffer being scanned into // buffer_size - the size of buffer // Returns: // the number of chars scanned // int scan_token(char *buffer, int buffer_size) { if (buffer_size == 0) { return 0; } char c; int i = 0; int num_scanned = 0; // consume all leading whitespace scanf(" "); // scan in characters until whitespace while (i < buffer_size - 1 && (num_scanned = scanf("%c", &c)) == 1 && !isspace(c)) { buffer[i++] = c; } if (i > 0) { buffer[i] = '\0'; } return num_scanned; } // Convert a string into an enum pet_type // // Parameters: // pet_type - a string version of the pet type // Returns: // enum pet_type // enum pet_type string_to_pet_type(char *pet_type) { int len = strlen(pet_type); if (strncasecmp(pet_type, "cat", len) == 0) { return CAT; } if (strncasecmp(pet_type, "dog", len) == 0) { return DOG; } if (strncasecmp(pet_type, "rabbit", len) == 0) { return RABBIT; } if (strncasecmp(pet_type, "parrot", len) == 0) { return PARROT; } return INVALID_PET_TYPE; }