用c语言写个成绩管理系统
时间: 2023-09-10 20:08:06 浏览: 59
用c语言编写成绩管理系统
好的,我可以为您提供一个简单的成绩管理系统的代码示例,具体实现如下:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 20
#define MAX_GRADE_NUM 5
struct student {
char name[MAX_NAME_LEN];
int grades[MAX_GRADE_NUM];
int grade_num;
float average;
};
void add_student(struct student *students, int *num_students);
void print_student(struct student *students, int student_index);
void print_all_students(struct student *students, int num_students);
int main(void) {
struct student students[100];
int num_students = 0;
char input;
while (1) {
printf("Enter 'a' to add a student, 'p' to print a student, 'l' to print all students, or 'q' to quit: ");
scanf(" %c", &input);
if (input == 'a') {
add_student(students, &num_students);
} else if (input == 'p') {
int student_index;
printf("Enter the index of the student to print: ");
scanf("%d", &student_index);
print_student(students, student_index);
} else if (input == 'l') {
print_all_students(students, num_students);
} else if (input == 'q') {
break;
} else {
printf("Invalid input\n");
}
}
return 0;
}
void add_student(struct student *students, int *num_students) {
if (*num_students >= 100) {
printf("Cannot add more students\n");
return;
}
struct student new_student;
printf("Enter the name of the student: ");
scanf("%s", new_student.name);
printf("Enter the number of grades for the student: ");
scanf("%d", &new_student.grade_num);
for (int i = 0; i < new_student.grade_num; i++) {
printf("Enter grade %d: ", i+1);
scanf("%d", &new_student.grades[i]);
}
float sum = 0;
for (int i = 0; i < new_student.grade_num; i++) {
sum += new_student.grades[i];
}
new_student.average = sum / new_student.grade_num;
students[*num_students] = new_student;
(*num_students)++;
}
void print_student(struct student *students, int student_index) {
if (student_index < 0 || student_index >= 100 || student_index >= MAX_NAME_LEN) {
printf("Invalid index\n");
return;
}
struct student student = students[student_index];
printf("Name: %s\n", student.name);
printf("Grades: ");
for (int i = 0; i < student.grade_num; i++) {
printf("%d ", student.grades[i]);
}
printf("\n");
printf("Average: %.2f\n", student.average);
}
void print_all_students(struct student *students, int num_students) {
for (int i = 0; i < num_students; i++) {
printf("Student %d:\n", i+1);
print_student(students, i);
}
}
```
这个成绩管理系统可以添加学生、打印单个学生、打印全部学生信息。其中,每个学生有一个姓名、一组成绩和成绩数量,可以计算出平均分。
阅读全文