首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >记录场所消毒情况的程序

记录场所消毒情况的程序
EN

Code Review用户
提问于 2021-03-06 02:14:45
回答 2查看 98关注 0票数 2

我注意到我的代码中有很多重复,但我真的不知道如何解决复制,这就是为什么我决定向Stack溢出寻求建议和帮助。我还注意到,在更多的情况下,我已经使用了一个以上的循环,所以我想知道你们能不能检查一下下面的代码片段,并告诉我如何减少循环的使用量并删除重复。

我的代码有点长,所以我决定用将其放入联机编译器中。来让你们看清楚,我也很抱歉问了这个荒谬的问题,但是我真的很想知道如何减少我的程序中的行数和内存使用量,我在这方面做得很差。

下面的代码片段基本上是一个程序,它捕获有关清理场所的记录;数据将写入二进制文件中。

感染水平检查

该功能主要用于提示用户和收集用户的响应。它是根据他们的反应来跟踪他们的感染水平。我只允许用户回答是或否,这是一种验证方法,我相信。

表页眉/页脚

这只是为了打印表的页眉和页脚,这将在大多数功能中使用。我决定把它作为减少重复的一种功能。

消毒菜单

这基本上是这个程序的主菜单。

它允许用户导航到他或她希望使用的任何其他功能。

我使用嵌套的do…用于进行验证的while。我觉得这不是最好的方法,但我真的不知道如何改进它。

添加记录

主要用于添加新的卫生记录。它提示并询问用户一些事情,所有数据将被写入一个二进制文件调用Sanitization Record.dat。我对日期和时间做了一些验证,但我觉得有点太长了。我想知道是否可以减少验证的行数?

搜索记录

这只是一个菜单,用于根据他们的ID、公司名称等搜索记录。是的,我确实在这里使用了一个嵌套的while循环来进行验证,我觉得这肯定不是正确的方法。也许你们能给我提点建议?

搜索函数的其余部分

所有这些都是相同的,只是搜索方法是不同的。如果它是一个字符串,我将使用strcmp,否则我只会将它与正常的if条件进行比较。一旦在二进制文件中找到该记录,它将打印出该记录供用户查看。

修改函数

它允许用户修改除场地ID以外的二进制文件中的所有内容,它将提示并要求用户输入他希望修改的场地id,并将相关细节打印到该场所ID并要求确认。如果用户回答"Y",它将允许用户修改这些记录。一旦修改,所有记录都将被重写到二进制文件中。

删除函数

它允许用户删除二进制文件中的任何记录。它首先读取二进制文件并将所有数据捕获到数组结构中,然后要求用户输入删除记录的场所ID。如果找到一个记录,它将提示并询问用户是否要删除;如果用户回答"Y“,则通过数组移位方法删除该记录。然后,所有内容都将重写到二进制文件中。

代码语言:javascript
复制
#include 
#include 
#include 
#include 
#pragma warning (disable:4996)
#define MAX 20
typedef struct {
    char companyName[41], personInCharge[41], gender, contactNum[13];
    float temperature;
    int infectionLevel;
}Company;

typedef struct {
    int hours, minute;
}Time;

typedef struct {
    int day, month, year;
}Date;

struct SanitizeData {
    char venueID[10];
    Company Details;
    Time Start;
    Time End;
    Date sanitizeDate;
};

// Function Prototype Declaration
int infectionLevelCheck();
void tableHeader();
void tableFooter(int, int);
void sanitizeMenu();
void addRecord();
void searchRecord();
void searchVenueID();
void searchDate();
void searchCompany();
void searchName();
void searchContactNumber();
void searchGender();
void searchTemperature();
void modifyRecord();
void displayRecord();
void deleteRecord();

int infectionLevelCheck() {
    char userResponse;
    int infectionLevel;
    int invalidCounter;

    do {
        invalidCounter = 0;
        infectionLevel = 0;
        printf("\n\nRisk Of Infection Check: \n");
        printf("-----------------------\n");
        printf("Are you having the 2 or more of the following symptoms listed?\n");
        printf("- Fever\n");
        printf("- Sore Throat\n");
        printf("- Runny Nose\n");
        printf("- Diarrhea\n");
        printf("- Headache\n");
        printf("- Body Ache\n");
        printf("(Y = Yes, N = No): ");
        scanf("%c", &userResponse);
        rewind(stdin);

        if (toupper(userResponse) != 'Y' && toupper(userResponse) != 'N') {
            invalidCounter++;
        }
        else if (toupper(userResponse) == 'Y') {
            infectionLevel++;
        }

        printf("\n\nBesides the Symptoms listed above, are you having the following symptoms: \n");
        printf("- Cough\n");
        printf("- Difficulty breathing\n");
        printf("- Loss of smell\n");
        printf("- Loss of taste\n");
        printf("(Y = Yes, N = No): ");
        scanf("%c", &userResponse);
        rewind(stdin);

        if (toupper(userResponse) != 'Y' && toupper(userResponse) != 'N') {
            invalidCounter++;
        }
        else if (toupper(userResponse) == 'Y') {
            infectionLevel += 4;
        }

        printf("\n\nHave you travelled to any country outside Malaysia\nwithin the past 14 days?\n");
        printf("(Y = Yes, N = No): ");
        scanf("%c", &userResponse);
        rewind(stdin);

        if (toupper(userResponse) != 'Y' && toupper(userResponse) != 'N') {
            invalidCounter++;
        }

        else if (toupper(userResponse) == 'Y') {
            infectionLevel += 2;
        }

        printf("\n\nHave you had closed contact with anyone who \nconfirmed or suspected case of COVID 19 within the 14 days?");
        printf("(Y = Yes, N = No): ");
        scanf("%c", &userResponse);
        rewind(stdin);

        if (toupper(userResponse) != 'Y' && toupper(userResponse) != 'N') {
            invalidCounter++;
        }

        else if (toupper(userResponse) == 'Y') {
            infectionLevel += 4;
        }

        if (invalidCounter > 0) {
            printf("\n- Error Found: Invalid Response Entered - \n\n");
        }

    } while (invalidCounter != 0);
    return infectionLevel;
}

void tableHeader() {
    printf("------------------------------------------------------------------------------------------------------------------------\n");
    printf("| Venue |   Sanitization   |  Ending  |    Company    |    Person   | Gender |    Contact   | Temperature | Infection  |\n");
    printf("|  ID   |       Time       |   Time   |   In-Charge   |  In-Charge  |        |              |             |   Risk     |\n");
    printf("|-------|------------------|----------|---------------|-------------|--------|--------------|-------------|------------|\n");
}

void tableFooter(int i, int emptyRecord) {
    printf("------------------------------------------------------------------------------------------------------------------------\n");
    if (emptyRecord == MAX) {
        printf("No Such Record Found\n");
    }
    else {
        printf("%d Record Found\n", i);
    }
}

void addRecord() {
    struct SanitizeData newData;
    FILE* addPtr;
    char userResponse;
    int validate, invalidCounter, counter = 0, infectionlevel = 0;

    addPtr = fopen("Sanitization Record.dat", "ab");
    if (addPtr == NULL) {
        printf("Unable to open Sanitization Record.dat\n");
        exit(-1);
    }
    else {

        do {
            system("cls");
            do {
                invalidCounter = 0;
                printf("Add Sanitization Record\n");
                printf("-----------------------\n");
                printf("Enter Venue ID: ");
                scanf("%s", &newData.venueID);
                rewind(stdin);

                printf("Enter Sanitized Date in [DD/MM/YYYY Format Eg: 19/02/2021] : ");
                validate = scanf("%d/%d/%d", &newData.sanitizeDate.day, &newData.sanitizeDate.month, &newData.sanitizeDate.year);
                rewind(stdin);

                printf("Enter Sanitized Time in [HH:MM Format Eg: 12:51] - \n");
                printf("Starting: ");
                validate = scanf("%d:%d", &newData.Start.hours, &newData.Start.minute);
                rewind(stdin);

                printf("Ending: ");
                validate = scanf("%d:%d", &newData.End.hours, &newData.End.minute);
                rewind(stdin);

                printf("Enter Santizie Handling Company Details: \n");
                printf("Company Name: ");
                scanf("%[^\n]", &newData.Details.companyName);
                rewind(stdin);

                printf("Person In-Charge Name:  ");
                scanf("%[^\n]", &newData.Details.personInCharge);
                rewind(stdin);

                printf("Person In-Charge Gender: ");
                scanf("%c", &newData.Details.gender);
                rewind(stdin);

                printf("Person In-Charge Contact Numer: ");
                scanf("%s", &newData.Details.contactNum);
                rewind(stdin);

                printf("Person In-Charge Temperature: ");
                validate = scanf("%f", &newData.Details.temperature);
                rewind(stdin);

                newData.Details.infectionLevel = infectionLevelCheck();

                if (newData.Details.temperature > 37.8) newData.Details.infectionLevel++;


                if (validate == 0
                    || newData.sanitizeDate.day < 1 || newData.sanitizeDate.day > 31
                    || newData.sanitizeDate.month > 12 || newData.sanitizeDate.month < 1
                    || newData.Start.hours > 24 || newData.Start.hours < 1
                    || newData.Start.minute > 60 || newData.Start.minute < 1
                    || newData.End.hours > 24 || newData.End.hours < 1
                    || newData.End.minute > 60 || newData.End.minute < 1
                    || toupper(newData.Details.gender) != 'M' && toupper(newData.Details.gender) != 'F')
                {
                    if (newData.sanitizeDate.day < 1 || newData.sanitizeDate.day > 31 || newData.sanitizeDate.month > 12 || newData.sanitizeDate.month < 1) {
                        printf("- Error Found: Invalid Month or Days Entered. -\n");
                    }
                    if (newData.Start.hours > 24 || newData.Start.hours < 1 || newData.Start.minute > 60 || newData.Start.minute < 1) {
                        printf("- Error Found: Invalid Starting Hours or Minutes Entered. - \n");
                    }

                    if (newData.End.hours > 24 || newData.End.hours < 1 || newData.End.minute > 60 || newData.End.minute < 1) {
                        printf("- Error Found: Invalid Ending Hours or Minutes Entered. - \n");
                    }

                    if (toupper(newData.Details.gender) != 'M' || toupper(newData.Details.gender) != 'F') {
                        printf("- Error Found: Invalid Gender Entered. -\n");
                    }

                    if (validate == 0) {
                        printf("- Error Found: Kindly Enter Valid Input Only. - \n");
                    }

                    invalidCounter++;
                    system("pause");
                    system("cls");
                }
                else {
                    fwrite(&newData, sizeof(newData), 1, addPtr);
                    counter++;
                }
            } while (invalidCounter != 0);

            printf("Add Another Record? (N = No): ");
            scanf("%c", &userResponse);
            rewind(stdin);
        } while (toupper(userResponse) != 'N');
        printf("%d Record Added.....\n", counter);
        fclose(addPtr);
    }
}

void searchVenueID() {
    struct SanitizeData data[20];
    FILE* searchPtr;
    int i = 0, emptyRecord = 0;
    int counter = 0;
    char venueID[10];

    searchPtr = fopen("Sanitization Record.dat", "rb");
    if (searchPtr == NULL) {
        printf("Unable to open Santization Record.dat\n");
        exit(-1);
    }

    while (fread(&data[i], sizeof(data), 1, searchPtr) != 0) {
        i++;
    }

    printf("Enter Venue ID to Search: ");
    scanf("%s", &venueID);

    tableHeader();
    for (int i = 0; i < 20; i++) {
        if (strcmp(venueID, data[i].venueID) == 0) {
            counter++;
            printf("| %-5s | %02d-%02d-%4d %02d:%02d |   %02d:%02d  | %-13s | %-11s | ", data[i].venueID, data[i].sanitizeDate.day, data[i].sanitizeDate.month, data[i].sanitizeDate.year,
                data[i].Start.hours, data[i].Start.minute, data[i].End.hours, data[i].End.minute, data[i].Details.companyName,
                data[i].Details.personInCharge);

            switch (toupper(data[i].Details.gender)) {
            case 'F':
                printf("Female ");
                break;
            case 'M':
                printf(" Male  ");
                break;
            }

            printf("| %-12s |    %.2f    | ", data[i].Details.contactNum, data[i].Details.temperature);

            if (data[i].Details.infectionLevel <= 1) {
                printf(" Low Risk  |\n");
            }
            else if (data[i].Details.infectionLevel <= 3) {
                printf(" Mid Risk  |\n");
            }
            else if (data[i].Details.infectionLevel >= 4) {
                printf(" High Risk |\n");
            }
        }
        else {
            emptyRecord++;
        }
    }
    tableFooter(counter, emptyRecord);
    fclose(searchPtr);
}

void searchDate() {
    struct SanitizeData data[20];
    FILE* searchPtr;
    int i = 0, emptyRecord = 0;
    int counter = 0;
    int day, month, year;
    searchPtr = fopen("Sanitization Record.dat", "rb");
    if (searchPtr == NULL) {
        printf("Unable to open Santization Record.dat\n");
        exit(-1);
    }

    while (fread(&data[i], sizeof(data), 1, searchPtr) != 0) {
        i++;
    }

    printf("Enter Date to Search [Format: 19/02/2021] : ");
    scanf("%d/%d/%d", &day, &month, &year);

    tableHeader();
    for (i = 0; i < 20; i++) {
        if (day == data[i].sanitizeDate.day && month == data[i].sanitizeDate.month && year == data[i].sanitizeDate.year) {
            counter++;
            printf("| %-5s | %02d-%02d-%4d %02d:%02d |   %02d:%02d  | %-13s | %-11s | ", data[i].venueID, data[i].sanitizeDate.day, data[i].sanitizeDate.month, data[i].sanitizeDate.year,
                data[i].Start.hours, data[i].Start.minute, data[i].End.hours, data[i].End.minute, data[i].Details.companyName,
                data[i].Details.personInCharge);

            switch (toupper(data[i].Details.gender)) {
            case 'F':
                printf("Female ");
                break;
            case 'M':
                printf(" Male  ");
                break;
            }

            printf("| %-12s |    %.2f    | ", data[i].Details.contactNum, data[i].Details.temperature);

            if (data[i].Details.infectionLevel <= 1) {
                printf(" Low Risk  |\n");
            }
            else if (data[i].Details.infectionLevel <= 3) {
                printf(" Mid Risk  |\n");
            }
            else if (data[i].Details.infectionLevel >= 4) {
                printf(" High Risk |\n");
            }
        }
        else {
            emptyRecord++;
        }
    }
    tableFooter(counter, emptyRecord);
    fclose(searchPtr);
}

void searchCompany() {
    struct SanitizeData data[20];
    FILE* searchPtr;
    int i = 0, emptyRecord = 0;
    char companyName[41];
    int counter = 0;
    searchPtr = fopen("Sanitization Record.dat", "rb");
    if (searchPtr == NULL) {
        printf("Unable to open Santization Record.dat\n");
        exit(-1);
    }

    while (fread(&data[i], sizeof(data), 1, searchPtr) != 0) {
        i++;
    }

    printf("Enter Company Name to Search: ");
    scanf("%[^\n]", &companyName);

    tableHeader();
    for (i = 0; i < 20; i++) {
        if (strcmp(companyName, data[i].Details.companyName) == 0) {
            counter++;
            printf("| %-5s | %02d-%02d-%4d %02d:%02d |   %02d:%02d  | %-13s | %-11s | ", data[i].venueID, data[i].sanitizeDate.day, data[i].sanitizeDate.month, data[i].sanitizeDate.year,
                data[i].Start.hours, data[i].Start.minute, data[i].End.hours, data[i].End.minute, data[i].Details.companyName,
                data[i].Details.personInCharge);

            switch (toupper(data[i].Details.gender)) {
            case 'F':
                printf("Female ");
                break;
            case 'M':
                printf(" Male  ");
                break;
            }

            printf("| %-12s |    %.2f    | ", data[i].Details.contactNum, data[i].Details.temperature);

            if (data[i].Details.infectionLevel <= 1) {
                printf(" Low Risk  |\n");
            }
            else if (data[i].Details.infectionLevel <= 3) {
                printf(" Mid Risk  |\n");
            }
            else if (data[i].Details.infectionLevel >= 4) {
                printf(" High Risk |\n");
            }
        }
        else {
            emptyRecord++;
        }
    }
    tableFooter(counter, emptyRecord);
    fclose(searchPtr);
}

void searchName() {
    struct SanitizeData data[20];
    FILE* searchPtr;
    int i = 0, emptyRecord = 0;
    char personInCharge[41];
    int counter = 0;
    searchPtr = fopen("Sanitization Record.dat", "rb");
    if (searchPtr == NULL) {
        printf("Unable to open Santization Record.dat\n");
        exit(-1);
    }

    while (fread(&data[i], sizeof(data), 1, searchPtr) != 0) {
        i++;
    }

    printf("Enter Person In-Charge Name to Search: ");
    scanf("%[^\n]", &personInCharge);

    tableHeader();
    for (i = 0; i < 20; i++) {
        if (strcmp(personInCharge, data[i].Details.personInCharge) == 0) {
            counter++;
            printf("| %-5s | %02d-%02d-%4d %02d:%02d |   %02d:%02d  | %-13s | %-11s | ", data[i].venueID, data[i].sanitizeDate.day, data[i].sanitizeDate.month, data[i].sanitizeDate.year,
                data[i].Start.hours, data[i].Start.minute, data[i].End.hours, data[i].End.minute, data[i].Details.companyName,
                data[i].Details.personInCharge);

            switch (toupper(data[i].Details.gender)) {
            case 'F':
                printf("Female ");
                break;
            case 'M':
                printf(" Male  ");
                break;
            }

            printf("| %-12s |    %.2f    | ", data[i].Details.contactNum, data[i].Details.temperature);

            if (data[i].Details.infectionLevel <= 1) {
                printf(" Low Risk  |\n");
            }
            else if (data[i].Details.infectionLevel <= 3) {
                printf(" Mid Risk  |\n");
            }
            else if (data[i].Details.infectionLevel >= 4) {
                printf(" High Risk |\n");
            }
        }
        else {
            emptyRecord++;
        }
    }
    tableFooter(counter, emptyRecord);
    fclose(searchPtr);
}

void searchContactNumber() {
    struct SanitizeData data[20];
    FILE* searchPtr;
    int i = 0, emptyRecord = 0;
    char contactNum[13];
    int counter = 0;
    searchPtr = fopen("Sanitization Record.dat", "rb");
    if (searchPtr == NULL) {
        printf("Unable to open Santization Record.dat\n");
        exit(-1);
    }

    while (fread(&data[i], sizeof(data), 1, searchPtr) != 0) {
        i++;
    }

    printf("Enter Contact Number to Search [Format: 010-2012687]: ");
    scanf("%s", &contactNum);

    tableHeader();
    for (i = 0; i < 20; i++) {
        if (strcmp(contactNum, data[i].Details.contactNum) == 0) {
            counter++;
            printf("| %-5s | %02d-%02d-%4d %02d:%02d |   %02d:%02d  | %-13s | %-11s | ", data[i].venueID, data[i].sanitizeDate.day, data[i].sanitizeDate.month, data[i].sanitizeDate.year,
                data[i].Start.hours, data[i].Start.minute, data[i].End.hours, data[i].End.minute, data[i].Details.companyName,
                data[i].Details.personInCharge);

            switch (toupper(data[i].Details.gender)) {
            case 'F':
                printf("Female ");
                break;
            case 'M':
                printf(" Male  ");
                break;
            }

            printf("| %-12s |    %.2f    | ", data[i].Details.contactNum, data[i].Details.temperature);

            if (data[i].Details.infectionLevel <= 1) {
                printf(" Low Risk  |\n");
            }
            else if (data[i].Details.infectionLevel <= 3) {
                printf(" Mid Risk  |\n");
            }
            else if (data[i].Details.infectionLevel >= 4) {
                printf(" High Risk |\n");
            }
        }
        else {
            emptyRecord++;
        }
    }
    tableFooter(counter, emptyRecord);
    fclose(searchPtr);
}

void searchGender() {
    struct SanitizeData data[20];
    FILE* searchPtr;
    int i = 0, emptyRecord = 0;
    char gender;
    int counter = 0;
    searchPtr = fopen("Sanitization Record.dat", "rb");
    if (searchPtr == NULL) {
        printf("Unable to open Santization Record.dat\n");
        exit(-1);
    }

    while (fread(&data[i], sizeof(data), 1, searchPtr) != 0) {
        i++;
    }

    printf("Enter Person In-Charge Gender to Search [F = Female, M = Male]: ");
    scanf("%c", &gender);

    tableHeader();
    for (i = 0; i < 20; i++) {
        if (gender == data[i].Details.gender) {
            counter++;
            printf("| %-5s | %02d-%02d-%4d %02d:%02d |   %02d:%02d  | %-13s | %-11s | ", data[i].venueID, data[i].sanitizeDate.day, data[i].sanitizeDate.month, data[i].sanitizeDate.year,
                data[i].Start.hours, data[i].Start.minute, data[i].End.hours, data[i].End.minute, data[i].Details.companyName,
                data[i].Details.personInCharge);

            switch (toupper(data[i].Details.gender)) {
            case 'F':
                printf("Female ");
                break;
            case 'M':
                printf(" Male  ");
                break;
            }

            printf("| %-12s |    %.2f    | ", data[i].Details.contactNum, data[i].Details.temperature);

            if (data[i].Details.infectionLevel <= 1) {
                printf(" Low Risk  |\n");
            }
            else if (data[i].Details.infectionLevel <= 3) {
                printf(" Mid Risk  |\n");
            }
            else if (data[i].Details.infectionLevel >= 4) {
                printf(" High Risk |\n");
            }
        }
        else {
            emptyRecord++;
        }
    }
    tableFooter(counter, emptyRecord);
    fclose(searchPtr);
}

void searchTemperature() {
    struct SanitizeData data[20];
    FILE* searchPtr;
    int i = 0, emptyRecord = 0;
    float temperature;
    int counter = 0;
    searchPtr = fopen("Sanitization Record.dat", "rb");
    if (searchPtr == NULL) {
        printf("Unable to open Santization Record.dat\n");
        exit(-1);
    }

    while (fread(&data[i], sizeof(data), 1, searchPtr) != 0) {
        i++;
    }

    printf("Enter Temperature To Search [Eg: 36.6]: ");
    scanf("%f", &temperature);

    tableHeader();
    for (i = 0; i < 20; i++) {
        if (temperature == data[i].Details.temperature) {
            counter++;
            printf("| %-5s | %02d-%02d-%4d %02d:%02d |   %02d:%02d  | %-13s | %-11s | ", data[i].venueID, data[i].sanitizeDate.day, data[i].sanitizeDate.month, data[i].sanitizeDate.year,
                data[i].Start.hours, data[i].Start.minute, data[i].End.hours, data[i].End.minute, data[i].Details.companyName,
                data[i].Details.personInCharge);

            switch (toupper(data[i].Details.gender)) {
            case 'F':
                printf("Female ");
                break;
            case 'M':
                printf(" Male  ");
                break;
            }

            printf("| %-12s |    %.2f    | ", data[i].Details.contactNum, data[i].Details.temperature);

            if (data[i].Details.infectionLevel <= 1) {
                printf(" Low Risk  |\n");
            }
            else if (data[i].Details.infectionLevel <= 3) {
                printf(" Mid Risk  |\n");
            }
            else if (data[i].Details.infectionLevel >= 4) {
                printf(" High Risk |\n");
            }
        }
        else {
            emptyRecord++;
        }
    }
    tableFooter(counter, emptyRecord);
    fclose(searchPtr);
}

void searchRecord() {
    int selection, invalidCounter;
    char userResponse;
    do {
        system("cls");
        do {
            invalidCounter = 0;
            printf("Search Record Based On Following Criteria: \n");
            printf("1. Venue ID\n");
            printf("2. Sanitization Date\n");
            printf("3. Company Name\n");
            printf("4. Person In-Charge\n");
            printf("5. Contact Number\n");
            printf("6. Gender\n");
            printf("7. Temperature\n");
            printf("8. Back to Sanitize Menu\n");
            printf("Enter Your Choice > ");
            scanf("%d", &selection);
            if (selection < 1 || selection > 8) {
                invalidCounter++;
                printf("- Error Found: Enter 1 - 8 ONLY - \n");
                system("pause");
                system("cls");
            }
        } while (invalidCounter != 0);
        switch (selection) {
        case 1:
            searchVenueID();
            break;
        case 2:
            searchDate();
            break;
        case 3:
            searchCompany();
            break;
        case 4:
            searchName();
            break;
        case 5:
            searchContactNumber();
            break;
        case 6:
            searchGender();
            break;
        case 7:
            searchTemperature();
        case 8:
            sanitizeMenu();
            break;
        }
        printf("Anymore to Search? (N = No): ");
        scanf("%c", &userResponse);
        rewind(stdin);

    } while (toupper(userResponse) != 'N');
}

void modifyRecord() {
    struct SanitizeData data;
    struct SanitizeData modify[20];
    FILE* modifyPtr;
    int i = 0, counter = 0, validate, invalidCounter, emptyRecord = 0, modifyCounter = 0;
    char venueID[10], userResponse;
    modifyPtr = fopen("Sanitization Record.dat", "rb");
    if (modifyPtr == NULL) {
        printf("Unable to open Sanitization Record.dat\n");
        exit(-1);
    }

    while (fread(&data, sizeof(data), 1, modifyPtr) != 0) {
        modify[modifyCounter] = data;
        modifyCounter++;
    }

    modifyPtr = fopen("Sanitization Record.dat", "wb");
    if (modifyPtr == NULL) {
        printf("Unable to open Sanitization Record.dat\n");
        exit(-1);
    }
    do {
        system("cls");
        printf("Modify Sanitize Record\n");
        printf("----------------------\n\n");
        printf("Enter Venue ID To Modify: ");
        scanf("%s", &venueID);
        rewind(stdin);
        for (i = 0; i < modifyCounter; i++) {
            if (strcmp(venueID, modify[i].venueID) == 0) {

                printf("Venue ID                       : %s\n", modify[i].venueID);
                printf("Sanitization Date              : %02d-%02d-%4d\n", modify[i].sanitizeDate.day, modify[i].sanitizeDate.month, modify[i].sanitizeDate.year);
                printf("Starting Time                  : %02d:%02d\n", modify[i].Start.hours, modify[i].Start.minute);
                printf("Ending Time                    : %02d:%02d\n", modify[i].End.hours, modify[i].End.minute);
                printf("Sanitized Company Name         : %s\n", modify[i].Details.companyName);
                printf("Person In-Charge  Name         : %s\n", modify[i].Details.personInCharge);
                printf("Person In-Charge Gender        : %c\n", modify[i].Details.gender);
                printf("Person In-Charge Contact Number: %s\n", modify[i].Details.contactNum);
                printf("Person In-Charge Temperature   : %.2f\n\n", modify[i].Details.temperature);
                printf("Sure to Modify? (Y = Yes): ");
                scanf("%c", &userResponse);
                if (toupper(userResponse) == 'Y') {
                    counter++;
                    do {
                        invalidCounter = 0;

                        printf("Enter Sanitized Date in [DD/MM/YYYY Format Eg: 19/02/2021] : ");
                        validate = scanf("%d/%d/%d", &modify[i].sanitizeDate.day, &modify[i].sanitizeDate.month, &modify[i].sanitizeDate.year);
                        rewind(stdin);

                        printf("Enter Sanitized Time in [HH:MM Format Eg: 12:51] - \n");
                        printf("Starting: ");
                        validate = scanf("%d:%d", &modify[i].Start.hours, &modify[i].Start.minute);
                        rewind(stdin);

                        printf("Ending: ");
                        validate = scanf("%d:%d", &modify[i].End.hours, &modify[i].End.minute);
                        rewind(stdin);

                        printf("Enter Santizie Handling Company Details: \n");
                        printf("Company Name: ");
                        scanf("%[^\n]", &modify[i].Details.companyName);
                        rewind(stdin);

                        printf("Person In-Charge Name:  ");
                        scanf("%[^\n]", &modify[i].Details.personInCharge);
                        rewind(stdin);

                        printf("Person In-Charge Gender: ");
                        scanf("%c", &modify[i].Details.gender);
                        rewind(stdin);

                        printf("Person In-Charge Contact Numer: ");
                        scanf("%s", &modify[i].Details.contactNum);
                        rewind(stdin);

                        printf("Person In-Charge Temperature: ");
                        validate = scanf("%f", &modify[i].Details.temperature);
                        rewind(stdin);

                        modify[i].Details.infectionLevel = infectionLevelCheck();

                        if (modify[i].Details.temperature > 37.8) modify[i].Details.infectionLevel++;


                        if (validate == 0
                            || modify[i].sanitizeDate.day < 1 || modify[i].sanitizeDate.day > 31
                            || modify[i].sanitizeDate.month > 12 || modify[i].sanitizeDate.month < 1
                            || modify[i].Start.hours > 24 || modify[i].Start.hours < 1
                            || modify[i].Start.minute > 60 || modify[i].Start.minute < 1
                            || modify[i].End.hours > 24 || modify[i].End.hours < 1
                            || modify[i].End.minute > 60 || modify[i].End.minute < 1
                            || toupper(modify[i].Details.gender) != 'M' && toupper(modify[i].Details.gender) != 'F')
                        {
                            if (modify[i].sanitizeDate.day < 1 || modify[i].sanitizeDate.day > 31 || modify[i].sanitizeDate.month > 12 || modify[i].sanitizeDate.month < 1) {
                                printf("- Error Found: Invalid Month or Days Entered. -\n");
                            }
                            if (modify[i].Start.hours > 24 || modify[i].Start.hours < 1 || modify[i].Start.minute > 60 || modify[i].Start.minute < 1) {
                                printf("- Error Found: Invalid Starting Hours or Minutes Entered. - \n");
                            }

                            if (modify[i].End.hours > 24 || modify[i].End.hours < 1 || modify[i].End.minute > 60 || modify[i].End.minute < 1) {
                                printf("- Error Found: Invalid Ending Hours or Minutes Entered. - \n");
                            }

                            if (toupper(modify[i].Details.gender) != 'M' || toupper(modify[i].Details.gender) != 'F') {
                                printf("- Error Found: Invalid Gender Entered. -\n");
                            }

                            if (validate == 0) {
                                printf("- Error Found: Kindly Enter Valid Input Only. - \n");
                            }

                            invalidCounter++;
                            system("pause");
                            system("cls");
                        }

                    } while (invalidCounter != 0);

                }
                else {
                    break;
                }
                fwrite(&modify[i], sizeof(data), 1, modifyPtr);
            }
            else {
                emptyRecord++;
            }
        }
        if (emptyRecord == 20) {
            printf("No Record Found\n");
        }
        printf("Continue Modify Record? (N = No): ");
        scanf("%c", &userResponse);
    } while (userResponse != 'N');
    printf("%d Record Modified\n", counter);
    fclose(modifyPtr);
}

void displayRecord() {
    struct SanitizeData data;
    FILE* displayPtr;
    int i = 0;
    displayPtr = fopen("Sanitization Record.dat", "rb");

    if (displayPtr == NULL) {
        printf("Unable to open Sanitization Record.dat\n");
        exit(-1);
    }

    system("cls");

    printf("Display All Sanitization Record\n");
    printf("-------------------------------\n");
    tableHeader();
    while (fread(&data, sizeof(data), 1, displayPtr) != 0) {
        printf("| %-5s | %02d-%02d-%4d %02d:%02d |   %02d:%02d  | %-13s | %-11s | ", data.venueID, data.sanitizeDate.day, data.sanitizeDate.month, data.sanitizeDate.year,
            data.Start.hours, data.Start.minute, data.End.hours, data.End.minute, data.Details.companyName,
            data.Details.personInCharge);

        switch (toupper(data.Details.gender)) {
        case 'F':
            printf("Female ");
            break;
        case 'M':
            printf(" Male  ");
            break;
        }
        printf("| %-12s |    %.2f    | ", data.Details.contactNum, data.Details.temperature);
        if (data.Details.infectionLevel <= 1) {
            printf(" Low Risk  |\n");
        }
        else if (data.Details.infectionLevel <= 3) {
            printf(" Mid Risk  |\n");
        }
        else if (data.Details.infectionLevel >= 4) {
            printf(" High Risk |\n");
        }
        i++;
    }
    printf("------------------------------------------------------------------------------------------------------------------------\n");
    printf("%d Record Found\n", i);
    fclose(displayPtr);
}

void deleteRecord() {
    struct SanitizeData data;
    struct SanitizeData delData[20];
    FILE* deletePtr;

    int i = 0, delCount = 0, position, emptyRecord = 0;
    char userResponse;
    char venueID[10];

    deletePtr = fopen("Sanitization Record.dat", "rb");
    if (deletePtr == NULL) {
        printf("Unable to open Sanitization.dat\n");
        exit(-1);
    }
    else {
        while (fread(&data, sizeof(data), 1, deletePtr) != 0) {
            delData[i] = data;
            i++;
            delCount++;
        }
    }
    fclose(deletePtr);

    deletePtr = fopen("Sanitization Record.dat", "wb");
    if(deletePtr == NULL) {
        printf("Unable to open Sanitization.dat\n");
        exit(-1);
    }
    else {

        do {
            printf("Delete Record Based On: ");
            printf("1. Venue ID\n");
            printf("Enter Venue ID: ");
            scanf("%s", venueID);
            rewind(stdin);

            for (i = 0; i < delCount; i++) {
                if (strcmp(venueID, delData[i].venueID) == 0) {
                    printf("Venue ID                       : %s\n", delData[i].venueID);
                    printf("Sanitization Date              : %02d-%02d-%4d\n", delData[i].sanitizeDate.day, delData[i].sanitizeDate.month, delData[i].sanitizeDate.year);
                    printf("Starting Time                  : %02d:%02d\n", delData[i].Start.hours, delData[i].Start.minute);
                    printf("Ending Time                    : %02d:%02d\n", delData[i].End.hours, delData[i].End.minute);
                    printf("Sanitized Company Name         : %s\n", delData[i].Details.companyName);
                    printf("Person In-Charge  Name         : %s\n", delData[i].Details.personInCharge);
                    printf("Person In-Charge Gender        : %c\n", delData[i].Details.gender);
                    printf("Person In-Charge Contact Number: %s\n", delData[i].Details.contactNum);
                    printf("Person In-Charge Temperature   : %.2f\n\n", delData[i].Details.temperature);
                    position = i;
                    printf("Confirm to Delete? (Y = Yes): ");
                    scanf("%c", &userResponse);
                    rewind(stdin);
                    if (toupper(userResponse) == 'Y') {
                        for (i = position; i < delCount; i++) {
                            delData[i] = delData[i + 1];
                        }
                    }
                }
                else {
                    emptyRecord++;
                }
            }

            if (emptyRecord == 20) {
                printf("-Error Found: No Record - \n");
            }

            printf("Anymore to Delete? (Y = Yes): ");
            scanf("%c", &userResponse);
            rewind(stdin);

            if (toupper(userResponse) == 'N') {
                for (int i = 0; i < delCount--; i++) {
                    fwrite(&delData[i], sizeof(delData[i]), 1, deletePtr);
                }
            }

        } while (toupper(userResponse) != 'N');
    }
    fclose(deletePtr);
}

void sanitizeMenu() {
    int validate, invalidCounter, selection;
    char userResponse;
    do {
        system("cls");
        do {
            invalidCounter = 0;
            printf("     Sanitization Record Module\n");
            printf("-------------------------------------\n");
            printf("            MENU\n");
            printf("[1] - Add Sanitization Record\n");
            printf("[2] - Search Sanitization Record\n");
            printf("[3] - Modify Sanitization Record\n");
            printf("[4] - Display Sanitization Record\n");
            printf("[5] - Delete Sanitization Record\n");
            printf("[6] - Record \n");
            printf("[7] - Quit This Menu\n");
            printf("[8] - Exit The Program\n\n");
            printf("Enter Your Selection: ");
            validate = scanf("%d", &selection);
            rewind(stdin);
            if (selection < 1 || selection > 8 || validate == 0) {
                invalidCounter++;
                printf("- Error Found: Please Enter 1 - 7 Only -\n");
                system("pause");
                system("cls");
            }

        } while (invalidCounter != 0);

        switch (selection) {
        case 1:
            addRecord();
            break;
        case 2:
            searchRecord();
            break;
        case 3:
            modifyRecord();
            break;
        case 4:
            displayRecord();
            break;
        case 5:
            deleteRecord();
            break;
        case 6:
            menu();
            break;
        case 7:
            footer();
            exit(0);
        }

        printf("Back to Sanitize Menu? (N = No): ");
        rewind(stdin);
        scanf("%c", &userResponse);
        rewind(stdin);
    } while (toupper(userResponse) != 'N');
}
EN

回答 2

Code Review用户

发布于 2021-03-06 21:10:25

一般观测

函数名是描述性的,这是很好的。这些函数本身过于复杂(做得太多),其中许多可以分解成执行特定任务的较小的函数。(请参阅下面的错误检查)

代码范围内没有定义两个函数,而是由代码menu()footer()调用。这两者都在sanitizeMenu()中被调用。这可能会使问题偏离主题,因为Missing Code Review Context rule也会使代码很难进行良好的检查。

不清楚为什么代码中包含以下预处理器指令,因为它似乎不影响gcc编译器的警告消息:

代码语言:javascript
复制
#pragma warning (disable:4996)

如果要在Windows上使用Visual编译此代码,请注意Visual不一定遵循C标准,因此代码可能无法移植到Linux系统或其他平台。

还不清楚为什么SanitizeData结构没有D8,而CompanyTimeDate确实有typedef,这似乎不一致。在操作符和符号或类型名称之间留出空格,例如,在}之后应为所有类型设置一个空格。

不要在rewind()上使用stdin函数,没有办法重置到stdin的开头,因为它是一个特殊的文件指针,不能打开或关闭。建议使用fseek()而不是rewind()返回文件中的第一个记录。函数fseek()返回成功或失败,而rewind()函数不返回。fseek()函数也不会在stdin上工作。

干码或减少码重复

在这段代码中有几种减少代码重复的方法,最好的方法之一是编写一些实现打开和关闭文件的实用函数,这就是应用单一责任原则。(干意味着不要重复自己)

单一责任原则指出:

每个模块、类或函数都应该对软件提供的功能的单个部分负责,而该责任应该完全由该模块、类或函数封装。

具有特定于执行某一特定任务的每种数据结构类型的功能,例如读取或写入该数据结构。例如,对于结构Company

  • getCompanyName()
  • getPersonInCharge()
  • getGender()
  • getContactNumber()

对于结构SanitizeData

  • getVenueID()
  • getCompanyDetails()
  • getDate()

另一种可能提高程序性能的方法是只打开文件一次,将所有这些数据读入链接的SanitizeData结构列表,关闭数据文件,对数据进行所有必要的编辑,然后将更新的数据写入文件。除其他外,这可能允许对数据进行排序。这将提高性能,因为向文件打开、关闭、读取和写入数据是进行系统调用的非常昂贵的操作。这还将消除在每个函数中打开和关闭文件的必要性。

Recommendations

幻数

在整个代码中都有魔法数字,最好是为它们创建符号常量,以使代码更加可读性和易于维护。这些数字可以在许多地方使用,只要编辑一行就可以更改它们,从而使维护更容易。

代码中的数值常量有时被称为幻数,因为它们没有明显的意义。在堆栈过流上对此进行了讨论。

有一个符号常数(最大值)是好的,但至少应该有5个符号常数来提高可维护性。一个明显缺失的符号常量是VENUEID_SIZE,因为数字10用于多个位置(SanitizeData结构和函数searchVenueID() )。符号常量MAX只使用一次,但许多for循环似乎都可以使用它。

错误检查

对于系统提供的函数(如scanf() ),明显缺乏错误检查。函数scanf()返回一个整数值,该值指示读取的字符数。

infectionLevelCheck()中的这段代码可以变成一个函数,它将迫使用户输入正确的响应。

代码语言:javascript
复制
        printf("\n\nRisk Of Infection Check: \n");
        printf("-----------------------\n");
        printf("Are you having the 2 or more of the following symptoms listed?\n");
        printf("- Fever\n");
        printf("- Sore Throat\n");
        printf("- Runny Nose\n");
        printf("- Diarrhea\n");
        printf("- Headache\n");
        printf("- Body Ache\n");
        printf("(Y = Yes, N = No): ");
        scanf("%c", &userResponse);
        rewind(stdin);

        if (toupper(userResponse) != 'Y' && toupper(userResponse) != 'N') {
            invalidCounter++;
        }
        else if (toupper(userResponse) == 'Y') {
            infectionLevel++;
        }

在前面的代码中有一个循环,直到响应是YN时才返回。这样做的好处之一是变量invalidCounter在函数中可能不是必需的。

同一函数中的这段代码也可以移动到一个只返回响应的较小的函数:

代码语言:javascript
复制
        printf("\n\nBesides the Symptoms listed above, are you having the following symptoms: \n");
        printf("- Cough\n");
        printf("- Difficulty breathing\n");
        printf("- Loss of smell\n");
        printf("- Loss of taste\n");
        printf("(Y = Yes, N = No): ");
        scanf("%c", &userResponse);
        rewind(stdin);

        if (toupper(userResponse) != 'Y' && toupper(userResponse) != 'N') {
            invalidCounter++;
        }
        else if (toupper(userResponse) == 'Y') {
            infectionLevel += 4;
        }

在变量声明为

时初始化它们

infectionLevelCheck()中的代码包含以下变量声明:

代码语言:javascript
复制
    char userResponse;
    int infectionLevel;
    int invalidCounter;

这允许在不初始化的情况下使用变量,这会导致未定义的行为(Bug)。总是在声明变量时初始化它们,C编程语言在声明变量时不会为它们分配默认值。

票数 4
EN

Code Review用户

发布于 2021-03-06 22:43:08

票数 3
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/256788

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档