void addStudent(struct Student student) { if (count < 100) { // 判断学生数是否超过了数组的最大容量 strcpy(students[count].id, student.id); strcpy(students[count].name, student.name); strcpy(students[count].gender, student.gender); students[count].chinese = student.chinese; students[count].math = student.math; students[count].english = student.english; count++; } else { printf("The number of students has reached the maximum capacity!\n"); }}引用这个函数
时间: 2024-02-25 11:56:25 浏览: 67
可以在主函数中调用 addStudent 函数,例如:
```c
int main() {
// 初始化几个学生的基本信息
struct Student students[] = {
{"1001", "张三", "Male", 80, 90, 85},
{"1002", "李四", "Female", 75, 85, 90},
{"1003", "王五", "Male", 90, 80, 95},
{"1004", "赵六", "Female", 85, 95, 80},
{"1005", "钱七", "Male", 95, 85, 90}
};
int count = 5;
// 打印原始的学生信息
printf("Original students:\n");
for (int i = 0; i < count; i++) {
printf("%s\t%s\t%s\t%d\t%d\t%d\n", students[i].id, students[i].name, students[i].gender, students[i].chinese, students[i].math, students[i].english);
}
// 添加一个新学生信息
struct Student newStudent = {"1006", "孙八", "Male", 85, 90, 80};
addStudent(newStudent);
// 打印添加新学生信息后的所有学生信息
printf("All students after adding new student:\n");
for (int i = 0; i < count; i++) {
printf("%s\t%s\t%s\t%d\t%d\t%d\n", students[i].id, students[i].name, students[i].gender, students[i].chinese, students[i].math, students[i].english);
}
return 0;
}
```
这个示例代码中,我们首先初始化了几个学生的信息,然后打印出原始的学生信息。接着,我们定义了一个新的学生信息,调用 addStudent 函数将其添加到 students 数组中。最后,再次打印出所有学生的信息,包括新添加的学生。
阅读全文