TechWhizKid参考GPT回答:
#include <stdio.h>
#include <string.h>
struct student {
int id; // 学生序号
char no[11]; // 学号
char name[10]; // 姓名
char sex; // 性别:'F'或'M'
int age; // 年龄
};
void InputData(struct student *stu, int n) {
for (int i = 0; i < n; i++) {
printf("请输入第%d名学生的信息:\n", i + 1);
printf("学号:");
scanf("%s", stu[i].no);
printf("姓名:");
scanf("%s", stu[i].name);
printf("性别:");
scanf(" %c", &stu[i].sex);
printf("年龄:");
scanf("%d", &stu[i].age);
stu[i].id = i + 1;
}
}
int QueryData(struct student *stu, char *stu_no) {
for (int i = 0; i < 5; i++) {
if (strcmp(stu[i].no, stu_no) == 0) {
return i; // 学号存在,返回学生在数组中的索引
}
}
return -1; // 学号不存在
}
void DeleteData(struct student *stu, char *stu_no) {
int index = QueryData(stu, stu_no);
if (index != -1) {
for (int i = index; i < 4; i++) {
strcpy(stu[i].no, stu[i + 1].no);
strcpy(stu[i].name, stu[i + 1].name);
stu[i].sex = stu[i + 1].sex;
stu[i].age = stu[i + 1].age;
stu[i].id = stu[i + 1].id - 1;
}
printf("删除成功!\n");
} else {
printf("该学号不存在!\n");
}
}
void OutputData(struct student *stu, int i) {
printf("学号:%s\n", stu[i].no);
printf("姓名:%s\n", stu[i].name);
printf("性别:%c\n", stu[i].sex);
printf("年龄:%d\n", stu[i].age);
}
int main() {
struct student stu[5];
// 调用InputData函数实现对5名学生的信息录入
InputData(stu, 5);
// 调用QueryData函数实现对某输入学号的查询
char query_no[11];
printf("请输入要查询的学号:");
scanf("%s", query_no);
int query_index = QueryData(stu, query_no);
if (query_index != -1) {
printf("查询结果:\n");
OutputData(stu, query_index);
// 调用DeleteData函数进行删除
printf("是否删除该学生信息?(Y/N)");
char choice;
scanf(" %c", &choice);
if (choice == 'Y' || choice == 'y') {
DeleteData(stu, query_no);
}
} else {
printf("该学号不存在!\n");
}
// 调用OutputData函数实现对全部的学生信息进行输出
printf("全部学生信息:\n");
for (int i = 0; i < 4; i++) {
OutputData(stu, i);
printf("--------------\n");
}
return 0;
}