自动点名

举报
ReCclay 发表于 2022/02/22 01:59:04 2022/02/22
【摘要】 动态数组实现自动点名 描述 假设有若干个学生,为了威慑不来上课的同学,欲开发一套自动点名程序,每次课随机抽取若干个学生点名,给出结课后所以学生的 出勤分数。 规定 一共6次课 ...

动态数组实现自动点名

描述

假设有若干个学生,为了威慑不来上课的同学,欲开发一套自动点名程序,每次课随机抽取若干个学生点名,给出结课后所以学生的 出勤分数。

规定

 一共6次课
 每个学生出勤满分为5分
 如果出现一次缺勤扣1分,两次缺勤扣3分,三次及三次以上扣5分。 常规写法

   
  
  • 1
  • 2
  • 3
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX 100
#define CMAX 4//点名最多次数

int InputInfo(char stuName[][20], char stuID[][10])
{
    int stuNum, i;
    printf("Please input the number of students!\n");
    scanf("%d", &stuNum);
    printf("Please input the information of students!\n");
    getchar();//读出缓冲区的空格
    for(i=0; i<stuNum; i++)
    {
        gets(stuName[i]);//gets遇到回车就结束了!
        gets(stuID[i]);
    }
    return stuNum;
}
void RandomCall(char stuName[][20], char stuID[][10], int stuAbsentNum[], int stuNum)
{
    int i, callNum, callIndex[CMAX];
    char answer;
    srand(time(NULL));
    callNum = rand() % CMAX;
    for(i=0; i<callNum; i++)
    {
        callIndex[i] = rand() % stuNum;
        printf("Is the student %s, %s present?\n", stuName[callIndex[i]], stuID[callIndex[i]] );
        scanf(" %c", &answer);//读入学生是否出勤,注意在%c前加空格滤除缓冲区的空格!
        if(answer == 'n')
        {
            stuAbsentNum[callIndex[i]]++;
        }

    }
}

void SumScore(int stuAbsentNum[], int stuNum, int stuScore[])
{
    for(int i=0; i<stuNum; i++)
    {
        switch(stuAbsentNum[i])
        {
            case 0: stuScore[i] = 5; break;
            case 1: stuScore[i] = 4; break;
            case 2: stuScore[i] = 2; break;
            default: stuScore[i] = 0; break;

        }
    }
}
void OutputInfo(char stuName[][20], char stuID[][10], int stuScore[], int stuNum)
{
    for(int i=0; i<stuNum; i++)
    {
        printf("%s\t%s\t%d\n", stuName[i], stuID[i], stuScore[i]);
    }
}
int main()
{
    char stuName[MAX][20], stuID[MAX][10];
    int stuScore[MAX], stuAbsentNum[MAX]= {0};//后续要在缺勤次数上操作,要初始化!
    int stuNum, hours, i;//hours课时数
    stuNum = InputInfo(stuName, stuID);
    printf("Please input the number of lessons!\n");
    scanf("%d", &hours);
    for(i=0; i<hours; i++)
    {
        RandomCall(stuName, stuID, stuAbsentNum, stuNum);

    }
    SumScore(stuAbsentNum, stuNum, stuScore);
    OutputInfo(stuName, stuID, stuScore, stuNum);
    return 0;
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78

动态写法(动态一维数组使用方法)

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX 100
#define CMAX 4//点名最多次数

int InputInfo(char stuName[][20], char stuID[][10])
{
    int stuNum, i;
    printf("Please input the number of students!\n");
    scanf("%d", &stuNum);
    printf("Please input the information of students!\n");
    getchar();//读出缓冲区的空格
    for(i=0; i<stuNum; i++)
    {
        gets(stuName[i]);//gets遇到回车就结束了!
        gets(stuID[i]);
    }
    return stuNum;
}
void RandomCall(char stuName[][20], char stuID[][10], int stuAbsentNum[], int stuNum)
{
    int i, callNum;
    char answer;
    int *callIndex;//申请动态空间访问,因为指针和数组是通用的所以采用数组的形式也是合法的访问!
    srand(time(NULL));
    callNum = rand() % CMAX;
    callIndex = (int *)malloc(callNum*sizeof(int));
    if(callIndex == NULL)
    {
        printf("No enough memory!\n");
        exit(0);
    }
    else
    {
        for(i=0; i<callNum; i++)
        {
            callIndex[i] = rand() % stuNum; //数组形式
            //*(callIndex+i) = rand() % stuNum; //指针形式
            printf("Is the student %s, %s present?\n", stuName[callIndex[i]], stuID[callIndex[i]] );
            scanf(" %c", &answer);//读入学生是否出勤,注意在%c前加空格滤除缓冲区的空格!
            if(answer == 'n')
            {
                stuAbsentNum[callIndex[i]]++;
            }

        }
    }
    free(callIndex);
}

void SumScore(int stuAbsentNum[], int stuNum, int stuScore[])
{
    for(int i=0; i<stuNum; i++)
    {
        switch(stuAbsentNum[i])
        {
            case 0: stuScore[i] = 5; break;
            case 1: stuScore[i] = 4; break;
            case 2: stuScore[i] = 2; break;
            default: stuScore[i] = 0; break;

        }
    }
}
void OutputInfo(char stuName[][20], char stuID[][10], int stuScore[], int stuNum)
{
    for(int i=0; i<stuNum; i++)
    {
        printf("%s\t%s\t%d\n", stuName[i], stuID[i], stuScore[i]);
    }
}
int main()
{
    char stuName[MAX][20], stuID[MAX][10];
    int stuScore[MAX], stuAbsentNum[MAX]= {0};//后续要在缺勤次数上操作,要初始化!
    int stuNum, hours, i;//hours课时数
    stuNum = InputInfo(stuName, stuID);
    printf("Please input the number of lessons!\n");
    scanf("%d", &hours);
    for(i=0; i<hours; i++)
    {
        RandomCall(stuName, stuID, stuAbsentNum, stuNum);

    }
    SumScore(stuAbsentNum, stuNum, stuScore);
    OutputInfo(stuName, stuID, stuScore, stuNum);
    return 0;
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90

再改把里面的二维数组也改成动态申请的形式

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX 100
#define CMAX 4//点名最多次数

void InputInfo(char *pstuName, char *pstuID, int stuNum)
{
    printf("Please input the information of students!\n");
    for(int i=0; i<stuNum; i++)
    {
        gets(pstuName+i*20);//二维数组
        gets(pstuID+i*10);
    }
}
void RandomCall(char *pstuName, char *pstuID, int stuAbsentNum[], int stuNum)
{
    int i, callNum;
    char answer;
    int *callIndex;//申请动态空间访问,因为指针和数组是通用的所以采用数组的形式也是合法的访问!
    srand(time(NULL));
    callNum = rand() % CMAX;
    callIndex = (int *)malloc(callNum*sizeof(int));
    if(callIndex == NULL)
    {
        printf("No enough memory!\n");
        exit(0);
    }
    else
    {
        for(i=0; i<callNum; i++)
        {
            callIndex[i] = rand() % stuNum; //数组形式
            //*(callIndex+i) = rand() % stuNum; //指针形式
            printf("Is the student %s, %s present?\n", pstuName+ callIndex[i] *20, pstuID+ callIndex[i] *10);
            scanf(" %c", &answer);//读入学生是否出勤,注意在%c前加空格滤除缓冲区的空格!
            if(answer == 'n')
            {
                stuAbsentNum[callIndex[i]]++;
            }

        }
    }
    free(callIndex);
}

void SumScore(int stuAbsentNum[], int stuNum, int stuScore[])
{
    for(int i=0; i<stuNum; i++)
    {
        switch(stuAbsentNum[i])
        {
            case 0: stuScore[i] = 5; break;
            case 1: stuScore[i] = 4; break;
            case 2: stuScore[i] = 2; break;
            default: stuScore[i] = 0; break;

        }
    }
}
void OutputInfo(char *stuName, char *stuID, int stuScore[], int stuNum)
{
    for(int i=0; i<stuNum; i++)
    {
        printf("%s\t%s\t%d\n", stuName+i*20, stuID+i*10, stuScore[i]);
    }
}
int main()
{
    //char stuName[MAX][20], stuID[MAX][10];
    int stuScore[MAX], stuAbsentNum[MAX]= {0};//后续要在缺勤次数上操作,要初始化!
    int stuNum, hours, i;//hours课时数
    char *pstuName, *pstuID;
    printf("Please input the number of students!\n");
    scanf("%d", &stuNum);
    getchar();//读出缓冲区的空格
    pstuName = (char *)malloc(sizeof(char) *stuNum *20);
    pstuID = (char *)malloc(sizeof(char) *stuNum *20);
    if(pstuName == NULL || pstuID == NULL)
    {
        printf("No enough memory!\n");
        exit(0);
    }
    else
    {
        InputInfo(pstuName, pstuID, stuNum);
        printf("Please input the number of lessons!\n");
        scanf("%d", &hours);
        for(i=0; i<hours; i++)
        {
            RandomCall(pstuName, pstuID, stuAbsentNum, stuNum);

        }
        SumScore(stuAbsentNum, stuNum, stuScore);
        OutputInfo(pstuName, pstuID, stuScore, stuNum);
    }
    return 0;
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99

文章来源: recclay.blog.csdn.net,作者:ReCclay,版权归原作者所有,如需转载,请联系作者。

原文链接:recclay.blog.csdn.net/article/details/60870637

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。