Untitled
unknown
plain_text
2 years ago
6.2 kB
2
Indexable
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <assert.h> #include <stdbool.h> #define MAXLENGTH 11 #define MAXCOUNT 5 // Maximum number of names typedef struct { char *name; // Change name to a char pointer long int birthday; int count; } NameEntry; void flushInputBuffer(); bool isValidDate(long int date); int getBirthdayInput(); int main() { int count = 0; NameEntry *names[MAXCOUNT]; char nameInput[MAXLENGTH]; char intro[] = "Names Entry and Sorting Program\nPlease input name (max: 5 entries, enter '0' to finish).\n"; char nameInputPrompt[] = " Name (First Last): "; char birthdayInputPrompt[] = " Birthday (YYYYMMDD): "; char errorMessage[] = "Incorrect name or birthday format.\n"; char sortedNamesTitle[] = "\nSorted Names:\n"; int error = 0; int exitInput = 0; // Variable to track '0' input long int birthdayInput; printf(intro); while (count < MAXCOUNT) { printf(nameInputPrompt); if (fgets(nameInput, sizeof(nameInput), stdin) != NULL) { // Manually remove the newline character if present size_t length = strlen(nameInput); if (length > MAXLENGTH) { continue; } if (strcmp(nameInput, "0\n") == 0) { exitInput = 1; // User entered '0', set the exit flag break; // Exit the loop } if (length == 1 && nameInput[0] == '\n') { printf("Empty string"); return 0; } else if (strlen(nameInput) > 0 && strlen(nameInput) < MAXLENGTH ) { // Allocate memory for a new NameEntry struct names[count] = (NameEntry *)malloc(sizeof(NameEntry)); if (names[count] == NULL) { printf("Memory allocation failed\n"); exit(EXIT_FAILURE); } // Allocate memory for the name member (character array) and copy the input names[count]->name = (char *)malloc(MAXLENGTH); if (names[count]->name == NULL) { printf("Memory allocation failed\n"); free(names[count]); // Free the previously allocated struct exit(EXIT_FAILURE); } names[count]->count = count + 1; names[count]->birthday = 0; // Initialize birthday to zero strncpy(names[count]->name, nameInput, MAXLENGTH); long int birthdayInput = getBirthdayInput(); names[count]->birthday = birthdayInput; // Display the information in the specified order and format printf("[%-10s] [%-10ld] Entry count: [%d] malloc-address: [%p] name-address: [%p]\n", names[count]->name, names[count]->birthday, names[count]->count, (void *)names[count], (void *)(names[count]->name)); count++; } else { printf("Name is too long or empty\n"); } } else { printf("Input error\n"); exit(EXIT_FAILURE); } } if (!error || exitInput) { // Bubble sort the names int i, j; for (i = 0; i < count - 1; i++) { for (j = i + 1; j < count; j++) { if (strcmp(names[i]->name, names[j]->name) > 0) { // Swap the pointers to NameEntry structs NameEntry *temp = names[i]; names[i] = names[j]; names[j] = temp; } } } printf(sortedNamesTitle); for (i = 0; i < count; i++) { // Display the information in the specified order and format printf("[Name: %-10s] [Birthday: %ld] [Entry count: %d]\n [Name Address: %p] [NameEntry Address: %p]\n", names[i]->name, names[i]->birthday, i + 1, (void *)names[i], (void *)(names[i]->name)); // Free the dynamically allocated name member free(names[i]->name); // Free the dynamically allocated NameEntry struct free(names[i]); } } return 0; } int getBirthdayInput() { long int birthday; char buffer[100]; // Buffer to read user input while (1) { printf(" Birthday (YYYYMMDD): "); if (fgets(buffer, sizeof(buffer), stdin) != NULL) { // Remove trailing newline character, if present if (buffer[strlen(buffer) - 1] == '\n') { buffer[strlen(buffer) - 1] = '\0'; } // Check if the input is empty (only Enter key) if (strlen(buffer) == 0) { printf("Invalid birthday format\n"); continue; // Prompt again } // Attempt to convert the input to a long integer if (sscanf(buffer, "%ld", &birthday) != 1) { printf("Invalid birthday format\n"); continue; } if (birthday < 9) { printf("Invalid birthday format\n"); continue; } else if (!isValidDate(birthday)) { printf("Invalid date\n"); continue; } else { return birthday; } } else { printf("Input error\n"); return 0; // Exit with an error code } // Clear the input buffer flushInputBuffer(); } } void flushInputBuffer() { int c; while ((c = getchar()) != '\n' && c != EOF); } bool isValidDate(long int date) { int year = date / 10000; int month = (date / 100) % 100; int day = date % 100; if (year < 0000 || year > 9999) { return false; // Invalid year } if (month < 1 || month > 12) { return false; // Invalid month } int maxDays[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; if (day < 1 || day > maxDays[month]) { return false; } return true; }
Editor is loading...