那就这样吧e 2022-11-28 09:48 采纳率: 98.1%
浏览 8
已结题

用结构体来做学生工作信息管理系统

img

img

img


这个是题目及其要求,要求使用结构体的知识来做,可以使用指针,最好加一点注释

  • 写回答

2条回答 默认 最新

  • 关注

    给你写过一个了啊,这个就是改成结构体数组就可以了

    #define _CRT_SECURE_NO_WARNINGS 1
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct student
    {
        int number;
        char* name;
        float computer;
        float math;
        float english;
    };
    
    
    
    void display(struct student stu[], int n)
    {
        int i;
        printf("%-10s %-10s %-10s %-10s %-10s\n", "No.", "Name", "Computer", "Math", "English");
        for (i = 0; i < n; i++)
        {
            printf("%-10d %-10s", stu[i].number,stu[i].name);
            printf(" %-10.1f %-10.1f %-10.1f\n", stu[i].computer,stu[i].math,stu[i].english);
        }
    }
    
    
    void query(struct student stu[], int n)
    {
        int i, j, flag = 0;
        int id;
        printf("input a student ID: ");
        scanf("%d", &id);
        for (i = 0; i < n; i++)
        {
            if (id == stu[i].number)
            {
                flag = 1; //表示找到,输出学生信息
                printf("%-10d %-10s", stu[i].number, stu[i].name);
                printf(" %-10.1f %-10.1f %-10.1f\n", stu[i].computer, stu[i].math, stu[i].english);
                break;
            }
        }
        if (flag == 0)
            printf("No such student\n"); //提示学生不存在
    }
    
    void update(struct student stu[], int n)
    {
        int i, flag = 0;
        int id;
        float sc;
        char kc[10] = { 0 };
        printf("input a student ID: ");
        scanf("%ld", &id);
        printf("input course name: ");
        scanf("%s", kc);
        for (i = 0; i < n; i++)
        {
            if (id == stu[i].number)
            {
                flag = 1; //表示找到,输出学生信息
                printf("please input a score: ");
                scanf("%f", &sc);
                if (strcmp(kc, "English") == 0)
                {
                    stu[i].english = sc;
                    flag = 2;
                }
                else if (strcmp(kc, "Computer") == 0)
                {
                    stu[i].computer = sc;
                    flag = 2;
                }
                else if (strcmp(kc, "Math") == 0)
                {
                    stu[i].math = sc;
                    flag = 2;
                }
                break;
            }
        }
        if (flag == 0)
            printf("No such student\n"); //提示学生不存在
        else if (flag == 1)
            printf("No such course\n"); //提示没有这门课程
        else
            display(stu, n);
    }
    
    void remove(struct student stu[], int* n)
    {
        int i, j, k, flag = 0;
        int id;
        printf("input a student ID: ");
        scanf("%d", &id);
        for (i = 0; i < *n; i++)
        {
            if (id == stu[i].number)
            {
                flag = 1; //表示找到,输出学生信息
                free(stu[i].name);
                stu[i].name = 0;
                //前移
                for (j = i; j < *n - 1; j++)
                {
                    stu[j] = stu[j + 1];
                }
                *n = (*n) - 1;
                break;
            }
        }
        if (flag == 0)
            printf("No such student\n"); //提示学生不存在
        else
            display(stu, *n); //显示删除后的信息
    }
    
    void add(struct student stu[], int* n)
    {
        int i, flag = 0;
        long id;
        float* tmp;
        int n2 = *n;
        while (1)
        {
            printf("input a student ID: ");
            scanf("%d", &id);
            for (i = 0; i < n2; i++)
            {
                if (id == stu[i].number)
                {
                    flag = 1; //表示找到,输出学生信息
                    break;
                }
            }
            if (flag == 0)
                break;
            else
                printf("the Id has exist, please ");
        }
        stu[*n].number = id;
        printf("input student name: ");
        stu[*n].name = (char*)malloc(10);
        scanf("%s", stu[*n].name);
        
        printf("input Computer score: ");
        scanf("%f", &stu[i].computer);
        printf("input Math score: ");
        scanf("%f", &stu[i].math);
        printf("input English score: ");
        scanf("%f", &stu[i].english);
        *n = (*n) + 1; //学生数量+1
        display(stu, *n);
    }
    
    int main()
    {
        char c;
        struct student stu[10] = {
            {20210101,NULL,60.5,97.0,90.5},
            {20210102,NULL,88.5,67.5,88.1},
            {20210103,NULL,72.3,75.0,92.0},
            {20210104,NULL,66.8,86.0,45.5}
        };
        int stuNmb = 4; //学生数量 
    
        stu[0].name = (char*)malloc(10);
        strcpy(stu[0].name, "Stu_01");
        stu[1].name = (char*)malloc(10);
        strcpy(stu[1].name, "Stu_02");
        stu[2].name = (char*)malloc(10);
        strcpy(stu[2].name, "Stu_03");
        stu[3].name = (char*)malloc(10);
        strcpy(stu[3].name, "Stu_04");
    
    
    
        printf("d. show all information\n");
        printf("q. search students' scores according to student ID numbers\n");
        printf("u. modify the students' scores according to student ID numbers and course name\n");
        printf("r. delete the students' information according to student ID numbers\n");
        printf("a. add a student's information\n");
        printf("e. exit\n");
    
        while ((c = getchar()) != 'e') //input e to end the program
        {
            if (c == 'd')
            {
                display(stu, stuNmb);
            }
            else if (c == 'q')
            {
                query(stu, stuNmb);
            }
            else if (c == 'u')
            {
                update(stu, stuNmb);
            }
            else if (c == 'r')
            {
                remove(stu, &stuNmb);
            }
            else if (c == 'a')
            {
                add(stu, &stuNmb);
            }
        }
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 12月7日
  • 已采纳回答 11月29日
  • 创建了问题 11月28日