定义一个结构体表示学生成绩,包含学生学号、姓名、数学成绩、语文成绩、英语成绩等信息。然后定义一个数组,存储多个学生的成绩信息。实现查找可以遍历数组,根据姓名查找对应学生的成绩信息。插入可以在数组末尾添加一个新的学生成绩信息。删除可以根据学号或姓名查找到对应学生的成绩信息,然后将其从数组中删除。修改可以根据姓名查找到对应学生的成绩信息,然后修改其数学成绩、语文成绩、英语成绩等信息。使用C语言实现
时间: 2023-04-09 17:01:43 浏览: 212
以下是C语言实现:
```
#include <stdio.h>
#include <string.h>
#define MAX_STUDENT_NUM 100
struct Student {
char id[20];
char name[20];
int math_score;
int chinese_score;
int english_score;
};
struct Student students[MAX_STUDENT_NUM];
int student_num = 0;
void print_student(struct Student student) {
printf("id: %s, name: %s, math score: %d, chinese score: %d, english score: %d\n",
student.id, student.name, student.math_score, student.chinese_score, student.english_score);
}
void print_all_students() {
for (int i = 0; i < student_num; i++) {
print_student(students[i]);
}
}
void insert_student(struct Student student) {
if (student_num >= MAX_STUDENT_NUM) {
printf("Error: too many students\n");
return;
}
students[student_num++] = student;
}
void delete_student_by_id(char* id) {
for (int i = 0; i < student_num; i++) {
if (strcmp(students[i].id, id) == 0) {
for (int j = i; j < student_num - 1; j++) {
students[j] = students[j + 1];
}
student_num--;
return;
}
}
printf("Error: student not found\n");
}
void delete_student_by_name(char* name) {
for (int i = 0; i < student_num; i++) {
if (strcmp(students[i].name, name) == 0) {
for (int j = i; j < student_num - 1; j++) {
students[j] = students[j + 1];
}
student_num--;
return;
}
}
printf("Error: student not found\n");
}
void modify_student(char* name, int math_score, int chinese_score, int english_score) {
for (int i = 0; i < student_num; i++) {
if (strcmp(students[i].name, name) == 0) {
students[i].math_score = math_score;
students[i].chinese_score = chinese_score;
students[i].english_score = english_score;
return;
}
}
printf("Error: student not found\n");
}
void search_student_by_name(char* name) {
for (int i = 0; i < student_num; i++) {
if (strcmp(students[i].name, name) == 0) {
print_student(students[i]);
return;
}
}
printf("Error: student not found\n");
}
int main() {
struct Student student1 = {"001", "Tom", 80, 90, 85};
struct Student student2 = {"002", "Jerry", 70, 85, 90};
insert_student(student1);
insert_student(student2);
print_all_students();
delete_student_by_id("001");
print_all_students();
delete_student_by_name("Jerry");
print_all_students();
modify_student("Tom", 90, 95, 80);
print_all_students();
search_student_by_name("Tom");
return 0;
}
```
这个程序定义了一个结构体`Student`表示学生成绩,包含学生学号、姓名、数学成绩、语文成绩、英语成绩等信息。然后定义了一个数组`students`,存储多个学生的成绩信息。实现了查找可以遍历数组,根据姓名查找对应学生的成绩信息。插入可以在数组末尾添加一个新的学生成绩信息。删除可以根据学号或姓名查找到对应学生的成绩信息,然后将其从数组中删除。修改可以根据姓名查找到对应学生的成绩信息,然后修改其数学成绩、语文成绩、英语成绩等信息。
阅读全文