You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
drd/学籍管理系统5.cpp

94 lines
3.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义学生结构体,包含学号、班级、姓名以及三门课程成绩
typedef struct Student {
char id[20]; // 使用字符串存储学号,便于处理用户输入(可能是学号或姓名)
int class_num;
char name[20];
double score1;
double score2;
double score3;
} Student;
// 交换两个学生结构体的函数
void swap(Student *a, Student *b) {
Student temp = *a;
*a = *b;
*b = temp;
}
// 计算学生总成绩的函数,方便后续比较排序(虽然本题删除操作未直接用到,但保持代码完整性)
double totalScore(const Student *s) {
return s->score1 + s->score2 + s->score3;
}
// 根据输入的学号或姓名查找学生在数组中的索引位置,若没找到返回 -1
int findStudentIndex(Student students[], int n, char target[20]) {
for (int i = 0; i < n; i++) {
if (strcmp(students[i].id, target) == 0 || strcmp(students[i].name, target) == 0) {
return i;
}
}
return -1;
}
// 删除指定学号或姓名的学生信息,并保持数组有序
void deleteStudent(Student students[], int *n, char target[20]) {
int index = findStudentIndex(students, *n, target);
if (index!= -1) {
for (int i = index; i < *n - 1; i++) {
students[i] = students[i + 1];
}
(*n)--;
}
}
// 打印学生数组信息
void printStudents(Student students[], int n) {
for (int i = 0; i < n; i++) {
printf("%s %d %s %.1lf %.1lf %.1lf\n", students[i].id, students[i].class_num, students[i].name,
students[i].score1, students[i].score2, students[i].score3);
}
}
int main() {
Student students[100]; // 假设最多存储100个学生信息可根据实际情况调整大小
int num_students = 3; // 初始已有3个学生信息
// 静态初始化已有三个学生信息(按题目给定示例)
Student s1 = {"10001", 11, "Zhang", 99.5, 88.5, 89.5};
Student s2 = {"10002", 12, "Yang", 77.9, 56.5, 87.5};
Student s3 = {"10003", 11, "Liang", 92.5, 99.0, 60.5};
students[0] = s1;
students[1] = s2;
students[2] = s3;
char input[20];
printf("please input:\n");
scanf("%s", input);
// 查找要删除的学生是否存在
int index = findStudentIndex(students, num_students, input);
if (index == -1) {
// 如果学生不存在,直接输出原有学生信息并结束程序
printStudents(students, num_students);
return 0;
}
// 如果学生存在,询问是否确定删除
printf("Are you sure(yes/no)?\n");
char choice[10];
scanf("%s", choice);
if (strcmp(choice, "n") == 0) {
// 用户输入 n输出所有学生信息后结束程序
printStudents(students, num_students);
} else if (strcmp(choice, "y") == 0) {
// 用户输入 y执行删除操作并输出剩余学生信息
deleteStudent(students, &num_students, input);
printStudents(students, num_students);
}
return 0;
}