数据结构学生成绩管理系统c语言代码
时间: 2023-07-25 20:31:44 浏览: 96
好的,下面是一个简单的数据结构学生成绩管理系统的C语言代码,供您参考:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 20
#define MAX_STU_NUM 100
typedef struct student {
char name[MAX_NAME_LEN];
int id;
int grade;
} Student;
void add_student(Student students[], int *num_students);
void delete_student(Student students[], int *num_students);
void search_student(Student students[], int num_students);
void print_all_students(Student students[], int num_students);
int main() {
Student students[MAX_STU_NUM];
int num_students = 0;
int option;
while (1) {
printf("1. Add student\n");
printf("2. Delete student\n");
printf("3. Search student\n");
printf("4. Print all students\n");
printf("5. Exit\n");
printf("Enter option: ");
scanf("%d", &option);
switch (option) {
case 1:
add_student(students, &num_students);
break;
case 2:
delete_student(students, &num_students);
break;
case 3:
search_student(students, num_students);
break;
case 4:
print_all_students(students, num_students);
break;
case 5:
exit(0);
default:
printf("Invalid option.\n");
break;
}
}
return 0;
}
void add_student(Student students[], int *num_students) {
if (*num_students == MAX_STU_NUM) {
printf("The student database is full.\n");
return;
}
printf("Enter student name: ");
scanf("%s", students[*num_students].name);
printf("Enter student ID: ");
scanf("%d", &students[*num_students].id);
printf("Enter student grade: ");
scanf("%d", &students[*num_students].grade);
printf("Student added successfully.\n");
(*num_students)++;
}
void delete_student(Student students[], int *num_students) {
int id, i, j;
printf("Enter student ID to delete: ");
scanf("%d", &id);
for (i = 0; i < *num_students; i++) {
if (students[i].id == id) {
for (j = i; j < *num_students - 1; j++) {
strcpy(students[j].name, students[j + 1].name);
students[j].id = students[j + 1].id;
students[j].grade = students[j + 1].grade;
}
printf("Student deleted successfully.\n");
(*num_students)--;
return;
}
}
printf("Student not found.\n");
}
void search_student(Student students[], int num_students) {
int id, i;
printf("Enter student ID to search: ");
scanf("%d", &id);
for (i = 0; i < num_students; i++) {
if (students[i].id == id) {
printf("Name: %s\n", students[i].name);
printf("ID: %d\n", students[i].id);
printf("Grade: %d\n", students[i].grade);
return;
}
}
printf("Student not found.\n");
}
void print_all_students(Student students[], int num_students) {
int i;
if (num_students == 0) {
printf("No students found.\n");
return;
}
printf("Name\tID\tGrade\n");
for (i = 0; i < num_students; i++) {
printf("%s\t%d\t%d\n", students[i].name, students[i].id, students[i].grade);
}
}
```
这段代码实现了一个简单的学生成绩管理系统,包括添加学生、删除学生、搜索学生和打印所有学生信息等功能。在代码中,使用了一个结构体 `Student` 表示每个学生的信息,包括姓名、学号和成绩。同时,使用一个数组 `students[]` 存储所有学生的信息,使用一个变量 `num_students` 表示当前有多少个学生信息已经被存储。
阅读全文