定义一个包含学生信息(学号,姓名,成绩)的顺序表,使其具有如下功能: (1) 根据指定学生个数,逐个输入学生信息; (2) 逐个显示学生表中所有学生的相关信息; (3) 根据姓名进行查找,返回此学生的学号和成绩; (4) 根据指定的位置可返回相应的学生信息(学号,姓名,成绩); (5) 给定一个学生信息,插入到表中指定的位置; (6) 删除指定位置的学生记录; (7) 统计表中学生个数。
时间: 2023-05-12 09:03:01 浏览: 39
好的,以下是我对你的问题的回答:
(1) 以下是一个示例代码,可以根据指定学生个数,逐个输入学生信息:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENT_NUM 100
typedef struct {
int id;
char name[20];
int score;
} Student;
typedef struct {
Student students[MAX_STUDENT_NUM];
int count;
} StudentList;
void inputStudentList(StudentList *list, int count) {
list->count = count;
for (int i = 0; i < count; i++) {
printf("请输入第%d个学生的学号、姓名、成绩:", i + 1);
scanf("%d %s %d", &list->students[i].id, list->students[i].name, &list->students[i].score);
}
}
```
(2) 以下是一个示例代码,可以逐个显示学生表中所有学生的相关信息:
```c
void printStudentList(StudentList *list) {
printf("学号\t姓名\t成绩\n");
for (int i = 0; i < list->count; i++) {
printf("%d\t%s\t%d\n", list->students[i].id, list->students[i].name, list->students[i].score);
}
}
```
(3) 以下是一个示例代码,可以根据姓名进行查找,返回此学生的学号和成绩:
```c
void findStudentByName(StudentList *list, char *name, int *id, int *score) {
for (int i = 0; i < list->count; i++) {
if (strcmp(list->students[i].name, name) == 0) {
*id = list->students[i].id;
*score = list->students[i].score;
return;
}
}
*id = -1;
*score = -1;
}
```
(4) 以下是一个示例代码,可以根据指定的位置可返回相应的学生信息(学号,姓名,成绩):
```c
void getStudentByIndex(StudentList *list, int index, int *id, char *name, int *score) {
if (index >= 0 && index < list->count) {
*id = list->students[index].id;
strcpy(name, list->students[index].name);
*score = list->students[index].score;
} else {
*id = -1;
strcpy(name, "");
*score = -1;
}
}
```
(5) 以下是一个示例代码,可以给定一个学生信息,插入到表中指定的位置:
```c
void insertStudent(StudentList *list, int index, int id, char *name, int score) {
if (list->count >= MAX_STUDENT_NUM) {
printf("学生表已满,无法插入!\n");
return;
}
if (index < 0 || index > list->count) {
printf("插入位置不合法!\n");
return;
}
for (int i = list->count - 1; i >= index; i--) {
list->students[i + 1] = list->students[i];
}
list->students[index].id = id;
strcpy(list->students[index].name, name);
list->students[index].score = score;
list->count++;
}
```
(6) 以下是一个示例代码,可以删除指定位置的学生记录:
```c
void deleteStudent(StudentList *list, int index) {
if (index < 0 || index >= list->count) {
printf("删除位置不合法!\n");
return;
}
for (int i = index; i < list->count - 1; i++) {
list->students[i] = list->students[i + 1];
}
list->count--;
}
```
(7) 以下是一个示例代码,可以统计表中学生个数:
```c
int countStudent(StudentList *list) {
return list->count;
}
```
希望以上回答能够帮助到你!
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)