C语言使用指针和函数的方式,查询自己的成绩。从键盘输入3位学生4门课程的成绩,输入学号,即可查询到自己各门课程的成绩。
时间: 2024-03-16 17:47:21 浏览: 22
好的,这道题需要你定义一个结构体来存储学生信息和成绩,然后使用指针和函数来进行查询。
下面是代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENT_NUM 100 // 最多学生数
#define COURSE_NUM 4 // 课程数
// 定义学生结构体
typedef struct student {
char id[20]; // 学号
char name[20]; // 姓名
int score[COURSE_NUM]; // 成绩
} Student;
// 初始化学生信息
void initStudent(Student *s, int n) {
int i, j;
for (i = 0; i < n; i++) {
printf("请输入第%d个学生的学号:", i + 1);
scanf("%s", s[i].id);
printf("请输入第%d个学生的姓名:", i + 1);
scanf("%s", s[i].name);
printf("请输入第%d个学生的成绩:\n", i + 1);
for (j = 0; j < COURSE_NUM; j++) {
printf("请输入第%d门课程的成绩:", j + 1);
scanf("%d", &s[i].score[j]);
}
}
}
// 查询学生信息
void queryStudent(Student *s, int n, char *id) {
int i, j;
for (i = 0; i < n; i++) {
if (strcmp(s[i].id, id) == 0) { // 学号匹配
printf("学号\t姓名\t成绩1\t成绩2\t成绩3\t成绩4\n");
printf("%s\t%s\t", s[i].id, s[i].name);
for (j = 0; j < COURSE_NUM; j++) {
printf("%d\t", s[i].score[j]);
}
printf("\n");
return;
}
}
printf("未找到该学生!\n");
}
int main() {
Student s[MAX_STUDENT_NUM];
int n;
char id[20];
printf("请输入学生个数:");
scanf("%d", &n);
initStudent(s, n);
printf("请输入要查询的学生学号:");
scanf("%s", id);
queryStudent(s, n, id);
return 0;
}
```
运行程序后,输入学生信息和成绩,然后输入要查询的学生学号即可查询到该学生各门课程的成绩。
相关推荐
![](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)