C语言基础 --有5个结构体变量,内含学生学号、姓名和3门课程的成绩。要求输出平均成绩最高的学生的信息(包括学号、姓名、3门课程成绩和平均成绩)。要求用结构体变量和结构体变量的指针作函数参数

解题思路:

  1. 定义一个结构体,其中包括学生学号、姓名和3门课程的成绩;
  2. 创建若干个结构体变量,并初始化;
  3. 定义一个函数,以结构体变量作为参数,用于计算平均成绩;
  4. 定义一个函数,以结构体指针作为参数,用于找到平均成绩最高的学生,输出该学生的信息;

以下是具体的代码实现:

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

#define MAX_STU_NUM 5

// 定义学生结构体
struct student {
    char id[10];
    char name[20];
    double score[3];   // 具体成绩
    double aver_score; // 平均成绩
};

// 计算某个学生的平均成绩
void calc_score(struct student *stu) {
    double sum = 0;
    for (int i = 0; i < 3; i++) {
        sum += stu->score[i];
    }
    stu->aver_score = sum / 3;
}

// 找到平均成绩最高的学生
void find_max_score(struct student *stus, int num) {
    double max_score = 0.0;
    int index = 0;

    for (int i = 0; i < num; i++) {
        struct student stu = stus[i];
        calc_score(&stu);

        if (max_score < stu.aver_score) {
            max_score = stu.aver_score;
            index = i;
        }
    }

    struct student max_stu = stus[index];
    calc_score(&max_stu);

    printf("平均成绩最高的学生信息如下:\n");
    printf("学号:%s\n", max_stu.id);
    printf("姓名:%s\n", max_stu.name);
    printf("成绩:%lf, %lf, %lf\n", max_stu.score[0], max_stu.score[1], max_stu.score[2]);
    printf("平均成绩:%lf\n", max_stu.aver_score);
}

int main(void) {
    // 创建MAX_STU_NUM个学生结构体变量
    struct student stus[MAX_STU_NUM] = {
        {"001", "张三", {90, 80, 85}},
        {"002", "李四", {75, 85, 65}},
        {"003", "王五", {60, 50, 70}},
        {"004", "赵六", {95, 90, 92}},
        {"005", "孙七", {80, 88, 82}}
    };

    // 执行查找最高平均成绩学生的操作
    find_max_score(stus, MAX_STU_NUM);

    return 0;
}

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是用C语言定义一个学生类型的结构体包括学号姓名成绩定义五个学生变量并需要定义结构体数组,循环输入学生信息,循环输出学生信息的代码示例: ```c #include <stdio.h> #define MAX_STUDENTS 5 struct student { int id; char name[20]; double score; }; int main() { struct student students[MAX_STUDENTS]; // Input student information for (int i = 0; i < MAX_STUDENTS; i++) { printf("Enter student %d information:\n", i+1); printf("ID: "); scanf("%d", &students[i].id); printf("Name: "); scanf("%s", students[i].name); printf("Score: "); scanf("%lf", &students[i].score); } // Output student information printf("Student Information:\n"); for (int i = 0; i < MAX_STUDENTS; i++) { printf("ID: %d, Name: %s, Score: %.2lf\n", students[i].id, students[i].name, students[i].score); } return 0; } ``` 这个程序定义一个名为`student`的结构体,包含了三个成员:`id`表示学号(整型),`name`表示姓名(字符数组),`score`表示成绩(浮点型)。然后定义一个长度为5的结构体数组`students`用来存储五个学生信息。 在输入学生信息时,通过一个循环来逐一输入每个学生学号姓名成绩。在输出学生信息时,同样也通过一个循环来逐一输出每个学生信息。 注意,在输入姓名时使用了`scanf("%s", students[i].name)`,这里没有使用`&`符号。这是因为字符数组名本身就是一个指针,不需要再取地址。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值