Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
6.0 kB
14
Indexable
Never
a#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

int main()
{
    char IC_Number[14];
    char Patient_Name[100];
    char Patient_address[100];
    char Patient_phone_number[20];
    char Date_birth[7];

    printf("Enter Patient's IC Number: ");
    fgets(IC_Number, sizeof(IC_Number), stdin);

    printf("Enter Patient's Name: ");
    fgets(Patient_Name, sizeof(Patient_Name), stdin);

    printf("Enter Patient's Address: ");
    fgets(Patient_address, sizeof(Patient_address), stdin);

    printf("Enter Patient's Phone Number: ");
    fgets(Patient_phone_number, sizeof(Patient_phone_number), stdin);

    for (int i = 0; i < sizeof(Date_birth) - 1; i++)
    {
        Date_birth[i] = IC_Number[i];
    }
    Date_birth[6] = '\0';

    char day[3];   // Day component
    char month[3]; // Month component
    char year[3];  // Year component

    for (int i = 0; i < 2; i++)
    {
        day[i] = Date_birth[i + 4];
        month[i] = Date_birth[i + 2];
        year[i] = Date_birth[i];
    }
    day[2] = '\0';
    month[2] = '\0';
    year[2] = '\0';

    printf("\nRegistration Summary\n");
    printf("Name: %s", Patient_Name);
    printf("Address: %s", Patient_address);
    printf("Date of birth (dd/mm/yy) : %s/%s/%s\n", day, month, year);
    printf("Contact Number: %s", Patient_phone_number);
    printf("Malaysian IC Number: %s", IC_Number);

    // Calculate age
    time_t current_time = time(NULL);
    struct tm *local_time = localtime(&current_time);
    int current_year = local_time->tm_year + 1900;
    int current_month = local_time->tm_mon + 1;
    int current_day = local_time->tm_mday;

    int birth_year = atoi(year);
    int birth_month = atoi(month);
    int birth_day = atoi(day);

    // Determine the century based on the first two digits of the current year
    int century = current_year - (current_year % 100);

    if (birth_year >= 0 && birth_year <= (current_year % 100))
    {
        birth_year += century;
    }
    else
    {
        birth_year += century - 100;
    }

    int age_years = current_year - birth_year;
    int age_months = current_month - birth_month;
    int age_days = current_day - birth_day;

    if (age_days < 0)
    {
        age_months--;
        age_days += 30;
    }

    if (age_months < 0)
    {
        age_years--;
        age_months += 12;
    }

    printf("Age: %d years, %d months, %d days\n", age_years, age_months, age_days);

    // Determine state based on the 7th and 8th numbers of the IC number
    // Extract the 7th and 8th characters from the IC number
    char state_code_str[3];
    state_code_str[0] = IC_Number[6];
    state_code_str[1] = IC_Number[7];
    state_code_str[2] = '\0';

    int state_code = atoi(state_code_str);

    char state[50];
    switch (state_code)
    {
        case 1:
        case 21:
        case 22:
        case 23:
        case 24:
            strcpy(state, "Johor");
            break;
        case 2:
        case 25:
        case 26:
        case 27:
            strcpy(state, "Kedah");
            break;
        case 3:
        case 28:
        case 29:
            strcpy(state, "Kelantan");
            break;
        case 4:
        case 30:
            strcpy(state, "Malacca");
            break;
        case 5:
        case 31:
        case 59:
            strcpy(state, "Negeri Sembilan");
            break;
        case 6:
        case 32:
        case 33:
            strcpy(state, "Pahang");
            break;
        case 7:
        case 34:
        case 35:
            strcpy(state, "Penang");
            break;
        case 8:
        case 36:
        case 37:
        case 38:
        case 39:
            strcpy(state, "Perak");
            break;
        case 9:
        case 40:
            strcpy(state, "Perlis");
            break;
        case 10:
        case 41:
        case 42:
        case 43:
        case 44:
            strcpy(state, "Selangor");
            break;
        case 11:
        case 45:
        case 46:
            strcpy(state, "Terengganu");
            break;
        case 12:
        case 47:
        case 48:
        case 49:
            strcpy(state, "Sabah");
            break;
        case 13:
        case 50:
        case 51:
        case 52:
        case 53:
            strcpy(state, "Sarawak");
            break;
        case 14:
        case 54:
        case 55:
        case 56:
        case 57:
            strcpy(state, "Federal Territory of Kuala Lumpur");
            break;
        case 15:
        case 58:
            strcpy(state, "Federal Territory of Labuan");
            break;
        case 16:
            strcpy(state, "Federal Territory of Putrajaya");
            break;
        default:
            strcpy(state, "Unknown State");
            break;
    }

    printf("State of Birth: %s\n", state);

    char formatted_time[100];
    strftime(formatted_time, sizeof(formatted_time), "%Y-%m-%d %H:%M:%S", localtime(&current_time));

    FILE *file = fopen("registration_data.txt", "w");
    if (file == NULL) {
        printf("Failed to open the file.\n");
        return 1;
    }

    fprintf(file, "Registration Summary\n");
    fprintf(file, "Name: %s", Patient_Name);
    fprintf(file, "Address: %s", Patient_address);
    fprintf(file, "Date of birth: %s/%s/%s\n", day, month, year);
    fprintf(file, "Contact Number: %s", Patient_phone_number);
    fprintf(file, "Malaysian IC Number: %s", IC_Number);
    fprintf(file, "Age: %d years, %d months, %d days\n", age_years, age_months, age_days);
    fprintf(file, "State of Birth: %s\n", state);
    fprintf(file, "Registration Date and Time: %s\n", formatted_time);

    fclose(file);
    printf("Data saved to registration_data.txt.\n");

    return 0;
}
Leave a Comment