IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> 学生成绩管理程序的设计与实现 C语言 -> 正文阅读

[C++知识库]学生成绩管理程序的设计与实现 C语言

学生成绩管理程序的设计与实现
(1)对学生的姓名、各科成绩进行输入和修改;
(2)老师可以对成绩按各种条件进行查询、统计、排名;
(3)以班为单位打印成绩单;
(4)学生对自己的成绩和排名进行查询;
(5)将(2)-(4)的结果写入文件中。

#include <stdio.h>
#define bool int
#define true 1
#define false 0
#define N 30 /* 程序允许的最大学生数 */
#define M 3  /* 课程的数量,只考虑 C、Math、English 这3门课 */

typedef struct student
{
    long studentID;       /* 学号 */
    char studentName[10]; /* 姓名 */
    char className[20];   /* 班级 */
    int score[M];         /* M门课程的分数 */
    float averageScore;   /* 平均分 */
} STUDENT;
STUDENT stu[N]; /* 存储学生信息的结构体数组 */
int n = 0;      /* stu存储的学生的数量 */

const char *scoreFileName = "stu.txt";            /* 存储stu的文件名 */
const char *sortedScoreFileName = "sorted.txt";   /* 排序后的学生成绩 */
const char *classScoreFileName = "class.txt";     /* 某一班级的成绩单 */
const char *studentScoreFileName = "student.txt"; /* 某位学生的成绩单 */

char course[M][10] = {"C", "Math", "English"};
int courseIndex = 0; /* 当前选中的课程编号,取值范围是 0,...,M-1 */

typedef bool (*COMPARE)(STUDENT, STUDENT);
void PrintMenu();                                 /* 在控制台中打印菜单 */
void PrintDataFormatTips(bool ignoreStudentID);   /* 在控制台中打印提示语,描述数据输入的格式 */
void InputScore();                                /* 输入数据,包括学号、姓名、班级、以及每门课程的成绩 */
void CalculateAverageScore();                     /* 为每位学生计算平均分 */
void ModifyScore();                               /* 修改成绩 */
bool WriteToFile();                               /* 将stu中保存的数据写入到文件中 */
bool ReadFromFile();                              /* 从文件中读数据,保存到stu中 */
int FindStudentByID(long ID);                     /* 根据学号在stu中查找学生,若找得到则返回该学生在stu中的下标,否则返回-1 */
void Sort(COMPARE compare);                       /* 按照compare函数描述的排序规则,对stu这个数组进行冒泡排序 */
bool CompareByCourseScore(STUDENT a, STUDENT b);  /* 根据第 courseIndex 号课程的成绩进行排序 */
bool CompareByAverageScore(STUDENT a, STUDENT b); /* 根据平均分进行排序 */
bool Equals(char *s, char *d);                    /* 若s和d这两个字符串相同,返回true,否则返回false */

/* 输出到控制台 */

void PrintScore();                         /* 在控制台中打印所有学生的成绩 */
void PrintClassScore(char *className);     /* 打印某个班级所有学生的成绩 */
void PrintStudentScoreAndRanking(long ID); /* 打印某个学生的成绩和排名,ID是该学生的学号 */

/* 输出到文件 */

void PrintScoreToFile(const char *fileName);                           /* 将所有学生的成绩输出到文件中 */
void PrintClassScoreToFile(char *className, const char *fileName);     /* 将某个班级所有学生的成绩输出到文件中 */
void PrintStudentScoreAndRankingToFile(long ID, const char *fileName); /* 将某个学生的成绩和排名输出到文件中 */

int main()
{
    while (true)
    {
        PrintMenu();
        int operation;
        scanf("%d", &operation);
        while (operation < 0 || operation > 7)
        {
            printf("Error: The number %d is not between 0 and 7 ! \n", operation);
            PrintMenu();
            scanf("%d", &operation);
        }
        if (operation == 7)
        {
            return 0;
        }
        if (operation != 1 && false == ReadFromFile())
        {
            printf("Error: Please select operation 1 and input the scores ! \n");
            continue;
        }
        switch (operation)
        {
        case 0:
        {
            PrintScore();
            break;
        }
        case 1:
        {
            InputScore();
            WriteToFile();
            break;
        }
        case 2:
        {
            PrintScore();
            ModifyScore();
            break;
        }
        case 3:
        {
            for (int i = 0; i < M; i++)
            {
                printf(" %d.  %s\n", i, course[i]);
            }
            printf("Please input the course number:");
            scanf("%d", &courseIndex);
            if (courseIndex < 0 || courseIndex >= M)
            {
                printf("Error: input the wrong number! \n");
                break;
            }
            Sort(CompareByCourseScore);
            printf("This is the result sorted by %s: \n", course[courseIndex]);
            PrintScore();
            PrintScoreToFile(sortedScoreFileName);
            break;
        }
        case 4:
        {
            CalculateAverageScore();
            Sort(CompareByAverageScore);
            printf("This is the result sorted by average score: \n");
            PrintScore();
            PrintScoreToFile(sortedScoreFileName);
            break;
        }
        case 5:
        {
            char className[20];
            printf("Please input the class:");
            scanf("%s", className);
            printf("This is the score report of %s: \n", className);
            PrintClassScore(className);
            PrintClassScoreToFile(className, classScoreFileName);
            break;
        }
        case 6:
        {
            long ID;
            printf("Please input the student's ID:");
            scanf("%ld", &ID);
            if (FindStudentByID(ID) == -1)
            {
                printf("Error: There is no student with ID %ld \n", ID);
                break;
            }
            PrintStudentScoreAndRanking(ID);
            PrintStudentScoreAndRankingToFile(ID, studentScoreFileName);
            break;
        }
        }
        printf("\n");
    }
    return 0;
}
/* 在控制台中打印菜单 */
void PrintMenu()
{
    printf("--------------------------------MENU--------------------------------\n");
    printf(" 0. Show all students                                               \n"); /* 0.列出所有学生     */
    printf(" 1. Input students' information                                     \n"); /* 1.输入学生信息     */
    printf(" 2. Modify information by student's ID                              \n"); /* 2.修改学生信息     */
    printf(" 3. Sort by score of individual course                              \n"); /* 3.根据课程成绩排序 */
    printf(" 4. Sort by average score                                           \n"); /* 4.根据平均成绩排序 */
    printf(" 5. Print score report according to class                           \n"); /* 5.打印班级成绩单   */
    printf(" 6. Query score and ranking by student's ID                         \n"); /* 6.查询成绩和排名   */
    printf(" 7. Exit                                                            \n"); /* 7.退出 */
    printf("--------------------------------------------------------------------\n");
    printf("Please input the operation number:");
}
/* 在控制台中打印提示语,描述数据输入的格式 */
void PrintDataFormatTips(bool ignoreStudentID)
{
    printf("This is the input format of student information:\n");
    if (ignoreStudentID)
    {
        printf("%10s%20s", "name", "class");
    }
    else
    {
        printf("%10s%10s%20s", "ID", "name", "class");
    }
    for (int i = 0; i < M; i++)
    {
        printf("%10s", course[i]);
    }
    printf("\n");
}
/* 输入数据,包括学号、姓名、班级、以及每门课程的成绩 */
void InputScore()
{
    printf("How many student? ");
    scanf("%d", &n);
    PrintDataFormatTips(false);
    for (int i = 0; i < n; i++)
    {
        printf("Input record %d:\n", i + 1);
        scanf("%ld", &stu[i].studentID);
        scanf("%s", stu[i].studentName);
        scanf("%s", stu[i].className);
        for (int j = 0; j < M; j++)
        {
            scanf("%d", &stu[i].score[j]);
        }
    }
}
/* 为每位学生计算平均分 */
void CalculateAverageScore()
{
    int sum[N];
    for (int i = 0; i < n; i++)
    {
        sum[i] = 0;
        for (int j = 0; j < M; j++)
        {
            sum[i] = sum[i] + stu[i].score[j];
        }
        stu[i].averageScore = (float)sum[i] / M;
    }
}
/* 修改成绩 */
void ModifyScore()
{
    printf("Which student would you want to modify? Input the ID: ");
    long ID;
    scanf("%ld", &ID);
    int index = FindStudentByID(ID);
    if (-1 == index)
    {
        printf("Error: There is no student with ID %ld \n", ID);
        return;
    }
    printf("Please input the new information \n");
    PrintDataFormatTips(true);
    scanf("%s", stu[index].studentName);
    scanf("%s", stu[index].className);
    for (int j = 0; j < M; j++)
    {
        scanf("%d", &stu[index].score[j]);
    }
    WriteToFile();
    if (ReadFromFile())
    {
        printf("This is the new student list: \n");

        PrintScore();
    }
}
/* 将stu中保存的数据写入到文件中 */
bool WriteToFile()
{

    FILE *f = fopen(scoreFileName, "w");
    if (NULL == f)
    {
        printf("Error: Fail to open the file %s !\n", scoreFileName);
        return false;
    }
    fprintf(f, "%d", n);
    for (int i = 0; i < n; i++)
    {
        fprintf(f, "%10ld%10s%20s",
                stu[i].studentID,
                stu[i].studentName,
                stu[i].className);
        for (int j = 0; j < M; j++)
        {
            fprintf(f, "%10ld", stu[i].score[j]);
        }
        fprintf(f, "\n");
    }
    fclose(f);
    return true;
}
/* 从文件中读数据,保存到stu中 */
bool ReadFromFile()
{
    FILE *f = fopen(scoreFileName, "r");
    if (NULL == f)
    {
        printf("Error: Fail to open the file %s !\n", scoreFileName);
        return false;
    }
    fscanf(f, "%d", &n);
    for (int i = 0; i < n; i++)
    {
        fscanf(f, "%ld", &stu[i].studentID);
        fscanf(f, "%s", stu[i].studentName);
        fscanf(f, "%s", stu[i].className);
        for (int j = 0; j < M; j++)
        {
            fscanf(f, "%d", &stu[i].score[j]);
        }
    }
    fclose(f);
    return true;
}
/* 根据学号在stu中查找学生,若找得到则返回该学生在stu中的下标,否则返回-1 */
int FindStudentByID(long ID)
{
    for (int i = 0; i < n; i++)
    {
        if (ID == stu[i].studentID)
        {
            return i;
        }
    }
    return -1;
}
/* 按照compare函数描述的排序规则,对stu这个数组进行冒泡排序 */
void Sort(COMPARE compare)
{
    for (int i = 0; i < n - 1; i++)
    {
        bool swap = false;
        for (int j = 0; j < n - 1 - i; j++)
        {
            if (compare(stu[j], stu[j + 1]) == false)
            {
                STUDENT temp = stu[j];
                stu[j] = stu[j + 1];
                stu[j + 1] = temp;
                swap = true;
            }
        }
        if (swap == false)
        {
            return;
        }
    }
}
/* 根据第 courseIndex 号课程的成绩进行排序 */
bool CompareByCourseScore(STUDENT a, STUDENT b)
{
    return a.score[courseIndex] >= b.score[courseIndex];
}
/* 根据平均分进行排序 */
bool CompareByAverageScore(STUDENT a, STUDENT b)
{
    return a.averageScore >= b.averageScore;
}
/* 若s和d这两个字符串相同,返回true,否则返回false */
bool Equals(char *s, char *d)
{
    int i = 0;
    while (s[i] != '\0' && d[i] != '\0')
    {
        if (s[i] != d[i])
        {
            return false;
        }
        i++;
    }
    return s[i] == '\0' && d[i] == '\0';
}
/* 在控制台中打印所有学生的成绩 */
void PrintScore()
{
    CalculateAverageScore();
    printf("%10s%10s%20s",
           "ID",
           "name",
           "class");
    for (int j = 0; j < M; j++)
    {
        printf("%10s", course[j]);
    }
    printf("%10s", "average");
    printf("\n");
    for (int i = 0; i < n; i++)
    {
        printf("%10ld%10s%20s",
               stu[i].studentID,
               stu[i].studentName,
               stu[i].className);
        for (int j = 0; j < M; j++)
        {
            printf("%10ld", stu[i].score[j]);
        }
        printf("%10.1f", stu[i].averageScore);
        printf("\n");
    }
}
/* 打印某个班级所有学生的成绩 */
void PrintClassScore(char *className)
{
    CalculateAverageScore();
    printf("%10s%10s%20s",
           "ID",
           "name",
           "class");
    for (int j = 0; j < M; j++)
    {
        printf("%10s", course[j]);
    }
    printf("%10s", "average");
    printf("\n");
    for (int i = 0; i < n; i++)
    {
        if (Equals(stu[i].className, className))
        {
            printf("%10ld%10s%20s",
                   stu[i].studentID,
                   stu[i].studentName,
                   stu[i].className);
            for (int j = 0; j < M; j++)
            {
                printf("%10ld", stu[i].score[j]);
            }
            printf("%10.1f", stu[i].averageScore);
            printf("\n");
        }
    }
}
/* 打印某个学生的成绩和排名,ID是该学生的学号 */
void PrintStudentScoreAndRanking(long ID)
{
    CalculateAverageScore();
    int rankings[M];
    int averageRanking; /* 平均分的排名 */
    for (int i = 0; i < M; i++)
    {
        courseIndex = i;
        Sort(CompareByCourseScore);
        rankings[i] = FindStudentByID(ID) + 1;
    }
    Sort(CompareByAverageScore);
    int index = FindStudentByID(ID);
    averageRanking = index + 1;
    printf("This is the score report of %s: \n", stu[index].studentName);
    printf("%10s : %10ld \n", "ID", stu[index].studentID);
    printf("%10s : %10s \n", "name", stu[index].studentName);
    printf("%10s : %10s \n", "class", stu[index].className);
    for (int i = 0; i < M; i++)
    {
        printf("%10s :%10.1f  ranking: %d \n", course[i], (float)stu[index].score[i], rankings[i]);
    }
    printf("%10s :%10.1f  ranking: %d \n", "average", stu[index].averageScore, averageRanking);
}
/* 将所有学生的成绩输出到文件中 */
void PrintScoreToFile(const char *fileName)
{
    CalculateAverageScore();
    FILE *f = fopen(fileName, "w");
    if (NULL == f)
    {
        printf("Error: Fail to open the file %s !\n", fileName);
        return;
    }
    fprintf(f, "%10s%10s%20s",
            "ID",
            "name",
            "class");
    for (int j = 0; j < M; j++)
    {
        fprintf(f, "%10s", course[j]);
    }
    fprintf(f, "%10s", "average");
    fprintf(f, "\n");
    for (int i = 0; i < n; i++)
    {
        fprintf(f, "%10ld%10s%20s",
                stu[i].studentID,
                stu[i].studentName,
                stu[i].className);
        for (int j = 0; j < M; j++)
        {
            fprintf(f, "%10ld", stu[i].score[j]);
        }
        fprintf(f, "%10.1f", stu[i].averageScore);
        fprintf(f, "\n");
    }
    fclose(f);
}
/* 将某个班级所有学生的成绩输出到文件中 */
void PrintClassScoreToFile(char *className, const char *fileName)
{
    CalculateAverageScore();
    FILE *f = fopen(fileName, "w");
    if (NULL == f)
    {
        printf("Error: Fail to open the file %s !\n", fileName);
        return;
    }
    fprintf(f, "%10s%10s%20s",
            "ID",
            "name",
            "class");
    for (int j = 0; j < M; j++)
    {
        fprintf(f, "%10s", course[j]);
    }
    fprintf(f, "%10s", "average");
    fprintf(f, "\n");
    for (int i = 0; i < n; i++)
    {
        if (Equals(stu[i].className, className))
        {
            fprintf(f, "%10ld%10s%20s",
                    stu[i].studentID,
                    stu[i].studentName,
                    stu[i].className);
            for (int j = 0; j < M; j++)
            {
                fprintf(f, "%10ld", stu[i].score[j]);
            }
            fprintf(f, "%10.1f", stu[i].averageScore);
            fprintf(f, "\n");
        }
    }
    fclose(f);
}
/* 将某个学生的成绩和排名输出到文件中 */
void PrintStudentScoreAndRankingToFile(long ID, const char *fileName)
{
    CalculateAverageScore();
    FILE *f = fopen(fileName, "w");
    if (NULL == f)
    {
        printf("Error: Fail to open the file %s !\n", fileName);
        return;
    }
    int rankings[M];
    int averageRanking; /* 平均分的排名 */
    for (int i = 0; i < M; i++)
    {
        courseIndex = i;
        Sort(CompareByCourseScore);
        rankings[i] = FindStudentByID(ID) + 1;
    }
    Sort(CompareByAverageScore);
    int index = FindStudentByID(ID);
    averageRanking = index + 1;
    fprintf(f, "This is the score report of %s: \n", stu[index].studentName);
    fprintf(f, "%10s : %10ld \n", "ID", stu[index].studentID);
    fprintf(f, "%10s : %10s \n", "name", stu[index].studentName);
    fprintf(f, "%10s : %10s \n", "class", stu[index].className);
    for (int i = 0; i < M; i++)
    {
        fprintf(f, "%10s :%10.1f  ranking: %d \n", course[i], (float)stu[index].score[i], rankings[i]);
    }
    fprintf(f, "%10s :%10.1f  ranking: %d \n", "average", stu[index].averageScore, averageRanking);
    fclose(f);
}
  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-12-26 21:57:27  更:2021-12-26 21:59:33 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 12:01:54-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码