帮我写一段C语言的学生成绩管理系统
时间: 2023-08-28 12:21:24 浏览: 134
好的,以下是一个简单的C语言学生成绩管理系统的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_STUDENTS 100
typedef struct {
int id;
char name[20];
float score;
} Student;
int main() {
Student students[MAX_STUDENTS];
int count = 0;
while (1) {
printf("Welcome to the student score management system!\n");
printf("1. Add a student\n");
printf("2. Show all students\n");
printf("3. Search a student\n");
printf("4. Modify a student\n");
printf("5. Delete a student\n");
printf("6. Exit\n");
printf("Please enter your choice (1-6): ");
int choice;
scanf("%d", &choice);
switch (choice) {
case 1: // Add a student
if (count >= MAX_STUDENTS) {
printf("The system can only store up to %d students.\n", MAX_STUDENTS);
break;
}
printf("Please enter the student's ID: ");
scanf("%d", &students[count].id);
printf("Please enter the student's name: ");
scanf("%s", students[count].name);
printf("Please enter the student's score: ");
scanf("%f", &students[count].score);
count++;
break;
case 2: // Show all students
if (count == 0) {
printf("There are no students in the system.\n");
break;
}
printf("ID\tName\tScore\n");
for (int i = 0; i < count; i++) {
printf("%d\t%s\t%.2f\n", students[i].id, students[i].name, students[i].score);
}
break;
case 3: // Search a student
if (count == 0) {
printf("There are no students in the system.\n");
break;
}
printf("Please enter the student's ID: ");
int search_id;
scanf("%d", &search_id);
for (int i = 0; i < count; i++) {
if (students[i].id == search_id) {
printf("ID\tName\tScore\n");
printf("%d\t%s\t%.2f\n", students[i].id, students[i].name, students[i].score);
break;
}
if (i == count - 1) {
printf("The student with ID %d is not found.\n", search_id);
}
}
break;
case 4: // Modify a student
if (count == 0) {
printf("There are no students in the system.\n");
break;
}
printf("Please enter the student's ID: ");
int modify_id;
scanf("%d", &modify_id);
for (int i = 0; i < count; i++) {
if (students[i].id == modify_id) {
printf("Please enter the new score: ");
scanf("%f", &students[i].score);
printf("The score is modified successfully.\n");
break;
}
if (i == count - 1) {
printf("The student with ID %d is not found.\n", modify_id);
}
}
break;
case 5: // Delete a student
if (count == 0) {
printf("There are no students in the system.\n");
break;
}
printf("Please enter the student's ID: ");
int delete_id;
scanf("%d", &delete_id);
for (int i = 0; i < count; i++) {
if (students[i].id == delete_id) {
for (int j = i; j < count - 1; j++) {
students[j] = students[j + 1];
}
count--;
printf("The student is deleted successfully.\n");
break;
}
if (i == count - 1) {
printf("The student with ID %d is not found.\n", delete_id);
}
}
break;
case 6: // Exit
printf("Thank you for using the student score management system!\n");
exit(0);
default:
printf("Invalid choice.\n");
break;
}
printf("\n");
}
return 0;
}
```
该程序使用了结构体存储学生信息,并提供了添加、显示、搜索、修改和删除学生信息的功能。
阅读全文