设计一个学生信息管理程序,管理 8 个学生信息,能够按学号查询输出指定学生的信息(姓名,课程,成绩),也能输出全部学生信息。要求:1)输出函数采用非成员函数形式,以对象指针为形参;2)在主函数中采用对象数组形式实例化8个学生对象;3)测试两种输出形式。请用C++代码,不要用C语言
时间: 2023-06-07 15:12:18 浏览: 103
的标准库函数。
#include <stdio.h>
#include <string.h>
#define NUM_OF_STUDENT 8 // 学生数量
typedef struct student {
char name[20]; // 姓名
int id; // 学号
char course[20];// 课程
int score; // 成绩
} Student;
void output(Student* stu); // 非成员函数, 输出学生信息
void output_all(Student* stu, int len); // 非成员函数, 输出全部学生信息
int main() {
Student students[NUM_OF_STUDENT]; // 学生数组
int i;
for (i = 0; i < NUM_OF_STUDENT; i++) {
printf("请输入第%d个学生的姓名, 学号, 课程和成绩: ", i+1);
scanf("%s%d%s%d", students[i].name, &(students[i].id), students[i].course, &(students[i].score));
}
// 按学号查询学生信息
int id;
printf("请输入要查询的学生学号: ");
scanf("%d", &id);
for (i = 0; i < NUM_OF_STUDENT; i++) {
if (students[i].id == id) {
printf("根据学号查询, 学生信息如下:\n");
output(&(students[i]));
break;
}
}
if (i == NUM_OF_STUDENT) {
printf("没有找到学号为%d的学生\n", id);
}
// 输出所有学生信息
printf("输出所有学生信息:\n");
output_all(students, NUM_OF_STUDENT);
return 0;
}
// 非成员函数, 输出学生信息
void output(Student* stu) {
printf("姓名:%s\n学号:%d\n课程:%s\n成绩:%d\n", stu->name, stu->id, stu->course, stu->score);
}
// 非成员函数, 输出全部学生信息
void output_all(Student* stu, int len) {
int i;
for (i = 0; i < len; i++) {
printf("第%d个学生信息如下:\n", i+1);
output(stu+i);
}
}
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://img-home.csdnimg.cn/images/20210720083646.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)
![](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)