学生成绩管理系统C语言代码
时间: 2023-10-13 17:04:50 浏览: 40
以下是一个简单的学生成绩管理系统的C语言代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
typedef struct {
char name[50];
int id;
int score;
} Student;
void add_student(Student students[], int *num_students);
void display_students(Student students[], int num_students);
void save_students(Student students[], int num_students);
int main() {
Student students[MAX_STUDENTS];
int num_students = 0;
char choice;
while (1) {
printf("Welcome to the student score management system!\n");
printf("1. Add a new student\n");
printf("2. Display all students\n");
printf("3. Save students to file\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf(" %c", &choice);
switch (choice) {
case '1':
add_student(students, &num_students);
break;
case '2':
display_students(students, num_students);
break;
case '3':
save_students(students, num_students);
break;
case '4':
printf("Goodbye!\n");
exit(0);
default:
printf("Invalid choice. Please try again.\n");
break;
}
}
return 0;
}
void add_student(Student students[], int *num_students) {
if (*num_students == MAX_STUDENTS) {
printf("Sorry, the maximum number of students has been reached.\n");
return;
}
Student new_student;
printf("Enter the student's name: ");
scanf("%s", new_student.name);
printf("Enter the student's ID: ");
scanf("%d", &new_student.id);
printf("Enter the student's score: ");
scanf("%d", &new_student.score);
students[*num_students] = new_student;
(*num_students)++;
printf("Student added successfully.\n");
}
void display_students(Student students[], int num_students) {
if (num_students == 0) {
printf("There are no students to display.\n");
return;
}
printf("%-20s %-10s %-10s\n", "Name", "ID", "Score");
for (int i = 0; i < num_students; i++) {
printf("%-20s %-10d %-10d\n", students[i].name, students[i].id, students[i].score);
}
}
void save_students(Student students[], int num_students) {
if (num_students == 0) {
printf("There are no students to save.\n");
return;
}
FILE *file = fopen("students.txt", "w");
if (file == NULL) {
printf("Error opening file. Please try again.\n");
return;
}
for (int i = 0; i < num_students; i++) {
fprintf(file, "%s %d %d\n", students[i].name, students[i].id, students[i].score);
}
fclose(file);
printf("Students saved to file successfully.\n");
}
```
这个程序提供了添加学生、显示所有学生和保存学生信息到文件的功能。它使用结构体来存储每个学生的姓名、ID和分数,并使用指针来跟踪学生数量。注意,此程序未包含任何错误检查或输入验证。在实际应用中,您可能需要添加更多的错误处理和安全措施。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)