编写一个函数print,打印一个学生的成绩数组,该组中有五个学生的数据记录,每个记录包含num,name,score[3],编写另一函数input用来输入5个学生的数据记录。在主函数中用input函数输入这些记录,用print函数输出这些记录。
以下是使用C++编写的代码:
#include <iostream>
#include <string>
using namespace std;
struct Student {
int num;
string name;
float scores[3];
};
void input(Student students[]) {
for (int i = 0; i < 5; i++) {
cout << "请输入第 " << i+1 << " 个学生的学号:";
cin >> students[i].num;
cout << "请输入第 " << i+1 << " 个学生的姓名:";
cin >> students[i].name;
cout << "请输入第 " << i+1 << " 个学生的三门课程成绩(用空格分隔):";
cin >> students[i].scores[0] >> students[i].scores[1] >> students[i].scores[2];
}
}
void print(const Student students[]) {
for (int i = 0; i < 5; i++) {
cout << "学生 " << i+1 << " 的信息:" << endl;
cout << "学号:" << students[i].num << endl;
cout << "姓名:" << students[i].name << endl;
cout << "成绩:";
for (int j = 0; j < 3; j++) {
cout << students[i].scores[j] << " ";
}
cout << endl << endl;
}
}
int main() {
Student students[5];
input(students);
print(students);
return 0;
}
在这个代码中,我们定义了一个Student
结构体,包含学号、姓名和三门课程的成绩。input
函数用于输入学生数据,通过循环输入学号、姓名和成绩,并将数据保存在一个Student
类型的数组中。print
函数用于打印学生的数据记录,通过循环遍历数组,输出学号、姓名和成绩。
在main
函数中,我们首先创建一个大小为5的Student
数组students
,然后分别调用input
函数和print
函数,实现输入学生数据和打印学生记录的功能。
请注意,在C++中,结构体成员使用点操作符(.
)来访问。另外,代码中使用了iostream
和string
头文件来支持输入和输出操作。