m0_66051118 2022-01-05 20:18 采纳率: 76.3%
浏览 31
已结题

这段代码哪里错了啊?

#include<stdio.h>
#include<malloc.h>
#include<assert.h>
#define N 5
struct Student
{
int num;
char name[20];
double score[3];
};

Student* Input(Student* Stu, int len)
{
assert(Stu != NULL);
printf("请输入学生信息,编号 姓名 课程1成绩,课程2成绩,课程3成绩\n");
for (int i = 0; i < len; i++)
{
scanf_s("%d %s", &Stu[i].num, &Stu[i].name, 20);
for (int j = 0; j < 3; j++)
{
scanf_s("%lf", &Stu[i].score[j]);
}

}
return Stu;

}
void Print(Student* Stu, int len)
{
int count = 0;
double tmp = 0;
double max = 0;
double tmp1 = 0;
double tmp2 = 0;
double tmp3 = 0;
double* arr = (double*)malloc(sizeof(double) * len);
double* brr = (double*)malloc(sizeof(double) * len);
double* crr = (double*)malloc(sizeof(double) * len);
double* drr = (double*)malloc(sizeof(double) * len);
for (int i = 0; i < len; i++)
{
tmp1 += Stu[i].score[0];
tmp2 += Stu[i].score[1];
tmp3 += Stu[i].score[2];
for (int j = 0; j < 3; j++)
{
tmp += Stu[i].score[j];
}
arr[i] = tmp;
tmp = 0;
}
for (int i = 0; i < len; i++)
{
if (max < arr[i])
{
max = arr[i];
count = i;
}
}
tmp1 = tmp1 / len;
tmp2 = tmp2 / len;
tmp3 = tmp3 / len;
printf("课程1平均成绩:%f 课程2平均成绩: %f 课程3平均成绩: %f\n", tmp1, tmp2, tmp3);
printf("最高分学生数据为: %d %s ", Stu[count].num, Stu[count].name);
for (int i = 0; i < 3; i++)
{
printf("%f ", Stu[count].score[i]);
}
printf("%f\n", max/3);

//释放动态内存
free(arr);
free(brr);
free(crr);
free(drr);
}
int main()
{

Student Stu1[N] = { 0 };
Input(Stu1, (sizeof(Stu1) / sizeof(Stu1[0])));
Print(Stu1, (sizeof(Stu1) / sizeof(Stu1[0])));


return 0;

}

  • 写回答

1条回答 默认 最新

  • qzjhjxj 2022-01-06 08:54
    关注

    修改如下,供参考:

    #include<stdio.h>
    #include<malloc.h>
    #include<assert.h>
    #define N 5     //人数
    #define M 3     //课程数
    struct Student{
        int  num;
        char name[20];
        double score[M];
    };
    void Input(Student* Stu, int len) //Student* Input(Student* Stu, int len)
    {
        int i,j;
        printf("请输入学生信息,编号 姓名  %d门课程成绩\n",M);//课程1成绩,课程2成绩,课程3成绩\n");
        for (i = 0; i < len; i++)
        {
            scanf("%d %s", &Stu[i].num, Stu[i].name);
            for (j = 0; j < M; j++)
            {
                 scanf("%lf", &Stu[i].score[j]);
            }
        }
    }
    void Print(Student* Stu, int len)
    {
        int    i,j,count=0;
        double tmp = 0;
        double max = 0;
        double tmp1 = 0;
        double tmp2 = 0;
        double tmp3 = 0;
        for (i = 0; i < len; i++)
        {
            tmp1 += Stu[i].score[0];
            tmp2 += Stu[i].score[1];
            tmp3 += Stu[i].score[2];
            for (j = 0; j < M; j++)
            {
                tmp += Stu[i].score[j];
            }
            if (max < tmp)
            {
                max = tmp;
                count = i;
            }
            tmp = 0;
        }
        tmp1 = tmp1 / len;
        tmp2 = tmp2 / len;
        tmp3 = tmp3 / len;
        printf("课程1平均成绩:%.2f 课程2平均成绩: %.2f 课程3平均成绩: %.2f\n", tmp1, tmp2, tmp3);
        printf("最高分学生数据为: %d %s ", Stu[count].num, Stu[count].name);
        for (i = 0; i < M; i++)
        {
            printf("%.2f ", Stu[count].score[i]);
        }
        printf("%.2f\n", max/M);
    }
    int main()
    {
        Student Stu1[N] = { 0 };
        //Input(Stu1, (sizeof(Stu1) / sizeof(Stu1[0])));
        Input(Stu1, N);
        //Print(Stu1, (sizeof(Stu1) / sizeof(Stu1[0])));
        Print(Stu1, N);
        return 0;
    }
     
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

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

悬赏问题

  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问