请写出学生成绩管理系统的c语言代码
时间: 2023-10-22 20:09:48 浏览: 62
学生成绩管理系统源代码
以下是一个简单的学生成绩管理系统的 C 语言代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
typedef struct {
char name[50];
int id;
float grade;
} Student;
void add_student(Student students[], int *num_students) {
if (*num_students >= MAX_STUDENTS) {
printf("Error: Maximum number of students reached.\n");
return;
}
Student s;
printf("Enter name: ");
scanf("%s", s.name);
printf("Enter ID: ");
scanf("%d", &s.id);
printf("Enter grade: ");
scanf("%f", &s.grade);
students[*num_students] = s;
(*num_students)++;
printf("Student added successfully.\n");
}
void remove_student(Student students[], int *num_students) {
int id;
printf("Enter ID of student to remove: ");
scanf("%d", &id);
for (int i = 0; i < *num_students; i++) {
if (students[i].id == id) {
for (int j = i; j < *num_students - 1; j++) {
students[j] = students[j + 1];
}
(*num_students)--;
printf("Student removed successfully.\n");
return;
}
}
printf("Error: Student with ID %d not found.\n", id);
}
void print_students(Student students[], int num_students) {
printf("Name\tID\tGrade\n");
for (int i = 0; i < num_students; i++) {
printf("%s\t%d\t%.2f\n", students[i].name, students[i].id, students[i].grade);
}
}
int main() {
Student students[MAX_STUDENTS];
int num_students = 0;
int choice;
while (1) {
printf("\nStudent Grade Management System\n");
printf("1. Add Student\n");
printf("2. Remove Student\n");
printf("3. Print Students\n");
printf("4. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_student(students, &num_students);
break;
case 2:
remove_student(students, &num_students);
break;
case 3:
print_students(students, num_students);
break;
case 4:
printf("Exiting program.\n");
exit(0);
default:
printf("Invalid choice.\n");
}
}
return 0;
}
```
该代码中定义了一个 `Student` 结构体,该结构体包含学生的姓名、ID 和成绩。然后,定义了三个函数来实现添加学生、删除学生和打印学生列表的功能。最后,在 `main()` 函数中实现了一个简单的菜单,让用户可以选择要执行的操作。
阅读全文