Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
6.1 kB
8
Indexable
Never
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

struct patient {
    int id;
    char patientName[50];
    char patientAddress[50];
    char disease[50];
    char date[12];
} p;

struct doctor {
    int id;
    char name[50];
    char address[50];
    char specialize[50];
    char date[12];
} d;

FILE* fp;

void admitPatient() {
    system("cls");

    char myDate[12];
    time_t t = time(NULL);
    struct tm tm = *localtime(&t);
    sprintf(myDate, "%02d/%02d/%d", tm.tm_mday, tm.tm_mon + 1, tm.tm_year + 1900);
    strcpy(p.date, myDate);

    fp = fopen("patient.txt", "ab");

    printf("Enter Patient id: ");
    scanf("%d", &p.id);

    printf("Enter Patient name: ");
    fflush(stdin);
    gets(p.patientName);

    printf("Enter Patient Address: ");
    fflush(stdin);
    gets(p.patientAddress);

    printf("Enter Patient Disease: ");
    fflush(stdin);
    gets(p.disease);

    printf("\nPatient Added Successfully\n");

    fwrite(&p, sizeof(p), 1, fp);
    fclose(fp);
}

void patientList() {
    system("cls");
    printf("<== Patient List ==>\n\n");
    printf("%-10s %-30s %-30s %-20s %s\n", "Id", "Patient Name", "Address", "Disease", "Date");
    printf("----------------------------------------------------------------------------------------------------------\n");

    fp = fopen("patient.txt", "rb");
    while (fread(&p, sizeof(p), 1, fp) == 1) {
        printf("%-10d %-30s %-30s %-20s %s\n", p.id, p.patientName, p.patientAddress, p.disease, p.date);
    }

    fclose(fp);

    printf("\n\nPress Any Key To Exit...");
    getchar();
}

void dischargePatient() {
    int id, f = 0;
    system("cls");
    printf("<== Discharge Patient ==>\n\n");
    printf("Enter Patient id to discharge: ");
    scanf("%d", &id);

    FILE* ft;

    fp = fopen("patient.txt", "rb");
    ft = fopen("temp.txt", "wb");

    while (fread(&p, sizeof(p), 1, fp) == 1) {
        if (id == p.id) {
            printf("%d\n", p.id);
            f = 1;
        } else {
            fwrite(&p, sizeof(p), 1, ft);
        }
    }

    if (f == 1) {
        printf("\n\nPatient Discharged Successfully.");
    } else {
        printf("\n\nRecord Not Found !");
    }

    fclose(fp);
    fclose(ft);

    remove("patient.txt");
    rename("temp.txt", "patient.txt");

    getchar();
}

void addDoctor() {
    system("cls");

    char myDate[12];
    time_t t = time(NULL);
    struct tm tm = *localtime(&t);
    sprintf(myDate, "%02d/%02d/%d", tm.tm_mday, tm.tm_mon + 1, tm.tm_year + 1900);
    strcpy(d.date, myDate);

    int f = 0;

    system("cls");
    printf("<== Add Doctor ==>\n\n");

    fp = fopen("doctor.txt", "ab");

    printf("Enter Doctor id: ");
    scanf("%d", &d.id);

    printf("Enter Doctor Name: ");
    fflush(stdin);
    gets(d.name);

    printf("Enter Doctor Address: ");
    fflush(stdin);
    gets(d.address);

    printf("Doctor Specialize in: ");
    fflush(stdin);
    gets(d.specialize);

    printf("Doctor Added Successfully\n\n");

    fwrite(&d, sizeof(d), 1, fp);
    fclose(fp);
}

void doctorList() {
    system("cls");
    printf("<== Doctor List ==>\n\n");

    printf("%-10s %-30s %-30s %-30s %s\n", "id", "Name", "Address", "Specialize", "Date");
    printf("-------------------------------------------------------------------------------------------------------------------\n");

    fp = fopen("doctor.txt", "rb");
    while (fread(&d, sizeof(d), 1, fp) == 1) {
        printf("%-10d %-30s %-30s %-30s %s\n", d.id, d.name, d.address, d.specialize, d.date);
    }

    fclose(fp);

    printf("\n\nPress Any Key To Exit...");
    getchar();
}

int isAdmin() {
    system("cls");

    char username[50], password[50];

    printf("username: ");
    fflush(stdin);
    gets(username);

    printf("password: ");
    fflush(stdin);
    gets(password);

    if (strcmp(username, "admin") == 0 && strcmp(password, "admin") == 0) {
        return 1;
    } else {
        return 0;
    }
}

int main() {
    int ch, is_admin = 0, program_started = 0;

    while (1) {
        system("cls");
        if (!is_admin && program_started) {
            printf("!!! Invalid username or password !!!\n\n");
        }

        program_started = 1;

        printf("<== Hospital Management System ==>\n");
        printf("1. Sign in as Admin\n");
        printf("2. Enter as Patient\n");
        printf("0. Exit\n");
        printf("\n\nEnter your choice: ");
        scanf("%d", &ch);

        if (ch == 1) {
            is_admin = isAdmin();

            if (is_admin) {
                while (1) {
                    system("cls");
                    printf("<== Hospital Management System (Admin) ==>\n");
                    printf("1. Discharge Patient\n");
                    printf("2. Add Doctor\n");
                    printf("3. Doctors' List\n");
                    printf("4. Patients' List\n");
                    printf("0. Exit\n");
                    printf("\n\nEnter your choice: ");
                    scanf("%d", &ch);

                    switch (ch) {
                        case 1:
                            dischargePatient();
                            break;
                        case 2:
                            addDoctor();
                            break;
                        case 3:
                            doctorList();
                            break;
                        case 4:
                            patientList();
                            break;
                        case 0:
                            exit(0);
                            break;
                        default:
                            printf("Invalid choice. Please try again.\n");
                            break;
                    }
                }
            } else {
                printf("Access denied. Please try again.\n");
            }
        } else if (ch == 2) {
            // Implement patient functionality if needed
        } else if (ch == 0) {
            exit(0);
        } else {
            printf("Invalid choice. Please try again.\n");
        }
    }

    return 0;
}