学生的基本信息包括学号、姓名、三门功课的成绩及平均分。参考例 9-3,输入一个学 生的信息,计算学生的平均成绩,并输出学生的完整信息。要求定义并调用函数 aver 计算 平均分,函数参数的类型是结构。
时间: 2023-09-20 11:05:50 浏览: 68
### 回答1:
参考代码如下:
```python
# 定义学生信息结构体
class Student:
def __init__(self, id, name, score1, score2, score3, average):
self.id = id
self.name = name
self.score1 = score1
self.score2 = score2
self.score3 = score3
self.average = average
# 定义计算平均分的函数
def aver(student):
student.average = (student.score1 + student.score2 + student.score3) / 3
# 输入学生信息
id = input("请输入学号:")
name = input("请输入姓名:")
score1 = float(input("请输入第一门课程成绩:"))
score2 = float(input("请输入第二门课程成绩:"))
score3 = float(input("请输入第三门课程成绩:"))
# 创建学生信息对象并计算平均分
student = Student(id, name, score1, score2, score3, 0)
aver(student)
# 输出学生完整信息
print("学号:%s,姓名:%s,成绩:%s,平均分:%.2f" % (student.id, student.name, [student.score1, student.score2, student.score3], student.average))
```
运行程序,输入如下信息:
```
请输入学号:001
请输入姓名:张三
请输入第一门课程成绩:80
请输入第二门课程成绩:78
请输入第三门课程成绩:85
```
程序将输出如下信息:
```
学号:001,姓名:张三,成绩:[80.0, 78.0, 85.0],平均分:81.00
```
### 回答2:
首先,我们可以定义一个结构体类型来表示学生的基本信息:
```
struct Student {
int studentNumber;
char name[20];
float score1;
float score2;
float score3;
float averageScore;
};
```
接下来,我们可以编写一个函数来计算学生的平均成绩,该函数的参数类型为结构体类型:
```
void aver(struct Student *student) {
student->averageScore = (student->score1 + student->score2 + student->score3) / 3;
}
```
在主函数中,我们可以先创建一个学生对象,然后输入学生的基本信息,计算平均成绩,并输出学生的完整信息:
```
int main() {
struct Student student;
printf("请输入学号:");
scanf("%d", &student.studentNumber);
printf("请输入姓名:");
scanf("%s", student.name);
printf("请输入第一门课成绩:");
scanf("%f", &student.score1);
printf("请输入第二门课成绩:");
scanf("%f", &student.score2);
printf("请输入第三门课成绩:");
scanf("%f", &student.score3);
aver(&student);
printf("学号:%d\n", student.studentNumber);
printf("姓名:%s\n", student.name);
printf("第一门课成绩:%f\n", student.score1);
printf("第二门课成绩:%f\n", student.score2);
printf("第三门课成绩:%f\n", student.score3);
printf("平均成绩:%f\n", student.averageScore);
return 0;
}
```
这样,当我们运行程序时,就可以输入学生的基本信息,然后自动计算平均成绩,并输出学生的完整信息。
### 回答3:
题目要求我们输入学生的基本信息(学号、姓名、三门功课成绩)并计算平均分,最后输出学生的完整信息。
首先,我们需要定义一个结构体来表示学生的基本信息,包括学号、姓名和成绩。然后,我们定义一个函数aver来计算平均分。
定义结构体:
```
struct Student {
int num; // 学号
char name[20]; // 姓名
float score1; // 第一门课成绩
float score2; // 第二门课成绩
float score3; // 第三门课成绩
float average; // 平均分
};
```
定义函数aver来计算平均分:
```
float aver(float score1, float score2, float score3) {
return (score1 + score2 + score3) / 3;
}
```
在主函数中,我们首先定义一个学生结构体并输入学生的基本信息。然后调用aver函数计算平均分,并将计算结果保存到学生结构体的average成员变量中。最后,输出学生的完整信息。
完整代码如下:
```
#include <stdio.h>
struct Student {
int num; // 学号
char name[20]; // 姓名
float score1; // 第一门课成绩
float score2; // 第二门课成绩
float score3; // 第三门课成绩
float average; // 平均分
};
float aver(float score1, float score2, float score3) {
return (score1 + score2 + score3) / 3;
}
int main() {
struct Student stu;
printf("请输入学号:");
scanf("%d", &stu.num);
printf("请输入姓名:");
scanf("%s", stu.name);
printf("请输入第一门课成绩:");
scanf("%f", &stu.score1);
printf("请输入第二门课成绩:");
scanf("%f", &stu.score2);
printf("请输入第三门课成绩:");
scanf("%f", &stu.score3);
stu.average = aver(stu.score1, stu.score2, stu.score3);
printf("\n学生信息:\n");
printf("学号:%d\n", stu.num);
printf("姓名:%s\n", stu.name);
printf("第一门课成绩:%.2f\n", stu.score1);
printf("第二门课成绩:%.2f\n", stu.score2);
printf("第三门课成绩:%.2f\n", stu.score3);
printf("平均分:%.2f\n", stu.average);
return 0;
}
```
这样,我们就能够输入学生的基本信息,并计算出平均分,最后输出学生的完整信息。
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)