用c语言对输出学生学号、姓名、班级信息并能对其用locate函数查找和删除插入操作
时间: 2024-04-29 19:25:28 浏览: 18
由于没有给出具体的数据结构和操作方式,以下代码仅提供一个简单的示例,仅供参考。
```c
#include <stdio.h>
#include <string.h>
#define MAX_STUDENT 100 // 最大学生数
#define MAX_NAME 20 // 姓名最大长度
#define MAX_ID 10 // 学号最大长度
#define MAX_CLASS 10 // 班级最大长度
struct student {
char id[MAX_ID];
char name[MAX_NAME];
char class[MAX_CLASS];
};
struct student students[MAX_STUDENT]; // 学生信息数组
int num_students = 0; // 学生数
// 定义查找学生函数
int locate_student(char *id) {
int i;
for (i = 0; i < num_students; i++) {
if (strcmp(students[i].id, id) == 0) {
return i;
}
}
return -1; // 没找到
}
// 定义删除学生函数
void delete_student(char *id) {
int idx = locate_student(id);
if (idx >= 0) {
// 将该学生后面的所有学生向前移动一个位置
for (; idx < num_students - 1; idx++) {
students[idx] = students[idx + 1];
}
num_students--;
printf("删除成功!\n");
} else {
printf("未找到该学生!\n");
}
}
// 定义插入学生函数
void insert_student(struct student s) {
if (num_students >= MAX_STUDENT) {
printf("学生数已达到最大值!\n");
return;
}
int idx = locate_student(s.id);
if (idx >= 0) {
printf("该学生已存在!\n");
return;
}
students[num_students++] = s;
printf("插入成功!\n");
}
int main() {
// 先手动添加一些学生
struct student s1 = {"1001", "张三", "01"};
struct student s2 = {"1002", "李四", "02"};
struct student s3 = {"1003", "王五", "03"};
students[0] = s1;
students[1] = s2;
students[2] = s3;
num_students = 3;
// 输出所有学生信息
printf("所有学生信息:\n");
int i;
for (i = 0; i < num_students; i++) {
printf("%s\t%s\t%s\n", students[i].id, students[i].name, students[i].class);
}
// 查找学生
char id[MAX_ID];
printf("请输入要查找的学生学号:");
scanf("%s", id);
int idx = locate_student(id);
if (idx >= 0) {
printf("找到该学生,信息如下:%s\t%s\t%s\n", students[idx].id, students[idx].name, students[idx].class);
} else {
printf("未找到该学生!\n");
}
// 删除学生
printf("请输入要删除的学生学号:");
scanf("%s", id);
delete_student(id);
// 插入学生
struct student s4 = {"1004", "赵六", "04"};
insert_student(s4);
// 输出所有学生信息
printf("所有学生信息:\n");
for (i = 0; i < num_students; i++) {
printf("%s\t%s\t%s\n", students[i].id, students[i].name, students[i].class);
}
return 0;
}
```
需要注意的是,该示例中只能根据学号进行查找和删除操作,如果需要根据其他属性进行操作,需要修改代码中的结构体定义和相关函数。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.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)