哈工大慕课 学生成绩管理系统V1.0~5.0

本文提供测试数据哦~
运行时请将数据粘贴到输入框中。

  • 由于自己才疏学浅,难免会有纰漏,假如你发现了有错误的地方,还望留言给我指出来,我会对其加以修正。
  • 如果你觉得文章还不错,你的转发、分享、赞赏、点赞、留言就是对我最大的鼓励。
  • 感谢您的阅读,十分欢迎并感谢您的关注哦~

学生成绩管理系统V1.0

/*注意: 本题没有空行! 所以一定要检查\n的使用!!!
数据:
11003001 87
11003005 98
11003003 75
11003002 48
11003004 65
11003006 100
*/
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

void printMessage();
void input(double score[], long id[], int n);
void caculate(double score[], int n);
void sortScore(double score[], long id[], int n);  //成绩降序
void sortNum(double score[], long id[], int n);    //学号升序
void search(double score[], long id[], int n);
void statistic(double score[], long id[], int n);
void output(double score[], long id[], int n);
void endProgram();
void wrong();

int main()
{
    int n = 0, choice = 0;
    long id[35];
    double score[35];
    printf("Input student number(n<30):\n");
    scanf("%d", &n);

    while(true)
    {
        printMessage();
        scanf("%d", &choice);

        switch(choice)
        {
        case 1: input(score, id, n);break;
        case 2: caculate(score, n);break;
        case 3: sortScore(score, id, n);break;
        case 4: sortNum(score, id, n); break;
        case 5: search(score, id, n); break;
        case 6: statistic(score, id, n); break;
        case 7: output(score, id, n);break;
        case 0: endProgram(); break;
        default: wrong(); break;
        }
    }

    return 0;

}

void printMessage()
{
    printf("Management for Students' scores\n");
    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 number\n");
    printf("5.Search by number\n");
    printf("6.Statistic analysis\n");
    printf("7.List record\n");
    printf("0.Exit\n");
    printf("Please Input your choice:\n");

}



void input(double score[], long id[], int n)   //1
{
    printf("Input student's ID, name and score:\n");
    for(int i = 0; i < n; i++)
    {
        scanf("%ld%lf", &id[i], &score[i]);
    }

}

void caculate(double score[], int n)   //2
{
    double sum = 0;
    for(int i = 0; i < n; i++)
    {
        sum += score[i];
    }
    printf("sum=%.0f,aver=%.2f\n", sum, sum / n);

}

void sortScore(double score[], long id[], int n)   //3: 成绩降序
{
    printf("Sort in descending order by score:\n");
    for(int i = 0; i < n - 1; i++)
    {
        for(int j = 0; j < n - 1 - i; j++)
        {
            if(score[j] < score[j + 1])
            {
                double tempScore = score[j];
                score[j] = score[j + 1];
                score[j + 1] = tempScore;

                long tempId = id[j];
                id[j] = id[j + 1];
                id[j + 1] = tempId;
            }
        }
    }
    /*打印*/
    for(int i = 0; i < n; i++)
    {
        printf("%ld\t%.0f\n", id[i], score[i]);
    }

}

void sortNum(double score[], long id[], int n)   //4: 学号升序
{
    printf("Sort in ascending order by number:\n");
    for(int i = 0; i < n - 1; i++)
    {
        for(int j = 0; j < n - 1 - i; j++)
        {
            if(id[j] > id[j + 1])
            {
                double tempScore = score[j];
                score[j] = score[j + 1];
                score[j + 1] = tempScore;

                long tempId = id[j];
                id[j] = id[j + 1];
                id[j + 1] = tempId;
            }
        }
    }
    /*打印*/
    for(int i = 0; i < n; i++)
    {
        printf("%ld\t%.0f\n", id[i], score[i]);
    }

}

void search(double score[], long id[], int n)   //5
{
    long searchNum = 0;
    printf("Input the number you want to search:\n");
    scanf("%ld", &searchNum);

    bool flag = false;
    for(int i = 0; i < n; i++)
    {
        if(id[i] == searchNum)
        {
            printf("%ld\t%.0f\n", id[i], score[i]);
            flag = true;
        }
    }

    if(flag == false)
    {
        printf("Not found!\n");
    }

}

void statistic(double score[], long id[], int n)   //6: 可以再优化一下
{
    double statisticScore[6] = {0};
    for(int i = 0; i < n; i++)
    {
        switch((int)score[i] / 10)
        {
        case 0:
        case 1:
        case 2:
        case 3:
        case 4:
        case 5: statisticScore[0]++; break;
        case 6: statisticScore[1]++; break;
        case 7: statisticScore[2]++; break;
        case 8: statisticScore[3]++; break;
        case 9: statisticScore[4]++; break;
        case 10: statisticScore[5]++; break;
        default: wrong(); break;
        }
    }
    /* 输出部分 */
    printf("<60\t%d\t%.2f%%\n", (int)statisticScore[0], statisticScore[0] / n * 100);

    printf("%d-%d\t%d\t%.2f%%\n", 60, 69, (int)statisticScore[1], statisticScore[1] / n * 100);
    printf("%d-%d\t%d\t%.2f%%\n", 70, 79, (int)statisticScore[2], statisticScore[2] / n * 100);
    printf("%d-%d\t%d\t%.2f%%\n", 80, 89, (int)statisticScore[3], statisticScore[3] / n * 100);
    printf("%d-%d\t%d\t%.2f%%\n", 90, 99, (int)statisticScore[4], statisticScore[4] / n * 100);

    printf("%d\t%d\t%.2f%%\n", 100, (int)statisticScore[5], statisticScore[5] / n * 100);

}

void output(double score[], long id[], int n)   //7
{
    for(int i = 0; i < n; i++)
    {
        printf("%ld\t%.0f\n", id[i], score[i]);
    }

}

void endProgram()   //0
{
    printf("End of program!\n");
    exit(0);

}

void wrong()   //default
{
    printf("Input error!\n");

}

学生成绩管理系统V2.0

/*
数据:
11003001 87
11003005 98
11003003 75
11003002 48
11003004 65
11003006 100
*/
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

void PrintMessage();
void Input(double score[], long id[], int n);
void Caculate(double score[], int n);

void SortScore(double score[], long id[], int n, bool (*compare)(double, double));  //成绩通用排序
bool Ascending(double x, double y);
bool Descending(double x, double y);

void SortNum(double score[], long id[], int n);    //学号升序
void Search(double score[], long id[], int n);
void Statistic(double score[], long id[], int n);
void Output(double score[], long id[], int n);
void EndProgram();
void Wrong();

int main()
{
    int n = 0, choice = 0;
    long id[35];
    double score[35];
    printf("Input student number(n<30):\n");
    scanf("%d", &n);

    while(true)
    {
        PrintMessage();
        scanf("%d", &choice);

        switch(choice)
        {
        case 1:
            Input(score, id, n);
            break;
        case 2:
            Caculate(score, n);
            break;
        case 3:
            printf("Sort in descending order by score:\n");
            SortScore( score, id, n, Descending );
            break;
        case 4:
            printf("Sort in ascending order by score:\n");
            SortScore( score, id, n, Ascending );
            break;
        case 5:
            SortNum(score, id, n);
            break;
        case 6:
            Search(score, id, n);
            break;
        case 7:
            Statistic(score, id, n);
            break;
        case 8:
            Output(score, id, n);
            break;
        case 0:
            EndProgram();
            break;
        default:
            Wrong();
            break;
        }
    }

    return 0;

}

void PrintMessage()
{
    printf("Management for Students' scores\n");
    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 Input your choice:\n");
}



void Input(double score[], long id[], int n)   //1
{
    printf("Input student's ID and score:\n");   //V2.0更改!
    for(int i = 0; i < n; i++)
    {
        scanf("%ld%lf", &id[i], &score[i]);
    }
}

void Caculate(double score[], int n)   //2
{
    double sum = 0;
    for(int i = 0; i < n; i++)
    {
        sum += score[i];
    }
    printf("sum=%.0f,aver=%.2f\n", sum, sum / n);
}

void SortScore(double score[], long id[], int n, bool (*compare)(double, double))  //成绩通用排序
{
    for(int i = 0; i < n - 1; i++)
    {
        for(int j = 0; j < n - 1 - i; j++)
        {
            if( (*compare)(score[j], score[j + 1]) )
            {
                double tempScore = score[j];
                score[j] = score[j + 1];
                score[j + 1] = tempScore;

                long tempId = id[j];
                id[j] = id[j + 1];
                id[j + 1] = tempId;
            }
        }
    }
    /*打印*/
    for(int i = 0; i < n; i++)
    {
        printf("%ld\t%.0f\n", id[i], score[i]);
    }
}

bool Ascending(double x, double y)   //4
{
    return x > y;
}

bool Descending(double x, double y)   //3
{
    return x < y;
}

void SortNum(double score[], long id[], int n)   //5: 学号升序
{
    printf("Sort in ascending order by number:\n");
    for(int i = 0; i < n - 1; i++)
    {
        for(int j = 0; j < n - 1 - i; j++)
        {
            if(id[j] > id[j + 1])
            {
                double tempScore = score[j];
                score[j] = score[j + 1];
                score[j + 1] = tempScore;

                long tempId = id[j];
                id[j] = id[j + 1];
                id[j + 1] = tempId;
            }
        }
    }
    /*打印*/
    for(int i = 0; i < n; i++)
    {
        printf("%ld\t%.0f\n", id[i], score[i]);
    }
}

void Search(double score[], long id[], int n)   //6:查找
{
    long searchNum = 0;
    printf("Input the number you want to search:\n");
    scanf("%ld", &searchNum);

    bool flag = false;
    for(int i = 0; i < n; i++)
    {
        if(id[i] == searchNum)
        {
            printf("%ld\t%.0f\n", id[i], score[i]);
            flag = true;
        }
    }

    if(flag == false)
    {
        printf("Not found!\n");
    }
}

void Statistic(double score[], long id[], int n)   //7:统计, 可以再优化一下
{
    double statisticScore[6] = {0};
    for(int i = 0; i < n; i++)
    {
        switch((int)score[i] / 10)
        {
        case 0:
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
            statisticScore[0]++;
            break;
        case 6:
            statisticScore[1]++;
            break;
        case 7:
            statisticScore[2]++;
            break;
        case 8:
            statisticScore[3]++;
            break;
        case 9:
            statisticScore[4]++;
            break;
        case 10:
            statisticScore[5]++;
            break;
        default:
            Wrong();
            break;
        }
    }
    /*输出部分*/
    printf("<60\t%d\t%.2f%%\n", (int)statisticScore[0], statisticScore[0] / n * 100);

    printf("%d-%d\t%d\t%.2f%%\n", 60, 69, (int)statisticScore[1], statisticScore[1] / n * 100);
    printf("%d-%d\t%d\t%.2f%%\n", 70, 79, (int)statisticScore[2], statisticScore[2] / n * 100);
    printf("%d-%d\t%d\t%.2f%%\n", 80, 89, (int)statisticScore[3], statisticScore[3] / n * 100);
    printf("%d-%d\t%d\t%.2f%%\n", 90, 99, (int)statisticScore[4], statisticScore[4] / n * 100);

    printf("%d\t%d\t%.2f%%\n", 100, (int)statisticScore[5], statisticScore[5] / n * 100);
}

void Output(double score[], long id[], int n)   //8
{
    for(int i = 0; i < n; i++)
    {
        printf("%ld\t%.0f\n", id[i], score[i]);
    }
}

void EndProgram()   //0
{
    printf("End of program!\n");
    exit(0);
}

void Wrong()   //default
{
    printf("Input error!\n");
}

学生成绩管理系统V3.0

/*数据:
11003001
lisi
87
11003005
heli
98
11003003
ludi
75
11003002
dumo
48
11003004
zuma
65
11003006
suyu
100
*/
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>

void PrintMessage();
void Input(double score[], long id[], int n, char name[][10]);           //1
void Caculate(double score[], int n);                                    //2

void SortScore(double score[], long id[], int n, bool (*compare)(double, double), char name[][10]); //3与4
bool Ascending(double x, double y);
bool Descending(double x, double y);

void SortbyId(double score[], long id[], int n, char name[][10]);        //5: 学号升序
void SortbyName(double score[], long id[], int n, char name[][10]);      //6: 按名字的字典序升序

void SearchbyScore(double score[], long id[], int n, char name[][10]);   //7: 查找分数
void SearchbyName(double score[], long id[], int n, char name[][10]);    //8: 查找名字

void Statistic(double score[], long id[], int n);                        //9
void Output(double score[], long id[], int n, char name[][10]);          //10
void EndProgram();   //0
void Wrong();        //default

int main()
{
    int n = 0, choice = 0;
    long id[35] = {0};
    double score[35] = {0};
    printf("Input student number(n<30):\n");
    scanf("%d", &n);

    char name[35][10];

    while(true)
    {
        PrintMessage();
        scanf("%d", &choice);

        switch(choice)
        {
        case 1:
            Input(score, id, n, name);
            break;
        case 2:
            Caculate(score, n);
            break;
        case 3:
            printf("Sort in descending order by score:\n");
            SortScore( score, id, n, Descending, name );
            break;
        case 4:
            printf("Sort in ascending order by score:\n");
            SortScore( score, id, n, Ascending, name );
            break;
        case 5:
            SortbyId(score, id, n, name);
            break;
        case 6:
            SortbyName(score, id, n, name);
            break;
        case 7:
            SearchbyScore(score, id, n, name);
            break;
        case 8:
            SearchbyName(score, id, n, name);
            break;
        case 9:
            Statistic(score, id, n);
            break;
        case 10:
            Output(score, id, n, name);
            break;
        case 0:
            EndProgram();
            break;
        default:
            Wrong();
            break;
        }
    }

    return 0;

}

void PrintMessage()
{
    printf("Management for Students' scores\n");
    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.Sort in dictionary order by name\n");

    printf("7.Search by number\n");
    printf("8.Search by name\n");
    printf("9.Statistic analysis\n");
    printf("10.List record\n");
    printf("0.Exit\n");
    printf("Please Input your choice:\n");
}

void Input(double score[], long id[], int n, char name[][10])   //1
{
    printf("Input student's ID, name and score:\n");
    for(int i = 0; i < n; i++)
    {
        scanf("%ld%s%lf", &id[i], name[i], &score[i]);
    }
}

void Caculate(double score[], int n)   //2
{
    double sum = 0;
    for(int i = 0; i < n; i++)
    {
        sum += score[i];
    }
    printf("sum=%.0f,aver=%.2f\n", sum, sum / n);
}

void SortScore(double score[], long id[], int n, bool (*compare)(double, double), char name[][10])
{
    //成绩通用排序 3与4
    for(int i = 0; i < n - 1; i++)
    {
        for(int j = 0; j < n - 1 - i; j++)
        {
            if( (*compare)(score[j], score[j + 1]) )
            {
                double tempScore = score[j];
                score[j] = score[j + 1];
                score[j + 1] = tempScore;

                long tempId = id[j];
                id[j] = id[j + 1];
                id[j + 1] = tempId;

                char tempName[10];
                strcpy(tempName, name[j]);
                strcpy(name[j], name[j + 1]);
                strcpy(name[j + 1], tempName);
            }
        }
    }
    /*打印*/
    for(int i = 0; i < n; i++)
    {
        printf("%ld\t%s\t%.0f\n", id[i], name[i], score[i]);
    }
}

bool Ascending(double x, double y)   //4
{
    return x > y;
}

bool Descending(double x, double y)   //3
{
    return x < y;
}

void SortbyId(double score[], long id[], int n, char name[][10])   //5: 学号升序
{
    printf("Sort in ascending order by number:\n");
    for(int i = 0; i < n - 1; i++)
    {
        for(int j = 0; j < n - 1 - i; j++)
        {
            if(id[j] > id[j + 1])
            {
                double tempScore = score[j];
                score[j] = score[j + 1];
                score[j + 1] = tempScore;

                long tempId = id[j];
                id[j] = id[j + 1];
                id[j + 1] = tempId;

                char tempName[10];
                strcpy(tempName, name[j]);
                strcpy(name[j], name[j + 1]);
                strcpy(name[j + 1], tempName);
            }
        }
    }
    /* 输出语句 */
    for(int i = 0; i < n; i++)
    {
        printf("%ld\t%s\t%.0f\n", id[i], name[i], score[i]);
    }
}

void SortbyName(double score[], long id[], int n, char name[][10])   //6: 按名字的字典序升序
{
    printf("Sort in dictionary order by name:\n");
    for(int i = 0; i < n - 1; i++)
    {
        for(int j = 0; j < n - 1 - i; j++)
        {
            if(strcmp(name[j], name[j + 1]) > 0)
            {
                double tempScore = score[j];
                score[j] = score[j + 1];
                score[j + 1] = tempScore;

                long tempId = id[j];
                id[j] = id[j + 1];
                id[j + 1] = tempId;

                char tempName[10];
                strcpy(tempName, name[j]);
                strcpy(name[j], name[j + 1]);
                strcpy(name[j + 1], tempName);
            }
        }
    }
    /* 输出语句 */
    for(int i = 0; i < n; i++)
    {
        printf("%ld\t%s\t%.0f\n", id[i], name[i], score[i]);
    }
}


void SearchbyScore(double score[], long id[], int n, char name[][10])   //7:查找
{
    long searchNum = 0;
    printf("Input the number you want to search:\n");
    scanf("%ld", &searchNum);

    bool flag = false;
    for(int i = 0; i < n; i++)
    {
        if(id[i] == searchNum)
        {
            printf("%ld\t%s\t%.0f\n", id[i], name[i], score[i]);
            flag = true;
        }
    }

    if(flag == false)
    {
        printf("Not found!\n");
    }
}

void SearchbyName(double score[], long id[], int n, char name[][10])   //8: 查找名字
{
    char searchName[10];
    printf("Input the name you want to search:\n");
    scanf(" %10s", searchName);   //注意: 这里要加上一个空格来吃掉回车!

    bool flag = false;
    for(int i = 0; i < n; i++)
    {
        if(strcmp(name[i], searchName) == 0)
        {
            printf("%ld\t%s\t%.0f\n", id[i], name[i], score[i]);
            flag = true;
        }
    }

    if(flag == false)
    {
        printf("Not found!\n");
    }
}

void Statistic(double score[], long id[], int n)   //9:统计, 可以再要优化一下
{
    double statisticScore[6] = {0};
    for(int i = 0; i < n; i++)
    {
        switch((int)score[i] / 10)
        {
        case 0:
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
            statisticScore[0]++;
            break;
        case 6:
            statisticScore[1]++;
            break;
        case 7:
            statisticScore[2]++;
            break;
        case 8:
            statisticScore[3]++;
            break;
        case 9:
            statisticScore[4]++;
            break;
        case 10:
            statisticScore[5]++;
            break;
        default:
            Wrong();
            break;
        }
    }
    /* 输出部分 */
    printf("<60\t%d\t%.2f%%\n", (int)statisticScore[0], statisticScore[0] / n * 100);

    printf("%d-%d\t%d\t%.2f%%\n", 60, 69, (int)statisticScore[1], statisticScore[1] / n * 100);
    printf("%d-%d\t%d\t%.2f%%\n", 70, 79, (int)statisticScore[2], statisticScore[2] / n * 100);
    printf("%d-%d\t%d\t%.2f%%\n", 80, 89, (int)statisticScore[3], statisticScore[3] / n * 100);
    printf("%d-%d\t%d\t%.2f%%\n", 90, 99, (int)statisticScore[4], statisticScore[4] / n * 100);

    printf("%d\t%d\t%.2f%%\n", 100, (int)statisticScore[5], statisticScore[5] / n * 100);
}

void Output(double score[], long id[], int n, char name[][10])   //10
{
    for(int i = 0; i < n; i++)
    {
        printf("%ld\t%s\t%.0f\n", id[i], name[i], score[i]);
    }
}

void EndProgram()   //0
{
    printf("End of program!\n");
    exit(0);
}

void Wrong()        //default
{
    printf("Input error!\n");
}

学生成绩管理系统V4.0

//注意: 本题没有空行! 所以一定要检查\n的使用!!!
/*数据1:
11003001
lisi
87
82
89
11003005
heli
98
92
90
11003003
ludi
75
78
80
11003002
dumo
48
50
67
11003004
zuma
65
69
72
11003006
suyu
100
95
94
*/
/*数据2:
6
1
3
11003001
lisi
87
82
89
11003005
heli
98
92
90
11003003
ludi
75
78
80
11003002
dumo
48
50
67
11003004
zuma
65
69
72
11003006
suyu
100
95
94
2
3
4
5
6
7
8
11003007
8
11003004
9
lili
9
lisi
10
11
12
0
*/
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>

void PrintMessage();
void Input(double score[][6], long id[], char name[][10], int n, int m);           //1
void CaculateCourse(double score[][6], int n, int m);                              //2
void CaculateStudent(double score[][6], double totalScore[], int n, int m);        //3

void SortScore(double score[][6], double totalScore[], long id[],
               char name[][10], int n, int m, bool (*compare)(double, double));
bool Ascending(double x, double y);
bool Descending(double x, double y);

void SortbyId(double score[][6], double totalScore[],
              long id[], char name[][10], int n, int m);      //5: 学号升序
void SortbyName(double score[][6], double totalScore[],
                long id[], char name[][10], int n, int m);    //6: 按名字的字典序升序

void SearchbyScore(double score[][6], double totalScore[], long id[],
                   char name[][10], int n, int m); //7: 查找分数
void SearchbyName(double score[][6], double totalScore[], long id[],
                  char name[][10], int n, int m);  //8: 查找名字

void Statistic(double score[][6], long id[], int n, int m);                         //9: 统计
void Output(double score[][6], double totalScore[], long id[], char name[][10], int n, int m);  //10
void EndProgram();   //0
void Wrong();        //default

void SwapD(double *x, double *y);
void SwapL(long *x, long *y);
void SwapStr(char x[], char y[]);
void SwapScore(double score[][6], int j, int m);

int main()
{
    int n = 0;   /*学生人数*/
    int m = 0;   /*课程数目*/
    int choice = 0;
    long id[35] = {0};               //学生人数最多是35
    double score[35][6] = {0};       //最多考试的科目数是6
    double totalScore[35] = {0};   /*学生总分*/
    printf("Input student number(n<30):\n");
    scanf("%d", &n);

    char name[35][10];   /*学生人数最多是35, 名字最长为10*/

    while(true)
    {
        PrintMessage();
        scanf("%d", &choice);

        switch(choice)
        {
        case 1:
            printf("Input course number(m<=%d):\n", 6);
            scanf("%d", &m);            //课程数目
            Input(score, id, name, n, m);
            break;
        case 2:
            CaculateCourse(score, n, m);
            break;
        case 3:
            CaculateStudent(score, totalScore, n, m);
            break;
        case 4:
            printf("Sort in descending order by score:\n");
            SortScore(score, totalScore, id, name, n, m, Descending);
            break;
        case 5:
            printf("Sort in ascending order by score:\n");
            SortScore(score, totalScore, id, name, n, m, Ascending);
            break;
        case 6:
            SortbyId(score, totalScore, id, name, n, m);
            break;
        case 7:
            SortbyName(score, totalScore, id, name, n, m);
            break;
        case 8:
            SearchbyScore(score, totalScore, id, name, n, m);
            break;
        case 9:
            SearchbyName(score, totalScore, id, name, n, m);
            break;
        case 10:
            Statistic(score, id, n, m);
            break;
        case 11:
            Output(score, totalScore, id, name, n, m);
            break;
        case 0:
            EndProgram();
            break;
        default:
            Wrong();
            break;
        }
    }
    return 0;

}

void PrintMessage()
{
    printf("Management for Students' scores\n");
    printf("1.Input record\n");
    printf("2.Caculate total and average score of every course\n");
    printf("3.Caculate total and average score of every student\n");

    printf("4.Sort in descending order by score\n");
    printf("5.Sort in ascending order by score\n");
    printf("6.Sort in ascending order by number\n");
    printf("7.Sort in dictionary order by name\n");

    printf("8.Search by number\n");
    printf("9.Search by name\n");

    printf("10.Statistic analysis\n");
    printf("11.List record\n");
    printf("0.Exit\n");
    printf("Please Input your choice:\n");
}

void Input(double score[][6], long id[], char name[][10], int n, int m)   //1
{
    printf("Input student's ID, name and score:\n");
    for(int i = 0; i < n; i++)
    {
        scanf("%ld%s", &id[i], name[i]);
        for(int j = 0; j < m; j++)
        {
            scanf("%lf", &score[i][j]);
        }
    }
}

void CaculateCourse(double score[][6], int n, int m)   //2
{
    double sum = 0;
    for(int i = 0; i < m; i++)     //课程数
    {
        sum = 0;
        for(int j = 0; j < n; j++) //学生数
        {
            sum += score[j][i];
        }
        printf("course %d:sum=%.0f,aver=%.0f\n", i + 1, sum, sum / n);
    }
}

void CaculateStudent(double score[][6], double totalScore[], int n, int m)   //3
{
    for(int i = 0; i < n; i++)     //学生数
    {
        for(int j = 0; j < m; j++) //课程数
        {
            totalScore[i] += score[i][j];
        }
        printf("student %d:sum=%.0f,aver=%.0f\n", i + 1, totalScore[i], totalScore[i] / m);
    }
}

void SortScore(double score[][6], double totalScore[], long id[], char name[][10], int n, int m, bool (*compare)(double, double))
{
    //成绩通用排序 4与5
    for(int i = 0; i < n - 1; i++)
    {
        for(int j = 0; j < n - 1 - i; j++)
        {
            if( (*compare)(totalScore[j], totalScore[j + 1]) )
            {
                SwapD(&totalScore[j], &totalScore[j + 1]);   /*注意这里的交换*/
                SwapL(&id[j], &id[j + 1]);
                SwapStr(name[j], name[j + 1]);
                SwapScore(score, j, m);
            }
        }
    }
    /*打印*/
    Output(score, totalScore, id, name, n, m);
}

bool Descending(double x, double y)   //5
{
    return x < y;
}

bool Ascending(double x, double y)   //4
{
    return x > y;
}

void SortbyId(double score[][6], double totalScore[], long id[], char name[][10], int n, int m)   //6: 学号升序
{
    printf("Sort in ascending order by number:\n");
    for(int i = 0; i < n - 1; i++)
    {
        for(int j = 0; j < n - 1 - i; j++)
        {
            if(id[j] > id[j + 1])
            {
                SwapD(&totalScore[j], &totalScore[j + 1]);   /*注意这里的交换*/
                SwapL(&id[j], &id[j + 1]);
                SwapStr(name[j], name[j + 1]);
                SwapScore(score, j, m);
            }
        }
    }
    /*打印*/
    Output(score, totalScore, id, name, n, m);
}

void SortbyName(double score[][6], double totalScore[], long id[], char name[][10], int n, int m)   //7: 按名字的字典序升序
{
    printf("Sort in dictionary order by name:\n");
    for(int i = 0; i < n - 1; i++)
    {
        for(int j = 0; j < n - 1 - i; j++)
        {
            if(strcmp(name[j], name[j + 1]) > 0)
            {
                SwapD(&totalScore[j], &totalScore[j + 1]);   /*注意这里的交换*/
                SwapL(&id[j], &id[j + 1]);
                SwapStr(name[j], name[j + 1]);
                SwapScore(score, j, m);
            }
        }
    }
    /*打印*/
    Output(score, totalScore, id, name, n, m);
}


void SearchbyScore(double score[][6], double totalScore[], long id[], char name[][10], int n, int m)   //8:查找
{
    long searchNum = 0;
    printf("Input the number you want to search:\n");
    scanf("%ld", &searchNum);

    bool flag = false;
    for(int i = 0; i < n; i++)
    {
        if(id[i] == searchNum)
        {
            flag = true;                           /*同下*/
            printf("%ld\t%s\t", id[i], name[i]);
            for(int j = 0; j < m; j++)
            {
                printf("%.0f\t", score[i][j]);
            }
            printf("%.0f\t%.0f\n", totalScore[i], totalScore[i] / m);
            break;                 //确保只输出一行
        }
    }

    if(flag == false)
    {
        printf("Not found!\n");
    }
}

void SearchbyName(double score[][6], double totalScore[], long id[], char name[][10], int n, int m)   //8: 查找名字
{
    char searchName[10];
    printf("Input the name you want to search:\n");
    scanf(" %10s", searchName);   //注意: 这里要加上一个空格来吃掉回车!

    bool flag = false;
    for(int i = 0; i < n; i++)
    {
        if(strcmp(name[i], searchName) == 0)
        {
            flag = true;                         /*同上*/
            printf("%ld\t%s\t", id[i], name[i]);
            for(int j = 0; j < m; j++)
            {
                printf("%.0f\t", score[i][j]);
            }
            printf("%.0f\t%.0f\n", totalScore[i], totalScore[i] / m);
            break;               //确保只输出一行
        }
    }

    if(flag == false)
    {
        printf("Not found!\n");
    }
}

void Statistic(double score[][6], long id[], int n, int m)   //10:统计
{
    for(int j = 0; j < m; j++)          /*主循环次数: 课程数m*/
    {
        double statisticScore[6] = {0};
        for(int i = 0; i < n; i++)
        {
            switch((int)score[i][j] / 10)
            {
            case 0:
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
                statisticScore[0]++;
                break;
            case 6:
                statisticScore[1]++;
                break;
            case 7:
                statisticScore[2]++;
                break;
            case 8:
                statisticScore[3]++;
                break;
            case 9:
                statisticScore[4]++;
                break;
            case 10:
                statisticScore[5]++;
                break;
            default:
                Wrong();
                break;
            }
        }
        /* 输出部分 */
        printf("For course %d:\n", j + 1);
        printf("<60\t%d\t%.2f%%\n", (int)statisticScore[0], statisticScore[0] / n * 100);
        printf("%d-%d\t%d\t%.2f%%\n", 60, 69, (int)statisticScore[1], statisticScore[1] / n * 100);
        printf("%d-%d\t%d\t%.2f%%\n", 70, 79, (int)statisticScore[2], statisticScore[2] / n * 100);
        printf("%d-%d\t%d\t%.2f%%\n", 80, 89, (int)statisticScore[3], statisticScore[3] / n * 100);
        printf("%d-%d\t%d\t%.2f%%\n", 90, 99, (int)statisticScore[4], statisticScore[4] / n * 100);
        printf("%d\t%d\t%.2f%%\n", 100, (int)statisticScore[5], statisticScore[5] / n * 100);

    }
}

void Output(double score[][6], double totalScore[], long id[], char name[][10], int n, int m)   //11
{
    for(int i = 0; i < n; i++)
    {
        printf("%ld\t%s\t", id[i], name[i]);
        for(int j = 0; j < m; j++)
        {
            printf("%.0f\t", score[i][j]);
        }
        printf("%.0f\t%.0f\n", totalScore[i], totalScore[i] / m);
    }                                             //注意这里是课程数m
}

void EndProgram()   //0
{
    printf("End of program!\n");
    exit(0);
}

void Wrong()        //default
{
    printf("Input error!\n");
}

void SwapD(double *x, double *y)  /*注意交换两个数*/
{
    double temp = *x;
    *x = *y;
    *y = temp;
}

void SwapL(long *x, long *y)     /*注意交换两个数*/
{
    long temp = *x;
    *x = *y;
    *y = temp;
}

void SwapStr(char x[], char y[])
{
    char temp[10];
    strcpy(temp, x);
    strcpy(x, y);
    strcpy(y, temp);
}

void SwapScore(double score[][6], int j, int m)
{
    for(int k = 0; k < m; k++)   /*将二位数组整行交换*/
    {
        double temp = score[j][k];
        score[j][k] = score[j + 1][k];
        score[j + 1][k] = temp;
    }
}



学生成绩管理系统V5.0

/*数据:
6
1
3
11003001
lisi
87
82
89
11003005
heli
98
92
90
11003003
ludi
75
78
80
11003002
dumo
48
50
67
11003004
zuma
65
69
72
11003006
suyu
100
95
94
2
3
4
5
6
7
8
11003007
8
11003004
9
lili
9
lisi
10
11
12
0
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#define  MAX_LEN  10               /* 名字最大长度 */
#define  STU_NUM 30                /* 最多的学生人数 */
#define  COURSE_NUM 6              /* 最多的考试科目数 */

typedef struct student
{
    long num;			        /* 学号 */
    char name[MAX_LEN];         /* 姓名 */
    double score[COURSE_NUM];	/* 每个学生各门功课的成绩 */
    double sum;                 /* 总成绩 */
    double aver;               	/* 平均成绩 */
} STU;

int   Menu(void);
void  ReadScore(STU stu[], int n, int m);
void  AverSumofEveryStudent(STU stu[], int n, int m);
void  AverSumofEveryCourse(STU stu[], int n, int m);
void  SortbyScore(STU stu[], int n, int m, int (*compare)(double a, double b));
int   Ascending(double a, double b);
int   Descending(double a, double b);
void  SwapSTU(STU *a, STU *b);
void  AsSortbyNum(STU stu[], int n, int m);
void  SortbyName(STU stu[], int n, int m);
void  SearchbyNum(STU stu[], int n, int m);
void  SearchbyName(STU stu[], int n, int m);
void  StatisticAnalysis(STU stu[], int n, int m);
void  PrintScore(STU stu[], int n, int m);

int main()
{
    char ch;
    int n = 0, m = 0;  /* 学生人数为n, 课程门数为m */
    STU stu[STU_NUM];
    printf("Input student number(n<=30):\n");    //是<= 程序示例中有错误
    scanf("%d", &n);
    while(true)
    {
        ch = Menu();       /* 显示菜单, 并读取用户输入 */
        switch(ch)
        {
        case 1:
            printf("Input course number(m<=%d):\n", COURSE_NUM);
            scanf("%d", &m);
            ReadScore(stu, n, m);
            break;
        case 2:
            AverSumofEveryCourse(stu, n, m);
            break;
        case 3:
            AverSumofEveryStudent(stu, n, m);
            break;
        case 4:
            SortbyScore(stu, n, m, Descending);
            printf("Sort in descending order by score:\n");
            PrintScore(stu, n, m);
            break;
        case 5:
            SortbyScore(stu, n, m, Ascending);
            printf("Sort in ascending order by score:\n");
            PrintScore(stu, n, m);
            break;
        case 6:
            AsSortbyNum(stu, n, m);
            printf("Sort in ascending order by number:\n");
            PrintScore(stu, n, m);
            break;
        case 7:
            SortbyName(stu, n, m);
            printf("Sort in dictionary order by name:\n");
            PrintScore(stu, n, m);
            break;
        case 8:
            SearchbyNum(stu, n, m);
            break;
        case 9:
            SearchbyName(stu, n, m);
            break;
        case 10:
            StatisticAnalysis(stu, n, m);
            break;
        case 11:
            PrintScore(stu, n, m);
            break;
        case 0:
            printf("End of program!");
            exit(0);
        default:
            printf("Input error!\n");
        }
    }
    return 0;
}

/*  函数功能:显示菜单并获得用户键盘输入的选项 */
int Menu(void)
{
    printf("Management for Students' scores\n");
    printf("1.Input record\n");
    printf("2.Caculate total and average score of every course\n");
    printf("3.Caculate total and average score of every student\n");
    printf("4.Sort in descending order by score\n");
    printf("5.Sort in ascending order by score\n");
    printf("6.Sort in ascending order by number\n");
    printf("7.Sort in dictionary order by name\n");
    printf("8.Search by number\n");
    printf("9.Search by name\n");
    printf("10.Statistic analysis\n");
    printf("11.List record\n");
    printf("0.Exit\n");
    printf("Please Input your choice:\n");
    int choice = 0;
    scanf("%d", &choice);
    return choice;
}

/* 函数功能:输入n个学生的m门课成绩 */
void ReadScore(STU stu[], int n, int m)
{
    printf("Input student's ID, name and score:\n");
    for(int i = 0; i < n; i++)
    {
        scanf("%ld%s", &stu[i].num, stu[i].name);
        for(int j = 0; j < m; j++)
        {
            scanf("%lf", &stu[i].score[j]);
        }
    }
}

/* 函数功能:计算每门课程的总分和平均分 */
void AverSumofEveryCourse(STU stu[], int n, int m)
{
    double sum = 0;
    for(int i = 0; i < m; i++)     //课程数
    {
        sum = 0;
        for(int j = 0; j < n; j++) //学生数
        {
            sum += stu[j].score[i];
        }
        printf("course %d:sum=%.0f,aver=%.0f\n", i + 1, sum, sum / n);
    }
}

/* 函数功能:计算每个学生总分和平均分 */
void AverSumofEveryStudent(STU stu[], int n, int m)
{
    for(int i = 0; i < n; i++)
    {
        stu[i].sum = 0;             //易错点: 一定要为结构体先赋初值~~~
        for(int j = 0; j < m; j++)
        {
            stu[i].sum += stu[i].score[j];
        }
        stu[i].aver = stu[i].sum / m;
    }
    for(int i = 0; i < n; i++)
    {
        printf("student %d: sum=%.0f,aver=%.0f\n", i + 1, stu[i].sum, stu[i].aver);
    }                 //空格~
}

/* 函数功能:按冒泡法将学生的总分sum进行排序 */
void SortbyScore(STU stu[], int n, int m, int (*compare)(double a, double b))
{
    for(int i = 0; i < n - 1; i++)
    {
        for(int j = 0; j < n - 1 - i; j++)
        {
            if( (*compare)(stu[j].sum, stu[j + 1].sum) )
            {
                SwapSTU(&stu[j], &stu[j + 1]);
            }
        }
    }
}

/* 使数据按升序排序 */
int Ascending(double a, double b)
{
    return a > b;
}

/* 使数据按降序排序 */
int Descending(double a, double b)
{
    return a < b;
}

/* 交换两个结构体 */
void SwapSTU(STU *a, STU *b)
{
    STU temp = *a;
    *a = *b;
    *b = temp;
}

/* 函数功能:按冒泡法将数组num的元素值按从低到高排序 */
void AsSortbyNum(STU stu[], int n, int m)
{
    for(int i = 0; i < n - 1; i++)
    {
        for(int j = 0; j < n - 1 - i; j++)
        {
            if(stu[j].num > stu[j + 1].num)
            {
                SwapSTU(&stu[j], &stu[j + 1]);
            }
        }
    }
}

/* 函数功能:冒泡法实现名字按字典顺序升序排序 */
void SortbyName(STU stu[], int n, int m)
{
    for(int i = 0; i < n - 1; i++)
    {
        for(int j = 0; j < n - 1 - i; j++)
        {
            if(strcmp(stu[j].name, stu[j + 1].name) > 0)
            {
                SwapSTU(&stu[j], &stu[j + 1]);
            }
        }
    }
}

/* 函数功能:按学号查找学生成绩并显示查找结果 */
void SearchbyNum(STU stu[], int n, int m)
{
    long searchNum = 0;
    printf("Input the number you want to search:\n");
    scanf("%ld", &searchNum);

    bool flag = false;
    for(int i = 0; i < n; i++)
    {
        if(stu[i].num == searchNum)
        {
            flag = true;                           /*同下*/
            printf("%ld\t%s\t", stu[i].num, stu[i].name);
            for(int j = 0; j < m; j++)
            {
                printf("%.0f\t", stu[i].score[j]);
            }
            printf("%.0f\t%.0f\n", stu[i].sum, stu[i].aver);
            break;                 //确保只输出一行
        }
    }

    if(flag == false)
    {
        printf("Not found!\n");
    }
}

/* 函数功能:按姓名的字典顺序排出成绩表 */
void SearchbyName(STU stu[], int n, int m)
{
    char searchName[10];
    printf("Input the name you want to search:\n");
    scanf(" %10s", searchName);   //注意: 这里要加上一个空格来吃掉回车!

    bool flag = false;
    for(int i = 0; i < n; i++)
    {
        if(strcmp(stu[i].name, searchName) == 0)
        {
            flag = true;                         /*同上*/
            printf("%ld\t%s\t", stu[i].num, stu[i].name);
            for(int j = 0; j < m; j++)
            {
                printf("%.0f\t", stu[i].score[j]);
            }
            printf("%.0f\t%.0f\n", stu[i].sum, stu[i].aver);
            break;               //确保只输出一行
        }
    }

    if(flag == false)
    {
        printf("Not found!\n");
    }
}

/* 函数功能:统计各分数段的学生人数及所占的百分比 */
void StatisticAnalysis(STU stu[], int n, int m)
{
    for(int j = 0; j < m; j++)          /* 主循环次数: 课程数m */
    {
        double statisticScore[6] = {0};
        for(int i = 0; i < n; i++)
        {
            switch((int)stu[i].score[j] / 10)
            {
            case 0:
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
                statisticScore[0]++;
                break;
            case 6:
                statisticScore[1]++;
                break;
            case 7:
                statisticScore[2]++;
                break;
            case 8:
                statisticScore[3]++;
                break;
            case 9:
                statisticScore[4]++;
                break;
            case 10:
                statisticScore[5]++;
                break;
            default:
                printf("Not found!\n");
                break;
            }
        }
        /*输出部分*/
        printf("For course %d:\n", j + 1);
        printf("<60\t%d\t%.2f%%\n", (int)statisticScore[0], statisticScore[0] / n * 100);
        printf("%d-%d\t%d\t%.2f%%\n", 60, 69, (int)statisticScore[1], statisticScore[1] / n * 100);
        printf("%d-%d\t%d\t%.2f%%\n", 70, 79, (int)statisticScore[2], statisticScore[2] / n * 100);
        printf("%d-%d\t%d\t%.2f%%\n", 80, 89, (int)statisticScore[3], statisticScore[3] / n * 100);
        printf("%d-%d\t%d\t%.2f%%\n", 90, 99, (int)statisticScore[4], statisticScore[4] / n * 100);
        printf("%d\t%d\t%.2f%%\n", 100, (int)statisticScore[5], statisticScore[5] / n * 100);

    }
}


/* 函数功能: 打印学生成绩 */
void PrintScore(STU stu[], int n, int m)
{
    for(int i = 0; i < n; i++)
    {
        printf("%ld\t%s\t", stu[i].num, stu[i].name);
        for(int j = 0; j < m; j++)
        {
            printf("%.0f\t", stu[i].score[j]);
        }
        printf("%.0f\t%.0f\n", stu[i].sum, stu[i].aver);
    }
}

  • 15
    点赞
  • 64
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值