关于#结构体#的问题,如何解决?(语言-c语言)

【题目描述】

完成函数fun,利用结构体变量存储了一名学生的姓名和3门课的成绩,输入学生的姓名、3门课成绩和系数a,将该学生的各科成绩都乘以一个系数a,并输出该生信息(成绩保留1位小数)。(仅提交函数)

【输入】

学生信息及系数

【输出】

修改后的学生信息

【样例输入】

Zhanghua 76.5 78.0 82.0 1

【样例输出】

Zhanghua 76.5 78.0 82.0

【前置信息】

#include

#include

#include

typedef struct

{

char name[9];

float score[3];

}STU;

int main( )

{

STU std;

float a;

int i;

scanf("%s%f%f%f%f",&std.name,&std.score[0],&std.score[1],&std.score[2],&a);

fun(&std,&a);

printf("%s",std.name);

for(i=0; i<3; i++)

    printf("% .1f",std.score[i]);

printf("\n");

return 0;

}

  • 先看截图

img

  • 代码参考如下:
#include <stdio.h>

#include <string.h>

#include <stdio.h>

typedef struct
{
    char name[9];   
    float score[3];
}STU;

void fun(STU *std, float *a)
{
    std->score[0] *= *a;
    std->score[1] *= *a;
    std->score[2] *= *a;
}

int main( )
{
    STU std;
     
    float a;
     
    int i;
     
    scanf("%s%f%f%f%f",std.name,&std.score[0],&std.score[1],&std.score[2],&a);
     
    fun(&std,&a);
     
    printf("%s",std.name);
     
    for(i=0; i<3; i++)
        printf("% .1f",std.score[i]);
     
    printf("\n");
     
    return 0;
}


void fun(STU *stu, float *a)
{
    for (int i = 0; i < 3; i++)
        stu->score[i] *= *a;
}