//3、删除信息 void Deletedatd() { if(stu_num == 0){ printf("当前没有学生信息。\n"); } int i,j,flag=0; int studentID1; printf("请输入要删除学生的学号:"); scanf("%d",&studentID1); for(i=0;i<n;i++) { if(stu[i].studentID==studentID1) { flag=1; for(j=i;j<n-1;j++) { stu[j]=stu[j+1]; } } } getch(); stu_num=stu_num-1; }
时间: 2024-03-31 19:36:54 浏览: 22
这是一段 C 语言代码,实现了一个删除学生信息的函数。函数名为 Deletedatd,没有返回值。该函数首先判断是否有学生信息,如果没有则输出提示信息。接着要求用户输入要删除学生的学号,然后在学生信息数组 stu 中查找是否有该学号对应的学生信息。如果找到了,则将该学生信息从数组中删除,即将后面的学生信息依次向前移动一个位置。最后,将学生总数减一,表示成功删除一个学生信息。
需要注意的是,该代码只能删除一个学生信息,如果有多个学生信息对应该学号,则只会删除第一个。另外,该函数没有做输入合法性检查,如果用户输入的学号不合法或者学号不存在,则可能会导致程序出错。
相关问题
// 添加学生 void add_student() { if (stu_num >= MAX_STU_NUM) { printf("\t\t\t学生数已达上限,添加失败!\n"); return; } printf("\n\t\t\t请输入学生信息:学号 姓名 密码 身份证号 宿舍号 班级"); scanf("%s %s %s %s %s %s", students[stu_num].id, students[stu_num].name, students[stu_num].pwd, students[stu_num].id_card, students[stu_num].dorm, students[stu_num].cls); students[stu_num].status = 0; // 初始报到状态为0(未报到) stu_num++; printf("\t\t\t添加成功!\n"); }添加一个学号存在则添加失败的代码
可以在添加学生时,先遍历已有的学生信息,判断新输入的学生学号是否已经存在,如果已经存在则添加失败,否则继续添加学生信息。可以按照以下方式修改代码:
```
// 添加学生
void add_student() {
if (stu_num >= MAX_STU_NUM) {
printf("\t\t\t学生数已达上限,添加失败!\n");
return;
}
char new_id[ID_LEN]; // 新输入的学生学号
printf("\n\t\t\t请输入学生信息:学号 姓名 密码 身份证号 宿舍号 班级");
scanf("%s %s %s %s %s %s", new_id, students[stu_num].name,
students[stu_num].pwd, students[stu_num].id_card, students[stu_num].dorm,
students[stu_num].cls);
// 遍历已有的学生信息,判断新输入的学生学号是否已经存在
for (int i = 0; i < stu_num; i++) {
if (strcmp(students[i].id, new_id) == 0) {
printf("\t\t\t学号已存在,添加失败!\n");
return;
}
}
// 学号不存在,继续添加学生信息
strcpy(students[stu_num].id, new_id);
students[stu_num].status = 0; // 初始报到状态为0(未报到)
stu_num++;
printf("\t\t\t添加成功!\n");
}
```
// 添加学生 void add_student() { int i; if (stu_num >= MAX_STU_NUM) { printf("\t\t\t学生数已达上限,添加失败!\n"); return; } for (i = 0; i < stu_num; i++) { printf("请输入新添加的学生学号:"); scanf("%s",id); if (strcmp(students[i].id, id) == 0) { printf("该学号已存在!\n"); }优化此段代码
可以将输入新添加学生学号的代码放到for循环外面,避免重复输入,同时可以在输入学号时进行格式和输入值的验证,例如:
```
void add_student() {
int i;
char id[ID_LEN]; // 学号长度需要定义
if (stu_num >= MAX_STU_NUM) {
printf("\t\t\t学生数已达上限,添加失败!\n");
return;
}
printf("请输入新添加的学生学号:");
scanf("%s", id);
// 进行格式和输入值的验证
if (strlen(id) != ID_LEN || !is_valid_id(id)) {
printf("学号格式不正确或输入值不合法!\n");
return;
}
for (i = 0; i < stu_num; i++) {
if (strcmp(students[i].id, id) == 0) {
printf("该学号已存在!\n");
return;
}
}
strcpy(students[stu_num].id, id);
printf("请输入学生姓名:");
scanf("%s", students[stu_num].name);
// 其他属性的输入和处理
...
stu_num++; // 学生数加1
printf("添加成功!\n");
}
// 判断学号是否合法
int is_valid_id(char *id) {
int i;
for (i = 0; i < ID_LEN; i++) {
if (!isdigit(id[i])) {
return 0;
}
}
return 1;
}
```
这样就可以避免重复输入学号,并且增加了学号格式和输入值的验证,保证了数据的正确性。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)