结构体学生成绩统计函数

编写一个课程成绩管理程序。要求如下:

学生数据包括学号、姓名、课程成绩、

课程成绩 = 平时成绩 * 40% + 卷面成绩 * 60%

  1. 编写input函数,输入n个学生的信息及各个成绩分量。

  2. 编写calculate1函数,统计和计算卷面成绩的最高分、最低分、平均分、及格率。

  3. 编写calculate2函数,计算所有学生的总成绩。

  4. 编写output1函数,输出卷面成绩的最高分、最低分、平均分、及格率。

  5. 编写output2函数,按总成绩从高到底输出所有学生的学号、姓名、总成绩,成绩相同的按学号顺序。

  6. 编写main函数,依次调用并测试上述各个函数。

要求

1)用typedef定义结构体类型。

2)根据用户输入的n的值,调用malloc动态内存分配。

3)不用全局变量,用&引用类型作参数,传递各个计算结果。

代码和运行图如下,记得采纳哦!

img

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

#define MAX_LEN 20

typedef struct {
    char id[MAX_LEN];       // 学号
    char name[MAX_LEN];     // 姓名
    float daily_score;      // 平时成绩
    float exam_score;       // 卷面成绩
    float total_score;      // 总成绩
} student;

void input(int n, student **students);
void calculate1(int n, student *students, float *max_exam, float *min_exam, float *avg_exam, float *pass_rate);
void calculate2(int n, student *students);
void output1(float max_exam, float min_exam, float avg_exam, float pass_rate);
void output2(int n, student *students);

int main() {
    int n;
    printf("请输入学生人数:");
    scanf("%d", &n);

    student *students = (student*)malloc(n * sizeof(student));
    input(n, &students);
    float max_exam, min_exam, avg_exam, pass_rate;
    calculate1(n, students, &max_exam, &min_exam, &avg_exam, &pass_rate);
    output1(max_exam, min_exam, avg_exam, pass_rate);
    calculate2(n, students);
    output2(n, students);

    free(students);
    return 0;
}

void input(int n, student **students) {
    printf("请依次输入每位学生的学号、姓名、平时成绩和卷面成绩:\n");
    for (int i = 0; i < n; i++) {
        scanf("%s %s %f %f", (*students)[i].id, (*students)[i].name, &(*students)[i].daily_score, &(*students)[i].exam_score);
    }
}

void calculate1(int n, student *students, float *max_exam, float *min_exam, float *avg_exam, float *pass_rate) {
    float total_exam = 0;
    *max_exam = students[0].exam_score;
    *min_exam = students[0].exam_score;
    int num_pass = 0;
    for (int i = 0; i < n; i++) {
        total_exam += students[i].exam_score;
        if (students[i].exam_score > *max_exam) {
            *max_exam = students[i].exam_score;
        }
        if (students[i].exam_score < *min_exam) {
            *min_exam = students[i].exam_score;
        }
        if (students[i].exam_score >= 60) {
            num_pass++;
        }
    }
    *avg_exam = total_exam / n;
    *pass_rate = (float)num_pass / n * 100;
}

void calculate2(int n, student *students) {
    for (int i = 0; i < n; i++) {
        students[i].total_score = students[i].daily_score * 0.4 + students[i].exam_score * 0.6;
    }
}

void output1(float max_exam, float min_exam, float avg_exam, float pass_rate) {
    printf("卷面成绩的最高分为:%.2lf\n", max_exam);
    printf("卷面成绩的最低分为:%.2lf\n", min_exam);
    printf("卷面成绩的平均分为:%.2lf\n", avg_exam);
    printf("卷面成绩的及格率为:%.2lf%%\n", pass_rate);
    
}

void output2(int n, student *students) {
// 按总成绩从高到底排序
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - 1 - i; j++) {
if (students[j].total_score < students[j + 1].total_score) {
student tmp = students[j];
students[j] = students[j + 1];
students[j + 1] = tmp;
} else if (students[j].total_score == students[j + 1].total_score && strcmp(students[j].id, students[j + 1].id) > 0) {
student tmp = students[j];
students[j] = students[j + 1];
students[j + 1] = tmp;
}
}
}

printf("按总成绩从高到低输出所有学生的信息:\n");
printf("%-10s %-10s %-10s\n", "学号", "姓名", "总成绩");
for (int i = 0; i < n; i++) {
    printf("%-10s %-10s %-10g\n", students[i].id, students[i].name, students[i].total_score);
}
}

1-6 只能一个个写,不能整合一个写


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
typedef struct {
    int stu_id;
    char stu_name[20];
    float score_class;
    float score_test;
} student;
void input(student *s, int n);
void calculate1(student *s, int n, float *max, float *min, float *average, float *rate);
void calculate2(student *s, int n);
void output1(float max, float min, float average, float rate);
void output2(student *s, int n);
int main()
{
    int n;
    student *s;
    float max, min, average, rate;
    printf("请输入学生人数:");
    scanf("%!d(MISSING)", &n);
    s = (student*)malloc(sizeof(student)*n);
    input(s, n);
    calculate1(s, n, &max, &min, &average, &rate);
    calculate2(s, n);
    output1(max, min, average, rate);
    output2(s, n);
    free(s);
    return 0;
}
void input(student *s, int n)
{
    int i;
    for(i=0; i<n; i++)
    {
        printf("请输入第%!d(MISSING)个学生的学号:", i+1);
        scanf("%!d(MISSING)", &s[i].stu_id);
        printf("请输入第%!d(MISSING)个学生的姓名:", i+1);
        scanf("%!s(MISSING)", s[i].stu_name);
        printf("请输入第%!d(MISSING)个学生的平时成绩:", i+1);
        scanf("%!f(MISSING)", &s[i].score_class);
        printf("请输入第%!d(MISSING)个学生的卷面成绩:", i+1);
        scanf("%!f(MISSING)", &s[i].score_test);
    }
}
void calculate1(student *s, int n, float *max, float *min, float *average, float *rate)
{
    int i;
    float temp;
    *max = s[0].score_test;
    *min = s[0].score_test;
    *average = 0;
    *rate = 0;
    for(i=0; i<n; i++)
    {
        temp = s[i].score_test;
        if(temp > *max)
            *max = temp;
        if(temp < *min)
            *min = temp;
        *average = *average + temp;
        if(temp >= 60)
            *rate = *rate + 1;
    }
    *average = *average / n;
    *rate = *rate / n * 100;
}
void calculate2(student *s, int n)
{
    int i;
    for(i=0; i<n; i++)
    {
        s[i].score_test = s[i].score_class * 0.4 + s[i].score_test * 0.6;
    }
}
void output1(float max, float min, float average, float rate)
{
    printf("卷面成绩最高分:%!f(MISSING)\n", max);
    printf("卷面成绩最低分:%!f(MISSING)\n", min);
    printf("卷面成绩平均分:%!f(MISSING)\n", average);
    printf("卷面成绩及格率:%!f(MISSING)%\n", rate);
}
void output2(student *s, int n)
{
    int i, j;
    student temp;
    for(i=0; i<n-1; i++)
    {
        for(j=i+1; j<n; j++)
        {
            if(s[j].score_test > s[i].score_test)
            {
                temp = s[i];
                s[i] = s[j];
                s[j] = temp;
            }
            else if(s[j].score_test == s[i].score_test && s[j].stu_id < s[i].stu_id)
            {
                temp = s[i];
                s[i] = s[j];
                s[j] = temp;
            }
        }
    }
    printf("学号\t姓名\t总成绩\n");
    for(i=0; i<n; i++)
    {
        printf("%!d(MISSING)\t%!s(MISSING)\t%!f(MISSING)\n", s[i].stu_id, s[i].stu_name, s[i].score_test);
    }
}

参考GPT和自己的思路,以下是一个符合要求的C语言课程成绩管理程序:

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

typedef struct student {
    char id[20];
    char name[20];
    float usualScore;
    float examScore;
    float totalScore;
} Student;

void input(int n, Student **students) {
    int i;
    for (i = 0; i < n; i++) {
        printf("请输入第%d个学生的信息:\n", i + 1);
        printf("学号:");
        scanf("%s", students[i]->id);
        printf("姓名:");
        scanf("%s", students[i]->name);
        printf("平时成绩:");
        scanf("%f", &(students[i]->usualScore));
        printf("卷面成绩:");
        scanf("%f", &(students[i]->examScore));
    }
}

void calculate1(int n, Student **students, float *maxScore, float *minScore, float *averageScore, float *passRate) {
    int i, passCount = 0;
    *maxScore = students[0]->examScore;
    *minScore = students[0]->examScore;
    *averageScore = 0;
    for (i = 0; i < n; i++) {
        if (students[i]->examScore > *maxScore) {
            *maxScore = students[i]->examScore;
        }
        if (students[i]->examScore < *minScore) {
            *minScore = students[i]->examScore;
        }
        *averageScore += students[i]->examScore;
        if (students[i]->totalScore >= 60) {
            passCount++;
        }
    }
    *averageScore /= n;
    *passRate = (float)passCount / n;
}

void calculate2(int n, Student **students) {
    int i;
    for (i = 0; i < n; i++) {
        students[i]->totalScore = students[i]->usualScore * 0.4 + students[i]->examScore * 0.6;
    }
}

void output1(float maxScore, float minScore, float averageScore, float passRate) {
    printf("卷面成绩最高分:%.2f\n", maxScore);
    printf("卷面成绩最低分:%.2f\n", minScore);
    printf("卷面成绩平均分:%.2f\n", averageScore);
    printf("及格率:%.2f%%\n", passRate * 100);
}

void output2(int n, Student **students) {
    int i, j;
    Student *temp;
    for (i = 0; i < n - 1; i++) {
        for (j = i + 1; j < n; j++) {
            if (students[i]->totalScore < students[j]->totalScore || (students[i]->totalScore == students[j]->totalScore && strcmp(students[i]->id, students[j]->id) > 0)) {
                temp = students[i];
                students[i] = students[j];
                students[j] = temp;
            }
        }
    }
    // 按总成绩从高到低排序
for (i = 0; i < n - 1; i++) {
    for (j = 0; j < n - i - 1; j++) {
        if (students[j].total_score < students[j+1].total_score) {
            // 交换两个学生的位置
            swap(&students[j], &students[j+1]);
        } else if (students[j].total_score == students[j+1].total_score) {
            // 如果总成绩相同,按学号从小到大排序
            if (strcmp(students[j].id, students[j+1].id) > 0) {
                // 交换两个学生的位置
                swap(&students[j], &students[j+1]);
            }
        }
    }
}

// 输出按总成绩从高到低的所有学生信息
for (i = 0; i < n; i++) {
    printf("学号:%s 姓名:%s 总成绩:%.2f\n", students[i].id, students[i].name, students[i].total_score);
}

// 释放动态分配的内存
free(students);

return 0;
}

注意,在这段代码中,我们使用了 swap 函数来交换两个学生结构体的位置。具体实现可以参考下面的代码:

void swap(Student *a, Student *b) {
Student temp = *a;
*a = *b;
*b = temp;
}

这样,我们就完成了整个程序的编写。希望能对您有所帮助。