啊啊啊编程难,难于上青天 2023-12-15 16:51 采纳率: 100%
浏览 11
已结题

c语言用函数实现成绩管理系统

img

img


c语言用函数实现成绩管理系统,求一个符合题目要求的完整的代码。

  • 写回答

15条回答 默认 最新

  • Leodong. 2023-12-15 17:19
    关注

    该回答通过自己思路及引用到GPTᴼᴾᴱᴺᴬᴵ搜索,得到内容具体如下:

    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct student {
        int id;
        char name[50];
        float chinese;
        float math;
        float english;
        float sum;
    } student;
    
    int ReadStuInfoFromFile(char *name, student **stu);
    int NoPass(student stu[], int n, student **noPassStudent, int *m);
    int Pass(student stu[], int n, student **PassStudent, int *m);
    int SortStudents(student stu[], int n);
    void PrintStudents(student stu[], int n);
    int SearchStudent(student stu[], int n, int id, int *rank, student *rstu);
    
    int main() {
        int n, rank, id, i, m;
        char name[] = "stuScores.txt";
        student *stu = NULL, *noPassStu = NULL, *passStu = NULL, rstu;
        n = ReadStuInfoFromFile(name, &stu);
        if (n == 0) {
            printf("error");
            return -1;
        }
        PrintStudents(stu, n);
        printf("\n no pass studnt-- \n---n: ");
        i = NoPass(stu, n, &noPassStu, &m);
        if (i == -1) {
            printf("no pass error");
        } else {
            PrintStudents(noPassStu, m);
        }
        printf("\n passed studnt--------\n");
        i = Pass(stu, n, &passStu, &m);
        if (i == -1) {
            printf("pass error");
        } else {
            PrintStudents(passStu, m);
        }
        printf("\n sort studnt--\n");
        i = SortStudents(stu, n);
        if (i == -1) {
            printf("sort error");
        } else {
            PrintStudents(stu, n);
        }
        printf("\n search id----\n");
        scanf("%d", &id);
        i = SearchStudent(stu, n, id, &rank, &rstu);
        if (i == -1) {
            printf("search error");
        } else {
            PrintStudents(&rstu, 1);
        }
        if (stu) {
            free(stu);
            stu = NULL;
        }
        if (noPassStu) {
            free(noPassStu);
            noPassStu = NULL;
        }
        if (passStu) {
            free(passStu);
            passStu = NULL;
        }
        system("pause");
        return 0;
    }
    
    int ReadStuInfoFromFile(char *name, student **stu) {
        FILE *fp = fopen(name, "rb");
        if (fp == NULL) {
            return 0;
        }
        fscanf(fp, "%d", &(*stu)->n);
        for (int i = 0; i < (*stu)->n; i++) {
            fscanf(fp, "%d %s %f %f %f", &(*stu)[i].id, (*stu)[i].name, &(*stu)[i].chinese, &(*stu)[i].math, &(*stu)[i].english);
            (*stu)[i].sum = (*stu)[i].chinese + (*stu)[i].math + (*stu)[i].english;
        }
        fclose(fp);
        return (*stu)->n;
    }
    
    int NoPass(student stu[], int n, student **noPassStudent, int *m) {
        *noPassStudent = (student *)malloc(sizeof(student) * n);
        if (*noPassStudent == NULL) {
            return -1;
        }
        *m = 0;
        for (int i = 0; i < n; i++) {
            if (stu[i].chinese < 60 || stu[i].math < 60 || stu[i].english < 60) {
                (*noPassStudent)[*m] = stu[i];
                (*m)++;
            }
        }
        return 0;
    }
    
    int Pass(student stu[], int n, student **PassStudent, int *m) {
        *PassStudent = (student *)malloc(sizeof(student) * n);
        if (*PassStudent == NULL) {
            return -1;
        }
        *m = 0;
        for (int i = 0; i < n; i++) {
            if (stu[i].chinese >= 60 && stu[i].math >= 60 && stu[i].english >= 60) {
                (*PassStudent)[*m] = stu[i];
                (*m)++;
            }
        }
        return 0;
    }
    
    int SortStudents(student stu[], int n) {
        qsort(stu, n, sizeof(student), (int (*)(const void *, const void *))compare);
        return 0;
    }
    
    int compare(const void *a, const void *b) {
        student *sa = (student *)a;
        student *sb = (student *)b;
        return (sb->sum - sa->sum);
    }
    
    void PrintStudents(student stu[], int n) {
        for (int i = 0; i < n; i++) {
            printf("ID: %d Name: %s Chinese: %f Math: %f English: %f Sum: %f\n", stu[i].id, stu[i].name, stu[i].chinese, stu[i].math, stu[i].english, stu[i].sum);
        }
    }
    
    int SearchStudent(student stu[], int n, int id, int *rank, student *rstu) {
        for (int i = 0; i < n; i++) {
            if (stu[i].id == id) {
                *rank = i + 1;
                *rstu = stu[i];
                return 0;
            }
        }
        return -1;
    }
    

    如果以上回答对您有所帮助,点击一下采纳该答案~谢谢

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(14条)

报告相同问题?

问题事件

  • 系统已结题 12月26日
  • 已采纳回答 12月18日
  • 创建了问题 12月15日