基于C语言课程成绩统计系统程序源代码

课程成绩统计系统
Course Achievement Statistics System
某班有最多不超过 30人(具体人数由键盘输入)参加某门课程的期末考试。
There are no more than 30 people (specific number entered by keyboard) in a class to take the final examination.
(1)录入每个学生 名和各科考试成绩
Enter the stude name and examination results of each student
(2)计算每门课程的总分和平均分
Calculate the total score and average score of each course
(3) 按优秀(90~100)、良好(80~89)、中等(70~79)、及格(60~69)、不及格
(0~59)5个类别,对每门课程分别统计每个类别的人数以及所占的百分比
According to the five cateaories of excellent (90 ~ 100), good (80 ~ 89),
medium (70 ~79), pass (60 ~69) and fail (0 ~ 59), the number and percentage of each category are counted for each course respectively
(4) 输出每个学生的学号、姓名、各科考试成绩,以及每门课程的总分和平均分
Output the student number, name, examination results of each subject of each student,as well as the total score and average score of each course.
要求程序运行后先显示如下菜单,并提示用户输入选项:After the program is run, the following menu is required to be displayed first, and the user is prompted with input options. 1. Input record 2. Caculate total and average score of every course 3. Statisitic analysis for every course 4.List record 0.Exit Please enter your choice

源代码

#include <stdio.h>
#include <stdlib.h>

typedef struct {
    int id;
    char name[20];
    float score[3]; // 保存三门课的成绩
    float total;
    float avg;
} Student;

void calcCourse(int courseIndex, Student students[], int studentCount);
void statisticAnalysis(int courseIndex, Student students[], int studentCount);
void listRecord(Student students[], int studentCount);

int main() {
    int choice = -1;
    int studentCount = 0;

    printf("Welcome to Course Achievement Statistics System\n");
    printf("***********************************************\n");

    printf("Please input the number of students (no more than 30): ");
    scanf("%d", &studentCount);
    Student* students = (Student*)malloc(studentCount * sizeof(Student));

    while (choice != 0) {
        printf("\nMenu:\n");
        printf("1. Input record\n");
        printf("2. Calculate total and average score of every course\n");
        printf("3. Statistic analysis for every course\n");
        printf("4. List record\n");
        printf("0. Exit\n");
        printf("Please enter your choice: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                // 录入每个学生名字和各科考试成绩
                printf("Enter the name and examination results of each student:\n");
                for (int i = 0; i < studentCount; i++) {
                    printf("Student %d:\n", i+1);
                    printf("Name: ");
                    scanf("%s", students[i].name);
                    for (int j = 0; j < 3; j++) {
                        printf("Score %d: ", j+1);
                        scanf("%f", &students[i].score[j]);
                    }
                }
                break;
            case 2:
                // 计算每门课程的总分和平均分
                for (int i = 0; i < 3; i++) {
                    calcCourse(i, students, studentCount);
                }
                printf("Calculation completed\n");
                break;
            case 3:
                // 对每门课程进行统计分析
                for (int i = 0; i < 3; i++) {
                    statisticAnalysis(i, students, studentCount);
                }
                break;
            case 4:
                // 输出每个学生的学号、姓名、各科考试成绩,以及每门课程的总分和平均分
                listRecord(students, studentCount);
                break;
            case 0:
                printf("Thank you for using Course Achievement Statistics System\n");
                break;
            default:
                printf("Invalid choice, please enter again\n");
                break;
        }
    }

    free(students);
    return 0;
}

void calcCourse(int courseIndex, Student students[], int studentCount) {
    float total = 0;
    for (int i = 0; i < studentCount; i++) {
        total += students[i].score[courseIndex];
    }
    float avg = total / studentCount;

    for (int i = 0; i < studentCount; i++) {
        students[i].total += students[i].score[courseIndex];
        students[i].avg = students[i].total / 3;
    }
}

void statisticAnalysis(int courseIndex, Student students[], int studentCount) {
    int excellent = 0;
    int good = 0;
    int medium = 0;
    int pass = 0;
    int fail = 0;

    for (int i = 0; i < studentCount; i++) {
        float score = students[i].score[courseIndex];
        if (score >= 90) {
            excellent++;
        } else if (score >= 80) {
            good++;
        } else if (score >= 70) {
            medium++;
        } else if (score >= 60) {
            pass++;
        } else {
            fail++;
        }
    }

    printf("Course %d Statistic Analysis:\n", courseIndex+1);
    printf("Excellent: %d, Percentage: %.2f%%\n", excellent, (float)excellent / studentCount * 100);
    printf("Good: %d, Percentage: %.2f%%\n", good, (float)good / studentCount * 100);
    printf("Medium: %d, Percentage: %.2f%%\n", medium, (float)medium / studentCount * 100);
    printf("Pass: %d, Percentage: %.2f%%\n", pass, (float)pass / studentCount * 100);
    printf("Fail: %d, Percentage: %.2f%%\n", fail, (float)fail / studentCount * 100);
}

void listRecord(Student students[], int studentCount) {
    printf("Student Record:\n");
    for (int i = 0; i < studentCount; i++) {
        Student student = students[i];
        printf("====================\n");
        printf("Student %d:\n", i+1);
        printf("ID: %d\n", student.id);
        printf("Name: %s\n", student.name);
        for (int j = 0; j < 3; j++) {
            printf("Score %d: %.2f\n", j+1, student.score[j]);
        }
        printf("Total Score: %.2f\n", student.total);
        printf("Average Score: %.2f\n", student.avg);
    }
}

这个代码通过菜单的选项来实现录入学生信息、计算总分和平均分、统计分析以及输出每个学生的记录。你可以根据需要扩展这个代码或者调整细节来满足具体的要求。

© 版权声明
THE END
喜欢就支持一下吧
点赞15赞赏 分享