Bootstrap

成绩系统测试

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

struct student {
    char id[15];
    char name[20];
    int english;
    int math;
    int c;
    int total;
    float average;
    int rank;
    struct student *next;
};

typedef struct student Student;

void showMenu();
void createList(Student **pHead);
void addStudent(Student **pHead);
void deleteStudent(Student **pHead);
void searchStudent(Student *pHead);
void modifyStudent(Student *pHead);
void sortStudent(Student *pHead);
void display(Student *pHead);
void readFromFile(Student **pHead);
void calculate(Student *pHead);

int main() {
    Student *head = NULL;
    int choice;
    do {
        showMenu();
        scanf("%d", &choice);
        switch (choice) {
            case 1:
                createList(&head);
                break;
            case 2:
                addStudent(&head);
                break;
            case 3:
                deleteStudent(&head);
                break;
            case 4:
                searchStudent(head);
                break;
            case 5:
                modifyStudent(head);
                break;
            case 6:
                sortStudent(head);
                break;
            case 7:
                display(head);
                break;
            case 8:
                readFromFile(&head);
                break;
            case 9:
                calculate(head);
                break;
            case 0:
                printf("正在退出程序...\n");
                break;
            default:
                printf("无效输入!请重新输入。\n");
                break;
        }
    } while (choice != 0);
    return 0;
}

void showMenu() {
    printf("\n");
    printf("-------------------\n");
    printf("学生成绩管理系统\n");
    printf("1. 创建列表\n");
    printf("2. 添加学生\n");
    printf("3. 删除学生\n");
    printf("4. 查询学生\n");
    printf("5. 修改学生\n");
    printf("6. 排序学生\n");
    printf("7. 显示学生\n");
    printf("8. 从文件读取\n");
    printf("9. 统计\n");
    printf("0. 退出\n");
    printf("-------------------\n");
    printf("请输入您的选择:");
}

void createList(Student **pHead) {
    printf("正在创建列表...\n");
    *pHead = NULL;
    printf("列表创建成功!\n");
}
void addStudent(Student **pHead) {
    Student *pNew = (Student *)malloc(sizeof(Student));
    printf("输入学生信息:\n");
    printf("学号:");
    scanf("%s", pNew->id);
    printf("姓名:");
    scanf("%s", pNew->name);
    printf("英语:");
    scanf("%d", &(pNew->english));
    printf("数学:");
    scanf("%d", &(pNew->math));
    printf("C语言:");
    scanf("%d", &(pNew->c));
    pNew->total = pNew->english + pNew->math + pNew->c;
    pNew->average = pNew->total / 3.0;
    pNew->rank = 0;
    pNew->next = NULL;
    if (*pHead == NULL) {
        *pHead = pNew;
    } else {
        Student *pTemp = *pHead;
        while (pTemp->next != NULL) {
            pTemp = pTemp->next;
        }
        pTemp->next = pNew;
    }
    printf("学生添加成功!\n");
}

void deleteStudent(Student **pHead) {
    printf("输入要删除的学生的学号:");
    char id[15];
    scanf("%s", id);
    Student *pCurrent = *pHead;
    Student *pPrev = NULL;
    while (pCurrent != NULL && strcmp(pCurrent->id, id) != 0) {
        pPrev = pCurrent;
        pCurrent = pCurrent->next;
    }
    if (pCurrent != NULL) {
        if (pPrev == NULL) {
            *pHead = (*pHead)->next;
        } else {
            pPrev->next = pCurrent->next;
        }
        free(pCurrent);
        printf("学生删除成功!\n");
    } else {
        printf("未找到学生!请重试。\n");
    }
}

void searchStudent(Student *pHead) {
    int choice;
    printf("按照什么查询:\n");
    printf("1. 学号\n");
    printf("2. 姓名\n");
    printf("请输入选择:");
    scanf("%d", &choice);
    printf("输入");
    if (choice == 1) {
        printf("要查询的学生的学号:");
        char id[15];
        scanf("%s", id);
        Student *pTemp = pHead;
        while (pTemp != NULL && strcmp(pTemp->id, id) != 0) {
            pTemp = pTemp->next;
        }
        if (pTemp != NULL) {
            printf("学号: %s\n", pTemp->id);
            printf("姓名: %s\n", pTemp->name);
            printf("英语: %d\n", pTemp->english);
            printf("数学: %d\n", pTemp->math);
            printf("C语言: %d\n", pTemp->c);
            printf("总分: %d\n", pTemp->total);
            printf("平均分: %.2f\n", pTemp->average);
            printf("排名: %d\n", pTemp->rank);
        } else {
            printf("未找到学生!请重试。\n");
        }
    } else if (choice == 2) {
        printf("要查询的学生的姓名:");
        char name[20];
        scanf("%s", name);
        int found = 0;
        Student *pTemp = pHead;
        while (pTemp != NULL) {
            if (strcmp(pTemp->name, name) == 0) {
                printf("学号: %s\n", pTemp->id);
                printf("姓名: %s\n", pTemp->name);
                printf("英语: %d\n", pTemp->english);
                printf("数学: %d\n", pTemp->math);
                printf("C语言: %d\n", pTemp->c);
                printf("总分: %d\n", pTemp->total);
                printf("平均分: %.2f\n", pTemp->average);
                printf("排名: %d\n", pTemp->rank);
                found = 1;
            }
            pTemp = pTemp->next;
        }
        if (!found) {
            printf("未找到学生!请重试。\n");
        }
    } else {
        printf("无效输入!请重新输入。\n");
    }
}
void modifyStudent(Student *pHead) {
    printf("输入要修改的学生的学号:");
    char id[15];
    scanf("%s", id);
    Student *pTemp = pHead;
    while (pTemp != NULL && strcmp(pTemp->id, id) != 0) {
        pTemp = pTemp->next;
    }
    if (pTemp != NULL) {
        printf("输入新信息:\n");
        printf("姓名:");
        scanf("%s", pTemp->name);
        printf("英语:");
        scanf("%d", &(pTemp->english));
        printf("数学:");
        scanf("%d", &(pTemp->math));
        printf("C语言:");
        scanf("%d", &(pTemp->c));
        pTemp->total = pTemp->english + pTemp->math + pTemp->c;
        pTemp->average = pTemp->total / 3.0;
        printf("学生修改成功!\n");
    } else {
        printf("未找到学生!请重试。\n");
    }
}

void sortStudent(Student *pHead) {
    int choice;
    printf("按照什么排序:\n");
    printf("1. 学号\n");
    printf("2. 姓名\n");
    printf("3. 总分\n");
    printf("请输入选择:");
    scanf("%d", &choice);
    if (choice == 1) {
        Student *pCurrent = pHead;
        while (pCurrent != NULL) {
            Student *pNext = pCurrent->next;
            while (pNext != NULL) {
                if (strcmp(pCurrent->id, pNext->id) > 0) {
                    Student temp = *pCurrent;
                    *pCurrent = *pNext;
                    *pNext = temp;
                }
                pNext = pNext->next;
            }
            pCurrent = pCurrent->next;
        }
        printf("学生列表按照学号排序成功!\n");
    } else if (choice == 2) {
        Student *pCurrent = pHead;
        while (pCurrent != NULL) {
            Student *pNext = pCurrent->next;
            while (pNext != NULL) {
                if (strcmp(pCurrent->name, pNext->name) > 0) {
                    Student temp = *pCurrent;
                    *pCurrent = *pNext;
                    *pNext = temp;
                }
                pNext = pNext->next;
            }
            pCurrent = pCurrent->next;
        }
        printf("学生列表按照姓名排序成功!\n");
    } else if (choice == 3) {
        Student *pCurrent = pHead;
        while (pCurrent != NULL) {
            Student *pNext = pCurrent->next;
            while (pNext != NULL) {
                if (pCurrent->total < pNext->total) {
                    Student temp = *pCurrent;
                    *pCurrent = *pNext;
                    *pNext = temp;
                }
                pNext = pNext->next;
            }
            pCurrent = pCurrent->next;
        }
        printf("学生列表按照总分排序成功!\n");
    } else {
        printf("无效输入!请重新输入。\n");
    }
}

void display(Student *pHead) {
    if (pHead == NULL) {
        printf("列表中没有学生!\n");
    } else {
        printf("学生列表:\n");
        printf("%-10s%-20s%-10s%-10s%-10s%-10s%-10s%-10s\n", "学号", "姓名", "英语", "数学", "C语言", "总分", "平均分", "排名");
        Student *pTemp = pHead;
        while (pTemp != NULL) {
            printf("%-10s%-20s%-10d%-10d%-10d%-10d%-10.2f%-10d\n", pTemp->id, pTemp->name, pTemp->english, pTemp->math, pTemp->c, pTemp->total, pTemp->average, pTemp->rank);
            pTemp = pTemp->next;
        }
    }
}
void readFromFile(Student **pHead) {
    printf("输入要读取的文件名:");
    char filename[50];
    scanf("%s", filename);
    FILE *fp = fopen(filename, "r");
    if (fp == NULL) {
        printf("无法打开文件 %s 进行读取。\n", filename);
    } else {
        createList(pHead);
        Student *pTemp = *pHead;
        char id[15], name[20];
        int english, math, c;
        while (fscanf(fp, "%s\t%s\t%d\t%d\t%d\n", id, name, &english, &math, &c) != EOF) {
            pTemp->next = (Student *)malloc(sizeof(Student));
            pTemp = pTemp->next;
            strcpy(pTemp->id, id);
            strcpy(pTemp->name, name);
            pTemp->english = english;
            pTemp->math = math;
            pTemp->c = c;
            pTemp->total = pTemp->english + pTemp->math + pTemp->c;
            pTemp->average = pTemp->total / 3.0;
            pTemp->next = NULL;
        }
        fclose(fp);
        printf("文件读取成功!\n");
    }
}

void calculate(Student *pHead) {
    int highestTotal = 0, lowestTotal = 300;
    float sumTotal = 0.0;
    int countFail = 0, countPass = 0;
    Student *pTemp = pHead;
    while (pTemp != NULL) {
        if (pTemp->total > highestTotal) {
            highestTotal = pTemp->total;
        }
        if (pTemp->total < lowestTotal) {
            lowestTotal = pTemp->total;
        }
        sumTotal += pTemp->total;
        if (pTemp->total < 60) {
            countFail++;
        } else {
            countPass++;
        }
        pTemp = pTemp->next;
    }
    float avgTotal = sumTotal / (countPass + countFail);
    int rank = 1;
    pTemp = pHead;
    while (pTemp != NULL) {
        int countBetter = 0;
        Student *pNext = pTemp->next;
        while (pNext != NULL) {
            if (pTemp->total < pNext->total) {
                countBetter++;
            }
            pNext = pNext->next;
        }
        pTemp->rank = rank + countBetter;
        rank++;
        pTemp = pTemp->next;
    }
    printf("最高分: %d\n", highestTotal);
    printf("最低分: %d\n", lowestTotal);
    printf("平均分: %.2f\n", avgTotal);
    printf("不及格人数: %d\n", countFail);
    printf("及格人数: %d\n", countPass);
    printf("学生排名计算成功!\n");
}

欢迎订阅专栏,数据结构实验,期末大作业,前端后端都有哦,想我发哪个方面的资源或文章可以私信我,免费的哦

;