苏小红版 c语言程序设计(第三版)系列实验题:学生成绩管理系统V2.0

2023-11-09

github:https://github.com/Jackie0Feng/SAMS
#系统需求描述

某班有最多不超过30人(具体人数由键盘输入)参加某门课的考试:用一维数组和函数指针作为函数参数编程实现如下学生成绩管理:
(1)录入每个学生的学号和考试成绩;
(2)计算课程的总分和平均分;
(3)按成绩由高到低排出名次表;
(4)按成绩由低到高排出名次表;
(5)按学号由小到大排出成绩表;
(6)按学号查询学生排名及其考试成绩;
(7)按优秀(90- 100)、良好(80- 89)、中等(70-79)、及格(60- 69)、不及格(0-59)5个类别,统计每个类别的人数以及所占的百分比;
(8)输出每个学生的学号、考试成绩,课程总分和平均分。

要求程序运行后先显示如下菜单,并提示用户输入选项:
1.Input record
2.Caculate total and average score of course
3.Sort in descending order by score
4.Sort in ascending order by score
5.Sort in ascending order by number
6.Search by number
7.Statistic analysis
8.List record
0.Exit
Please enter your choice:
根据用户输入的选项执行相应的操作

实验目的:在学生管理系统V1.0的基础上,通过增加任务要求,熟悉函数指针做函数参数,模块化程序设计以及增量测试方法。

#顶层修改
对主函数,菜单函数和头文件进行更改。

int main()
{
    int order = -1;//用户指令
    
    /*int n;
    long num[STU_NUM];
    float score[STU_NUM];*/
    //初始数据
    int n = 5;
    long num[STU_NUM] = { 10001,10002,10003,10004,10005 };
    float score[STU_NUM] = { 20.0,80.0,50.0,66.0,95.0 };
    Mean();//显示菜单
    //使用系统
    while (1)
    {
        scanf_s("%d", &order);
        switch (order)
        {
        case 1://录入成绩
            n = ReadScore(num, score, n);
            break;
        case 2://求平均分和总分
            AverSumofScore(score, n);
            break;
        case 3://按成绩降序排名
            SortbyScore(num, score, n, Descending);
            printf("Descendingly scorted by scores:\n");
            PrintScore(num, score, n);
            break;
        case 4://按成绩升序排名
            SortbyScore(num, score, n, Ascending);
            printf("Ascendingly scorted by scores:\n");
            PrintScore(num, score, n);
            break;
        case 5://按学号升序排名
            //AsSortbyNum(num, score, n);
            SortbyNum(num, score, n, Ascending);
            printf("Ascendingly scorted by num:\n");
            PrintScore(num, score, n);
            break;
        case 6://按学号查询学生排名及其考试成绩
        {//如果想在case标签初始化变量,必须加大括号{}
            int rank = -1;//学生排名
            int x;//被查询学号
            printf("Please enter the student ID queried\n");
            scanf_s("%d", &x);
            DeSortbyScore(num, score, n);//先排序
            rank = SearchbyNum(num, x, n);//即使排名也是所在数组的位置
            printf("The student's rank is %d and his grade is %f\n", rank+1, score[rank]);//索引+1为排名
            break;
        }
        case 7://按优秀(90- 100)、良好(80- 89)、中等(70-79)、及格(60- 69)、不及格(0-59)5个类别,
                   //统计每个类别的人数以及所占的百分比;
            StatistAnalysis( score, n);
            break;
        case 8://输出每个学生的学号、考试成绩,课程总分和平均分
            PrintScore(num, score, n);
            AverSumofScore(score, n);
            break;
        case 0://退出系统
            printf("Good Bye!\n");
            return 0;
            break;
        default:
            break;
        }
        printf("Please enter your choice : \n");
    }
    return 0;
}

int Mean(void)
{
    printf("1.Input record\n");
    printf("2.Caculate total and average score of course\n");
    printf("3.Sort in descending order by score\n");
    printf("4.Sort in ascending order by score\n");
    printf("5.Sort in ascending order by number\n");
    printf("6.Search by number\n");
    printf("7.Statistic analysis\n");
    printf("8.List record\n");
    printf("0.Exit\n");
    printf("Please enter your choice : \n");
    return 0;
}

由于使用了函数指针的函数,所以将将两个排序函数重新做了设计,为了提高代码复用性,也专门做了交换函数

/*
函数名称:   DeSortbyScore
功能描述:   按照成绩降序排列名次表
参数:
    num[]:学号
    score[]:分数
    n:总人数
返回:
备注:已弃用,被使用函数指针参数的SortbyScore替用*/
void DeSortbyScore(long num[], float score[], int n);

/*
函数名称:   SortbyScore
功能描述:   按照成绩排列名次表,使用函数指针表达升降序
参数:
    num[]:学号
    score[]:分数
    n:总人数
    *compare:比较函数指针
返回:
备注:*/
void SortbyScore(long num[], float score[], int n, int (*compare)(int a, int b));
/*
函数名称:   Ascending
功能描述:   升序比较函数
参数:
    a:比较数
    b:比较数
返回:a<b
备注:*/
int Ascending(int a, int b);
/*
函数名称:   Descending
功能描述:   降序比较函数
参数:
    a:比较数
    b:比较数
返回:a>b
备注:*/
int Descending(int a, int b);
/*
函数名称:   AsSortbyNum
功能描述:   按照学号升序排列
参数:
    num[]:学号
    score[]:分数
    n:总人数
返回:
备注:已弃用,被使用函数指针参数的SortbyNum替用*/
void AsSortbyNum(long num[], float score[], int n);
/*
函数名称:   SortbyNum
功能描述:   按照学号升序排列
参数:
    num[]:学号
    score[]:分数
    n:总人数
    *compare:比较函数指针
返回:
备注*/
void SortbyNum(long num[], float score[], int n, int(*compare)(int a, int b));
/*
函数名称:   FloatSwap
功能描述:   交换两个浮点型变量
参数:
    a:float型指针
    b:float型指针
返回:
备注:*/
void FloatSwap(float* a, float* b);
/*
函数名称:   LongSwap
功能描述:   交换两个长整型变量
参数:
    a:long型指针
    b:long型指针
返回:
备注:*/
void LongSwap(long* a, long* b);

#具体实现

void SortbyScore(long num[], float score[], int n, int(*compare)(int a, int b))
{
    long nTemp;
    float sTemp;
    //选择排序,第一层循环整个数组,每次归一位
    for (int i = 0; i < n - 1; i++)
    {
        int cmp = i;//比较位索引值,初始为乱序区第一位
        //第二层循环乱序位,每次从乱序列中选择最大一位
        for (int j = i; j < n; j++)
        {
            if ((*compare)(score[cmp], score[j]))
            {
                cmp = j;
            }
        }
        //发生改变,交换
        if (i != cmp)
        {
            LongSwap(&num[i], &num[cmp]);

            FloatSwap(&score[i], &score[cmp]);
        }
    }
}

int Ascending(int a, int b)
{
    return a>b;
}

int Descending(int a, int b)
{
    return a<b;
}

void SortbyNum(long num[], float score[], int n, int(*compare)(int a, int b))
{
    long nTemp;
    float sTemp;
    //选择排序,第一层循环整个数组,每次归一位
    for (int i = 0; i < n - 1; i++)
    {
        int cmp = i;//最大位索引值,初试为乱序区第一位
        //第二层循环乱序位,每次从乱序列中选择学号最小的一位
        for (int j = i + 1; j < n; j++)
        {
            if ((*compare)(num[cmp], num[j]))
            {
                cmp = j;
            }
        }
        //发生改变,交换
        if (i != cmp)
        {
            LongSwap(&num[i], &num[cmp]);

            FloatSwap(&score[i], &score[cmp]);
        }
    }
}

void FloatSwap(float* a, float* b)
{
    float tmp = *a;
    *a = *b;
    *b = tmp;
}

void LongSwap(long* a, long* b)
{
    long tmp = *a;
    *a = *b;
    *b = tmp;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

苏小红版 c语言程序设计(第三版)系列实验题:学生成绩管理系统V2.0 的相关文章

随机推荐