请用C语言做一份学生成绩管理系统
时间: 2023-11-16 11:05:45 浏览: 57
好的,以下是一份简单的学生成绩管理系统的C语言代码:
```c
#include <stdio.h>
#include <string.h>
#define MAX_STUDENTS 100
#define MAX_NAME_LEN 50
struct student {
char name[MAX_NAME_LEN];
int id;
float score;
};
void add_student(struct student[], int *);
void display_students(struct student[], int);
void search_student(struct student[], int);
void update_student(struct student[], int);
void delete_student(struct student[], int *);
int main() {
struct student students[MAX_STUDENTS];
int num_students = 0;
int choice;
do {
printf("\n1. Add student\n");
printf("2. Display students\n");
printf("3. Search student\n");
printf("4. Update student\n");
printf("5. Delete student\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
add_student(students, &num_students);
break;
case 2:
display_students(students, num_students);
break;
case 3:
search_student(students, num_students);
break;
case 4:
update_student(students, num_students);
break;
case 5:
delete_student(students, &num_students);
break;
case 6:
printf("Exiting...\n");
break;
default:
printf("Invalid choice. Try again.\n");
}
} while(choice != 6);
return 0;
}
void add_student(struct student students[], int *num_students) {
if (*num_students == MAX_STUDENTS) {
printf("Maximum number of students reached.\n");
return;
}
struct student new_student;
printf("Enter name: ");
scanf("%s", new_student.name);
printf("Enter ID: ");
scanf("%d", &new_student.id);
printf("Enter score: ");
scanf("%f", &new_student.score);
students[*num_students] = new_student;
(*num_students)++;
printf("Student added.\n");
}
void display_students(struct student students[], int num_students) {
if (num_students == 0) {
printf("No students to display.\n");
return;
}
printf("Name\tID\tScore\n");
for (int i = 0; i < num_students; i++) {
printf("%s\t%d\t%.2f\n", students[i].name, students[i].id, students[i].score);
}
}
void search_student(struct student students[], int num_students) {
if (num_students == 0) {
printf("No students to search.\n");
return;
}
int search_id;
printf("Enter student ID to search: ");
scanf("%d", &search_id);
for (int i = 0; i < num_students; i++) {
if (students[i].id == search_id) {
printf("Name: %s\nID: %d\nScore: %.2f\n", students[i].name, students[i].id, students[i].score);
return;
}
}
printf("Student not found.\n");
}
void update_student(struct student students[], int num_students) {
if (num_students == 0) {
printf("No students to update.\n");
return;
}
int update_id;
printf("Enter student ID to update: ");
scanf("%d", &update_id);
for (int i = 0; i < num_students; i++) {
if (students[i].id == update_id) {
printf("Enter new name (or press enter to keep the same): ");
char new_name[MAX_NAME_LEN];
scanf("%s", new_name);
if (strlen(new_name) > 0) {
strcpy(students[i].name, new_name);
}
printf("Enter new score (or press enter to keep the same): ");
char new_score_str[10];
scanf("%s", new_score_str);
if (strlen(new_score_str) > 0) {
float new_score = atof(new_score_str);
students[i].score = new_score;
}
printf("Student updated.\n");
return;
}
}
printf("Student not found.\n");
}
void delete_student(struct student students[], int *num_students) {
if (*num_students == 0) {
printf("No students to delete.\n");
return;
}
int delete_id;
printf("Enter student ID to delete: ");
scanf("%d", &delete_id);
for (int i = 0; i < *num_students; i++) {
if (students[i].id == delete_id) {
for (int j = i; j < (*num_students) - 1; j++) {
students[j] = students[j+1];
}
(*num_students)--;
printf("Student deleted.\n");
return;
}
}
printf("Student not found.\n");
}
```
这份代码实现了以下功能:
- 添加学生
- 显示所有学生
- 查找学生
- 更新学生信息
- 删除学生信息
您可以根据需要进行修改和扩展。
阅读全文