只运用数组编写一个学生成绩管理系统,可运用于C-Free5
时间: 2024-01-14 10:02:55 浏览: 77
好的,以下是一个简单的学生成绩管理系统的代码,使用数组来存储学生信息和成绩:
```
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
struct student {
char name[20];
int id;
float score;
};
void print_menu();
void add_student(struct student students[], int* size);
void delete_student(struct student students[], int* size);
void modify_student(struct student students[], int size);
void print_all_students(struct student students[], int size);
void search_student(struct student students[], int size);
int main() {
struct student students[MAX_SIZE];
int size = 0;
int choice;
do {
print_menu();
scanf("%d", &choice);
switch (choice) {
case 1:
add_student(students, &size);
break;
case 2:
delete_student(students, &size);
break;
case 3:
modify_student(students, size);
break;
case 4:
print_all_students(students, size);
break;
case 5:
search_student(students, size);
break;
case 6:
printf("Exiting program...\n");
break;
default:
printf("Invalid choice. Please try again.\n");
break;
}
} while (choice != 6);
return 0;
}
void print_menu() {
printf("\n--- Student Grade Management System Menu ---\n");
printf("1. Add a new student\n");
printf("2. Delete a student\n");
printf("3. Modify a student's information\n");
printf("4. Print all students' information\n");
printf("5. Search for a student\n");
printf("6. Exit program\n");
printf("Enter your choice: ");
}
void add_student(struct student students[], int* size) {
if (*size >= MAX_SIZE) {
printf("The student database is full. Cannot add any more students.\n");
return;
}
printf("Enter the name of the student: ");
scanf("%s", students[*size].name);
printf("Enter the ID number of the student: ");
scanf("%d", &students[*size].id);
printf("Enter the score of the student: ");
scanf("%f", &students[*size].score);
(*size)++;
printf("Student added successfully.\n");
}
void delete_student(struct student students[], int* size) {
int id, i, j;
printf("Enter the ID number of the student you want to delete: ");
scanf("%d", &id);
for (i = 0; i < *size; i++) {
if (students[i].id == id) {
for (j = i; j < *size - 1; j++) {
students[j] = students[j + 1];
}
(*size)--;
printf("Student deleted successfully.\n");
return;
}
}
printf("Student with ID number %d not found.\n", id);
}
void modify_student(struct student students[], int size) {
int id, i;
printf("Enter the ID number of the student you want to modify: ");
scanf("%d", &id);
for (i = 0; i < size; i++) {
if (students[i].id == id) {
printf("Enter the new name for the student: ");
scanf("%s", students[i].name);
printf("Enter the new score for the student: ");
scanf("%f", &students[i].score);
printf("Student information modified successfully.\n");
return;
}
}
printf("Student with ID number %d not found.\n", id);
}
void print_all_students(struct student students[], int size) {
int i;
if (size == 0) {
printf("No students in the database.\n");
return;
}
printf("%-20s%-10s%-10s\n", "Name", "ID", "Score");
for (i = 0; i < size; i++) {
printf("%-20s%-10d%-10.2f\n", students[i].name, students[i].id, students[i].score);
}
}
void search_student(struct student students[], int size) {
int id, i;
printf("Enter the ID number of the student you want to search for: ");
scanf("%d", &id);
for (i = 0; i < size; i++) {
if (students[i].id == id) {
printf("%-20s%-10s%-10s\n", "Name", "ID", "Score");
printf("%-20s%-10d%-10.2f\n", students[i].name, students[i].id, students[i].score);
return;
}
}
printf("Student with ID number %d not found.\n", id);
}
```
该程序使用结构体数组来存储学生信息,有以下功能:
1. 添加学生
2. 删除学生
3. 修改学生信息
4. 打印所有学生信息
5. 搜索学生信息
6. 退出程序
在主函数中,使用一个循环不断显示菜单并等待用户输入选择。根据用户的选择,调用相应的函数来执行相应的操作。这些函数的实现都比较简单,使用了数组来存储学生信息和成绩,通过循环来遍历数组找到对应的学生,然后进行操作。注意,由于数组的大小是有限的,因此在添加学生时需要检查数组是否已满。
阅读全文