定义一个结构体表示学生成绩,包含学生学号、姓名、数学成绩、语文成绩、英语成绩等信息。然后定义一个数组,存储多个学生的成绩信息。实现查找可以遍历数组,根据姓名查找对应学生的成绩信息。插入可以在数组末尾添加一个新的学生成绩信息。删除可以根据学号或姓名查找到对应学生的成绩信息,然后将其从数组中删除。修改可以根据姓名查找到对应学生的成绩信息,然后修改其数学成绩、语文成绩、英语成绩等信息。使用代码实现
时间: 2023-04-09 19:01:39 浏览: 72
以下是代码实现:
```
#include <stdio.h>
#include <string.h>
#define MAX_STUDENT_NUM 100
struct Student {
char id[20];
char name[20];
float math_score;
float chinese_score;
float english_score;
};
int find_student_by_name(struct Student students[], int num_students, char name[]) {
for (int i = 0; i < num_students; i++) {
if (strcmp(students[i].name, name) == 0) {
return i;
}
}
return -1;
}
void print_student(struct Student student) {
printf("ID: %s, Name: %s, Math: %.2f, Chinese: %.2f, English: %.2f\n",
student.id, student.name, student.math_score, student.chinese_score, student.english_score);
}
void print_students(struct Student students[], int num_students) {
for (int i = 0; i < num_students; i++) {
print_student(students[i]);
}
}
void insert_student(struct Student students[], int *num_students, struct Student new_student) {
if (*num_students >= MAX_STUDENT_NUM) {
printf("Error: too many students.\n");
return;
}
students[*num_students] = new_student;
(*num_students)++;
}
void delete_student_by_id(struct Student students[], int *num_students, char id[]) {
int index = -1;
for (int i = 0; i < *num_students; i++) {
if (strcmp(students[i].id, id) == 0) {
index = i;
break;
}
}
if (index == -1) {
printf("Error: student not found.\n");
return;
}
for (int i = index; i < *num_students - 1; i++) {
students[i] = students[i + 1];
}
(*num_students)--;
}
void delete_student_by_name(struct Student students[], int *num_students, char name[]) {
int index = find_student_by_name(students, *num_students, name);
if (index == -1) {
printf("Error: student not found.\n");
return;
}
for (int i = index; i < *num_students - 1; i++) {
students[i] = students[i + 1];
}
(*num_students)--;
}
void modify_student(struct Student students[], int num_students, char name[], float math_score, float chinese_score, float english_score) {
int index = find_student_by_name(students, num_students, name);
if (index == -1) {
printf("Error: student not found.\n");
return;
}
students[index].math_score = math_score;
students[index].chinese_score = chinese_score;
students[index].english_score = english_score;
}
int main() {
struct Student students[MAX_STUDENT_NUM];
int num_students = 0;
// insert some students
insert_student(students, &num_students, (struct Student){"001", "Alice", 90, 80, 85});
insert_student(students, &num_students, (struct Student){"002", "Bob", 85, 90, 95});
insert_student(students, &num_students, (struct Student){"003", "Charlie", 95, 95, 90});
// print all students
printf("All students:\n");
print_students(students, num_students);
// find a student by name
char name_to_find[] = "Bob";
int index = find_student_by_name(students, num_students, name_to_find);
if (index != -1) {
printf("Found student by name '%s':\n", name_to_find);
print_student(students[index]);
} else {
printf("Student not found by name '%s'.\n", name_to_find);
}
// insert a new student
insert_student(students, &num_students, (struct Student){"004", "David", 80, 85, 90});
printf("After inserting a new student:\n");
print_students(students, num_students);
// delete a student by id
char id_to_delete[] = "002";
delete_student_by_id(students, &num_students, id_to_delete);
printf("After deleting a student by id '%s':\n", id_to_delete);
print_students(students, num_students);
// delete a student by name
char name_to_delete[] = "Charlie";
delete_student_by_name(students, &num_students, name_to_delete);
printf("After deleting a student by name '%s':\n", name_to_delete);
print_students(students, num_students);
// modify a student
char name_to_modify[] = "Alice";
float new_math_score = 95;
float new_chinese_score = 90;
float new_english_score = 80;
modify_student(students, num_students, name_to_modify, new_math_score, new_chinese_score, new_english_score);
printf("After modifying a student by name '%s':\n", name_to_modify);
print_students(students, num_students);
return 0;
}
```
阅读全文