Untitled

 avatar
unknown
plain_text
2 years ago
12 kB
7
Indexable
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>

#define MAXLENGTH 11
#define MAXCOUNT 5

typedef char* STRING;

struct Person {
    STRING name;
    long int birthday[9]; // 8 characters for YYYYMMDD + null terminator
};

int is_valid_date(const char *date) {
    // Validate the date format
    if (strlen(date) != 8)
        return 0;
    
    // Extract year, month, and day
    int year, month, day;
    if (sscanf(date, "%4d%2d%2d", &year, &month, &day) != 3)
        return 0;

    // Validate year
    if (year < 0 || year > 9999)
        return 0;

    // Validate month
    if (month < 1 || month > 12)
        return 0;

    // Validate day based on the month
    if ((month == 2 && (day < 1 || day > 28)) ||
        ((month == 4 || month == 6 || month == 9 || month == 11) && (day < 1 || day > 30)) ||
        (day < 1 || day > 31))
        return 0;

    return 1; // Date is valid
}

void display_person(struct Person *person, int entry_count) {
    printf("%-10s  %-8s Entry count: %d free-address: %p -> %p\n",
           person->name, person->birthday, entry_count, person, person->name);
}

int main() {
    // Variables
    int count = 0;
    struct Person people[MAXCOUNT];
    STRING nameInput[MAXLENGTH];
    STRING intro = "Names Entry and Sorting Program\nPlease input name (max: 5 entries).\n";
    STRING nameInputPrompt = "  Name (First Last): ";
    STRING nameTemplate = "%-10s Entry count: %2d address: %p\n";
    STRING sortedNamesTitle = "\nSorted Names:\n";
    STRING errorMessage = "Incorrect name format.\n";
    STRING errorDateFormatMessage = "Date format error! Please re-enter date (YYYYMMDD).\n";
    STRING errorEmptyDateMessage = "Empty date error!\n";
    char c;
    int length = 0;
    int i, j;

    // Show introduction text
    printf(intro);

    // Get names and birthdays
    while (count < MAXCOUNT) {
        printf(nameInputPrompt);
        length = 0;

        // Get a name
        while ((c = getchar()) != '\n' && c != '0') {
            if (length < MAXLENGTH - 1) {
                nameInput[length++] = c;
            }
        }

        if (c == '0') {
            if (count > 0) {
                break; // Display sorted data when '0' is entered after names
            } else {
                printf("No names entered. Exiting...\n");
                return 0;
            }
        } else if (length > 0) {
            nameInput[length] = '\0';
            people[count].name = malloc(strlen(nameInput));
            assert(people[count].name != NULL);
            strcpy(people[count].name, nameInput);

            // Get and validate birthday input
            while (1) {
                printf("  Birthday (YYYYMMDD): ");
                scanf("%8s", people[count].birthday);

                if (strlen(people[count].birthday) == 0) {
                    printf(errorEmptyDateMessage);
                    return 1; // Exit due to empty date
                } else if (is_valid_date(people[count].birthday)) {
                    break; // Date is valid, exit the loop
                } else {
                    printf(errorDateFormatMessage);
                }
                getchar(); // Consume the newline character left in the buffer
            }

            display_person(&people[count], count + 1); // Display the person's info
            count++;
        } else {
            printf(errorMessage);
        }
    }

    // Bubble sort the names
    for (i = 0; i < count - 1; i++) {
        for (j = i + 1; j < count; j++) {
            if (strcmp(people[i].name, people[j].name) > 0) {
                // Swap the structures
                struct Person temp = people[i];
                people[i] = people[j];
                people[j] = temp;
            }
        }
    }

    // Show sorted names and birthdays
    printf(sortedNamesTitle);
    for (i = 0; i < count; i++) {
        display_person(&people[i], i + 1);
        free(people[i].name);
    }

    return 0;
}

***

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>

#define MAXLENGTH 11
#define MAXCOUNT 5

typedef char* STRING;

struct Person {
    STRING name;
    char birthday[9]; // 8 characters for YYYYMMDD + null terminator
};

int is_valid_date(const char *date) {
    // Validate the date format
    if (strlen(date) != 8)
        return 0;
    
    // Extract year, month, and day
    int year, month, day;
    if (sscanf(date, "%4d%2d%2d", &year, &month, &day) != 3)
        return 0;

    // Validate year
    if (year < 0 || year > 9999)
        return 0;

    // Validate month
    if (month < 1 || month > 12)
        return 0;

    // Validate day based on the month
    if ((month == 2 && (day < 1 || day > 28)) ||
        ((month == 4 || month == 6 || month == 9 || month == 11) && (day < 1 || day > 30)) ||
        (day < 1 || day > 31))
        return 0;

    return 1; // Date is valid
}

void display_person(struct Person *person, int entry_count) {
    printf("%-20s  %-8s Entry count: %d free-address: %p -> %p\n",
           person->name, person->birthday, entry_count, person, person->name);
}

int main() {
    // Variables
    int count = 0;
    struct Person people[MAXCOUNT];
    char nameInput[MAXLENGTH];
    STRING intro = "Names Entry and Sorting Program\nPlease input name (max: 5 entries).\n";
    STRING nameInputPrompt = "  Name (First Last): ";
    STRING nameTemplate = "%-20s  %-8s Entry count: %2d free-address: %p -> %p\n";
    STRING sortedNamesTitle = "\nSorted Names:\n";
    STRING errorMessage = "Incorrect name format.\n";
    STRING errorDateFormatMessage = "Date format error! Please re-enter date (YYYYMMDD).\n";
    STRING errorEmptyDateMessage = "Empty date error!\n";
    char c;
    int length = 0;
    int i, j;

    // Show introduction text
    printf(intro);

    // Get names and birthdays
    while (count < MAXCOUNT) {
        printf(nameInputPrompt);
        length = 0;

        // Get a name
        while ((c = getchar()) != '\n' && c != '0') {
            if (length < MAXLENGTH - 1) {
                nameInput[length++] = c;
            }
        }

        if (c == '0') {
            if (count > 0) {
                break; // Display sorted data when '0' is entered after names
            } else {
                printf("No names entered. Exiting...\n");
                return 0;
            }
        } else if (length > 0) {
            nameInput[length] = '\0';
            people[count].name = malloc(strlen(nameInput) + 1); // +1 for null terminator
            assert(people[count].name != NULL);
            strcpy(people[count].name, nameInput);

            // Get and validate birthday input
            while (1) {
                printf("  Birthday (YYYYMMDD): ");
                scanf("%8s", people[count].birthday);

                if (strlen(people[count].birthday) == 0) {
                    printf(errorEmptyDateMessage);
                    return 1; // Exit due to empty date
                } else if (is_valid_date(people[count].birthday)) {
                    break; // Date is valid, exit the loop
                } else {
                    printf(errorDateFormatMessage);
                }
                getchar(); // Consume the newline character left in the buffer
            }

            display_person(&people[count], count + 1); // Display the person's info
            count++;
        } else {
            printf(errorMessage);
        }
    }

    // Bubble sort the names
    for (i = 0; i < count - 1; i++) {
        for (j = i + 1; j < count; j++) {
            if (strcmp(people[i].name, people[j].name) > 0) {
                // Swap the structures
                struct Person temp = people[i];
                people[i] = people[j];
                people[j] = temp;
            }
        }
    }

    // Show sorted names and birthdays
    printf(sortedNamesTitle);
    for (i = 0; i < count; i++) {
        display_person(&people[i], i + 1);
        free(people[i].name);
    }

    return 0;
}

***

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>

#define MAXLENGTH 11
#define MAXCOUNT 5

typedef char* STRING;

struct Person {
    STRING name;
    char birthday[9]; // 8 characters for YYYYMMDD + null terminator
};

int is_valid_date(const char *date) {
    // Validate the date format
    if (strlen(date) != 8)
        return 0;
    
    // Extract year, month, and day
    int year, month, day;
    if (sscanf(date, "%4d%2d%2d", &year, &month, &day) != 3)
        return 0;

    // Validate year
    if (year < 0 || year > 9999)
        return 0;

    // Validate month
    if (month < 1 || month > 12)
        return 0;

    // Validate day based on the month
    if ((month == 2 && (day < 1 || day > 28)) ||
        ((month == 4 || month == 6 || month == 9 || month == 11) && (day < 1 || day > 30)) ||
        (day < 1 || day > 31))
        return 0;

    return 1; // Date is valid
}

void display_person(struct Person *person, int entry_count) {
    printf("%-20s  %-8s Entry count: %d free-address: %p -> %p\n",
           person->name, person->birthday, entry_count, person, person->name);
}

int main() {
    // Variables
    int count = 0;
    struct Person people[MAXCOUNT];
    char nameInput[MAXLENGTH];
    STRING intro = "Names Entry and Sorting Program\nPlease input name (max: 5 entries).\n";
    STRING nameInputPrompt = "  Name (First Last): ";
    STRING nameTemplate = "%-20s  %-8s Entry count: %2d free-address: %p -> %p\n";
    STRING sortedNamesTitle = "\nSorted Names:\n";
    STRING errorMessage = "Incorrect name format.\n";
    STRING errorDateFormatMessage = "Date format error! Please re-enter date (YYYYMMDD).\n";
    STRING errorEmptyDateMessage = "Empty date error!\n";
    char c;
    int length = 0;
    int i, j;

    // Show introduction text
    printf(intro);

    // Get names and birthdays
    while (count < MAXCOUNT) {
        printf(nameInputPrompt);
        length = 0;

        // Get a name
        while ((c = getchar()) != '\n' && c != '0') {
            if (length < MAXLENGTH - 1) {
                nameInput[length++] = c;
            }
        }

        if (c == '0') {
            if (count > 0) {
                break; // Display sorted data when '0' is entered after names
            } else {
                printf("No names entered. Exiting...\n");
                return 0;
            }
        } else if (length > 0) {
            nameInput[length] = '\0';
            people[count].name = malloc(strlen(nameInput) + 1); // +1 for null terminator
            assert(people[count].name != NULL);
            strcpy(people[count].name, nameInput);

            // Get and validate birthday input
            while (1) {
                printf("  Birthday (YYYYMMDD): ");
                scanf("%8s", people[count].birthday);

                if (strlen(people[count].birthday) == 0) {
                    printf(errorEmptyDateMessage);
                    return 1; // Exit due to empty date
                } else if (is_valid_date(people[count].birthday)) {
                    break; // Date is valid, exit the loop
                } else {
                    printf(errorDateFormatMessage);
                }
                getchar(); // Consume the newline character left in the buffer
            }

            display_person(&people[count], count + 1); // Display the person's info
            count++;
        } else {
            printf(errorMessage);
        }
    }

    // Bubble sort the names
    for (i = 0; i < count - 1; i++) {
        for (j = i + 1; j < count; j++) {
            if (strcmp(people[i].name, people[j].name) > 0) {
                // Swap the structures
                struct Person temp = people[i];
                people[i] = people[j];
                people[j] = temp;
            }
        }
    }

    // Show sorted names and birthdays
    printf(sortedNamesTitle);
    for (i = 0; i < count; i++) {
        display_person(&people[i], i + 1);
        free(people[i].name);
    }

    return 0;
}
Editor is loading...