c++ 实战案例--学生管理系统

需求

1、学生信息的添加;
2、计算每个学生的总分;
3、输出所有学生的信息;
4、按照总分排序输出学生信息;
5、按照姓名查询学生情况;
6、按照学号删除某个学生,并输出删除后的所有学生信息;
7、将所有学生的信息存储在文本文件“student.txt”中。
8、从文件中读出信息。(打开文件)
9、退出程序。

源码

#include 
#include 

#include 
// 定义结构体表示学生信息
typedef struct student
{
    long studentID;
    char name[20];
    char sex[2];
    float chinese;   // 语文
    float maths;     // 数学
    float chemistry; // 化学
    float physics;   // 物理
    float English;   // 英语
    float sumScore;  // 总分
} STUDENT;
void addStudent(STUDENT students[], int* count);                         // 添加学生信息
void calculateSum(STUDENT students[], int count);                        // 计算每个学生的总分
void displayStudent(STUDENT students[], int count);                      // 显示学生信息
void writeStudent(STUDENT students[], int count);                        // 将学生信息写到student.txt中
void readStudent(STUDENT students[], int* count);                        // 从student.txt中读出学生信息
void sortStudentsByTotalScore(STUDENT students[], int count);            // 按总分排序学生信息
void findStudentByName(STUDENT students[], int count, const char* name); // 按姓名查找学生信息
void deleteStudentByID(STUDENT students[], int* count, long studentID);  // 按学号删除学生信息

int main()
{
    STUDENT students[100];
    int count = 0;
    while (1)
    {
        printf("\n\t\t\t\t请选择操作:\n");
        printf("\t\t\t\t1. 打开文件\n");
        printf("\t\t\t\t2. 添加学生信息\n");
        printf("\t\t\t\t3. 计算学生总分\n");
        printf("\t\t\t\t4. 显示学生信息\n");
        printf("\t\t\t\t5. 查找学生\n");
        printf("\t\t\t\t6. 删除学生\n");
        printf("\t\t\t\t7. 按总分降序输出\n");
        printf("\t\t\t\t8. 保存文件\n");
        printf("\t\t\t\t9. 退出\n");
        int choice;
        scanf("%d", &choice);
        switch (choice)
        {
        case 1:
            readStudent(students, &count);
            break;
        case 2:
            addStudent(students, &count);
            break;
        case 3:
            calculateSum(students, count);
            break;
        case 4:
            displayStudent(students, count);
            break;
        case 5:
        {
            char name[20];
            printf("请输入要查找的学生姓名:");
            scanf("%s", name);
            findStudentByName(students, count, name);
        }
        break;
        case 6:
        {
            long studentID;
            printf("请输入要删除的学生学号:");
            scanf("%ld", &studentID);
            deleteStudentByID(students, &count, studentID);
            displayStudent(students, count);
        }
        break;
        case 7:
            sortStudentsByTotalScore(students, count);
            displayStudent(students, count);
            break;
        case 8:
            writeStudent(students, count);
            break;
        case 9:
            exit(0);
        default:
            printf("无效的选择,请重新选择!\n");
            break;
        }
    }
    return 0;
}
void addStudent(STUDENT students[], int* count)
{
    STUDENT student;
    printf("请输入学生学号:");
    scanf("%ld", &student.studentID);
    printf("请输入学生姓名:");
    scanf("%s", student.name);
    printf("请输入学生性别:");
    scanf("%s", student.sex);
    printf("请输入学生语文成绩:");
    scanf("%f", &student.chinese);
    printf("请输入学生数学成绩:");
    scanf("%f", &student.maths);
    printf("请输入学生化学成绩:");
    scanf("%f", &student.chemistry);
    printf("请输入学生物理成绩:");
    scanf("%f", &student.physics);
    printf("请输入学生英语成绩:");
    scanf("%f", &student.English);
    students[*count] = student; // 将增加的学生信息添加到数组中
    (*count)++;                 // 学生数增加一个
}

// 显示学生信息
void displayStudent(STUDENT students[], int count)
{
    printf("\t\t学号\t姓名\t性别\t语文\t数学\t化学\t物理\t英语\t总分\n");
    for (int i = 0; i < count; i++)
    {
        printf("\t\t%ld\t%s\t%s\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t\n",
            students[i].studentID, students[i].name, students[i].sex,
            students[i].chinese, students[i].maths, students[i].chemistry,
            students[i].physics, students[i].English, students[i].sumScore);
    }
}

// 计算每个学生的总分
void calculateSum(STUDENT students[], int count)
{
    for (int i = 0; i < count; i++)
    {
        students[i].sumScore = students[i].chinese + students[i].maths + students[i].chemistry + students[i].physics + students[i].English;
    }
}

void writeStudent(STUDENT students[], int count) // 将学生信息写到student.txt中
{
    FILE* fp = fopen("student.txt", "w");
    int i;
    if (fp == NULL)
    {
        printf("打开文件失败!\n");
        return;
    }
    fwrite(students, sizeof(STUDENT), count, fp);
    fclose(fp); // 如果没有这个语句,保存不了
}

void readStudent(STUDENT students[], int* count)
{
    FILE* fp = fopen("student.txt", "r");
    STUDENT student;
    if (fp == NULL)
    {
        printf("打开文件失败!\n");
        // exit(0);
        return;
    }
    while (!feof(fp))
    {
        fread(&students[*count], sizeof(STUDENT), 1, fp); /* 按数据块读文件 */
        (*count)++;
    }
    (*count)--;
    // printf("个数为:%d\n",*count);
    fclose(fp);
}

// 按照总分排序输出学生信息
void sortStudentsByTotalScore(STUDENT students[], int count)
{
    for (int i = 0; i < count - 1; i++)
    {
        for (int j = 0; j < count - 1 - i; j++)
        {
            if (students[j].sumScore < students[j + 1].sumScore)
            {
                STUDENT temp = students[j];
                students[j] = students[j + 1];
                students[j + 1] = temp;
            }
        }
    }
}

void findStudentByName(STUDENT students[], int count, const char* name)
{
    int found = 0;
    printf("\t\t学号\t姓名\t性别\t语文\t数学\t化学\t物理\t英语\t总分\n");
    for (int i = 0; i < count; i++)
    {
        if (strcmp(students[i].name, name) == 0)
        {
            printf("\t\t%ld\t%s\t%s\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t\n",
                students[i].studentID, students[i].name, students[i].sex,
                students[i].chinese, students[i].maths, students[i].chemistry,
                students[i].physics, students[i].English, students[i].sumScore);
            found = 1;
        }
    }
    if (!found)
    {
        printf("未找到姓名为 %s 的学生。\n", name);
    }
}

// 按照学号删除某个学生,并输出删除后的所有学生信息
void deleteStudentByID(STUDENT students[], int* count, long studentID)
{
    int index = -1;
    for (int i = 0; i < *count; i++)
    {
        if (students[i].studentID == studentID)
        {
            index = i;
            break;
        }
    }

    if (index != -1)
    {
        for (int i = index; i < *count - 1; i++)
        {
            students[i] = students[i + 1];
        }
        (*count)--;
        printf("学号为 %ld 的学生已删除。\n", studentID);
    }
    else
    {
        printf("未找到学号为 %ld 的学生。\n", studentID);
    }
}



你可能感兴趣的:(从c入门到墓地,c++,算法)