主要内容:该系统实现对学生成绩的批量录入、修改、查询、浏览等功能。具体要求如下: 学生信息包括:学号、姓名、入学年月、4门课的成绩等8个成员,试设计学生成绩管理系统,使之能提供以下功能: 1、信息录入:录入每一位学生信息(包括学号、姓名、入学年月和4门课的成绩); 2、信息浏览:输出系统内所有学生的信息; 3、成绩修改:输入学号,修改该生某一门课的成绩; 4、分类查询:按学号或姓名查找学生,输出学生的成绩单; 5、统计学生总成绩:计算每个学生的总分及平均分,并根据学生的平均成绩进行排序;6、统计每门课程的成绩:计算每一门课程学生的及格率、优秀率等。的c语言代码
时间: 2023-12-16 20:27:19 浏览: 115
以下是一个简单的C语言代码示例,实现了上述要求:
```c
#include <stdio.h>
#include <string.h>
#define MAX_STUDENTS 100
#define MAX_NAME_LEN 50
typedef struct {
int id;
char name[MAX_NAME_LEN];
char enroll_date[20];
int scores[4];
int total_score;
float avg_score;
} Student;
Student students[MAX_STUDENTS];
int num_students = 0;
void add_student() {
if (num_students >= MAX_STUDENTS) {
printf("Too many students\n");
return;
}
Student *s = &students[num_students];
printf("Enter student ID: ");
scanf("%d", &s->id);
printf("Enter student name: ");
scanf("%s", s->name);
printf("Enter enrollment date: ");
scanf("%s", s->enroll_date);
printf("Enter scores for 4 courses: ");
for (int i = 0; i < 4; i++) {
scanf("%d", &s->scores[i]);
s->total_score += s->scores[i];
}
s->avg_score = (float)s->total_score / 4.0;
num_students++;
}
void view_students() {
printf("ID\tName\tEnroll Date\tCourse 1\tCourse 2\tCourse 3\tCourse 4\tTotal Score\tAvg Score\n");
for (int i = 0; i < num_students; i++) {
Student *s = &students[i];
printf("%d\t%s\t%s\t", s->id, s->name, s->enroll_date);
for (int j = 0; j < 4; j++) {
printf("%d\t\t", s->scores[j]);
}
printf("%d\t\t%.2f\n", s->total_score, s->avg_score);
}
}
void update_score() {
int id, course, score;
printf("Enter student ID: ");
scanf("%d", &id);
printf("Enter course number (1-4): ");
scanf("%d", &course);
printf("Enter new score: ");
scanf("%d", &score);
for (int i = 0; i < num_students; i++) {
if (students[i].id == id) {
students[i].scores[course-1] = score;
students[i].total_score = 0;
for (int j = 0; j < 4; j++) {
students[i].total_score += students[i].scores[j];
}
students[i].avg_score = (float)students[i].total_score / 4.0;
printf("Score updated\n");
return;
}
}
printf("Student not found\n");
}
void search_students() {
int choice;
printf("Search by:\n1. ID\n2. Name\n");
scanf("%d", &choice);
if (choice == 1) {
int id;
printf("Enter student ID: ");
scanf("%d", &id);
for (int i = 0; i < num_students; i++) {
if (students[i].id == id) {
Student *s = &students[i];
printf("Name: %s\nEnroll Date: %s\nCourse 1: %d\nCourse 2: %d\nCourse 3: %d\nCourse 4: %d\nTotal Score: %d\nAvg Score: %.2f\n", s->name, s->enroll_date, s->scores[0], s->scores[1], s->scores[2], s->scores[3], s->total_score, s->avg_score);
return;
}
}
printf("Student not found\n");
} else if (choice == 2) {
char name[MAX_NAME_LEN];
printf("Enter student name: ");
scanf("%s", name);
for (int i = 0; i < num_students; i++) {
if (strcmp(students[i].name, name) == 0) {
Student *s = &students[i];
printf("ID: %d\nEnroll Date: %s\nCourse 1: %d\nCourse 2: %d\nCourse 3: %d\nCourse 4: %d\nTotal Score: %d\nAvg Score: %.2f\n", s->id, s->enroll_date, s->scores[0], s->scores[1], s->scores[2], s->scores[3], s->total_score, s->avg_score);
return;
}
}
printf("Student not found\n");
}
}
void sort_students() {
for (int i = 0; i < num_students; i++) {
int max_idx = i;
for (int j = i+1; j < num_students; j++) {
if (students[j].avg_score > students[max_idx].avg_score) {
max_idx = j;
}
}
if (max_idx != i) {
Student temp = students[max_idx];
students[max_idx] = students[i];
students[i] = temp;
}
}
printf("Students sorted by average score:\n");
view_students();
}
void stats_by_course() {
for (int i = 0; i < 4; i++) {
int pass_count = 0;
int excellent_count = 0;
for (int j = 0; j < num_students; j++) {
if (students[j].scores[i] >= 60) {
pass_count++;
}
if (students[j].scores[i] >= 90) {
excellent_count++;
}
}
printf("Course %d:\nPass Rate: %.2f%%\nExcellent Rate: %.2f%%\n", i+1, (float)pass_count / num_students * 100.0, (float)excellent_count / num_students * 100.0);
}
}
int main() {
int choice;
while (1) {
printf("Select an option:\n1. Add student\n2. View students\n3. Update score\n4. Search students\n5. Sort students by average score\n6. View stats by course\n7. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
add_student();
break;
case 2:
view_students();
break;
case 3:
update_score();
break;
case 4:
search_students();
break;
case 5:
sort_students();
break;
case 6:
stats_by_course();
break;
case 7:
return 0;
default:
printf("Invalid choice\n");
break;
}
}
return 0;
}
```
请注意,这只是一个基本的实现,可能需要根据具体需求进行修改和改进。
阅读全文