C++之学生成绩管理系统
时间: 2023-06-10 20:06:27 浏览: 122
学生成绩管理系统可以用 C 语言来实现。以下是一个简单的学生成绩管理系统的代码示例,包括添加学生信息、添加课程信息、添加学生成绩、计算学生平均分数和输出学生成绩单等功能。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENT_NUM 1000
#define MAX_COURSE_NUM 10
#define COURSE_NAME_LEN 20
// 学生信息结构体
typedef struct {
char name[20];
int id;
} Student;
// 课程信息结构体
typedef struct {
char name[COURSE_NAME_LEN];
int credit;
} Course;
// 学生成绩结构体
typedef struct {
int student_id;
int course_id;
float score;
} Grade;
// 学生数组
Student students[MAX_STUDENT_NUM];
int student_count = 0;
// 课程数组
Course courses[MAX_COURSE_NUM];
int course_count = 0;
// 成绩数组
Grade grades[MAX_STUDENT_NUM * MAX_COURSE_NUM];
int grade_count = 0;
// 添加学生信息
void add_student() {
if (student_count >= MAX_STUDENT_NUM) {
printf("Max student number reached!\n");
return;
}
printf("Enter student name: ");
scanf("%s", students[student_count].name);
printf("Enter student id: ");
scanf("%d", &students[student_count].id);
student_count++;
}
// 添加课程信息
void add_course() {
if (course_count >= MAX_COURSE_NUM) {
printf("Max course number reached!\n");
return;
}
printf("Enter course name: ");
scanf("%s", courses[course_count].name);
printf("Enter course credit: ");
scanf("%d", &courses[course_count].credit);
course_count++;
}
// 添加学生成绩
void add_grade() {
int student_id, course_id;
float score;
printf("Enter student id: ");
scanf("%d", &student_id);
printf("Enter course id: ");
scanf("%d", &course_id);
if (student_id < 1 || student_id > student_count) {
printf("Invalid student id!\n");
return;
}
if (course_id < 1 || course_id > course_count) {
printf("Invalid course id!\n");
return;
}
printf("Enter score: ");
scanf("%f", &score);
grades[grade_count].student_id = student_id;
grades[grade_count].course_id = course_id;
grades[grade_count].score = score;
grade_count++;
}
// 计算学生平均分数
float calculate_avg_score(int student_id) {
float sum = 0;
int count = 0;
for (int i = 0; i < grade_count; i++) {
if (grades[i].student_id == student_id) {
sum += grades[i].score;
count++;
}
}
if (count == 0) {
return 0;
} else {
return sum / count;
}
}
// 输出学生成绩单
void print_grade_report() {
printf("Student\tCourse\tScore\n");
for (int i = 0; i < grade_count; i++) {
printf("%s\t%s\t%.2f\n", students[grades[i].student_id - 1].name, courses[grades[i].course_id - 1].name, grades[i].score);
}
}
int main() {
int choice;
while(1) {
printf("\nStudent Grade Management System\n");
printf("1. Add student\n");
printf("2. Add course\n");
printf("3. Add grade\n");
printf("4. Print grade report\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
add_student();
break;
case 2:
add_course();
break;
case 3:
add_grade();
break;
case 4:
print_grade_report();
break;
case 5:
exit(0);
default:
printf("Invalid choice!\n");
break;
}
}
return 0;
}
```
这段代码使用了结构体来存储学生、课程和成绩信息,并使用数组来存储多个学生、课程和成绩。通过添加、计算和输出等基本操作,实现了一个简单的学生成绩管理系统。
阅读全文