系统城装机大师 - 固镇县祥瑞电脑科技销售部宣传站!

当前位置:首页 > 网络编程 > 其它综合 > 详细页面

C语言实现简单班级成绩管理系统

时间:2022-03-01来源:www.pcxitongcheng.com作者:电脑系统城

前言:

有朋友最近在做c语言课设,要求写一个班级成绩管理系统,便写份简单的代码来玩。代码原创,未参考任何其他人的代码

程序要求

说明

  • 本程序主要采用结构体数组
  • 本文件采用多文件编写,由于程序规模小,故未采用编写头文件的方式
  • 使用 #pragma once 来防止头文件重复包含

代码

怎么使用本程序看看注释应该就知道了。run main.c 就行。其他各文件作用:

  • def.c 定义了一些常量和全局变量,结构体
  • myIO.c 实现了成绩录入和成绩打印输出
  • file.c 实现了将成绩保存为文件
  • menu.c 实现了菜单功能
  • function.c 包含其他一些要用的函数

main.c

1
2
3
4
5
6
7
#include "menu.c"
 
int main()
{
    select();
    return 0;
}

def.c

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
// 相同头文件只包含一次,后不赘述
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define Status int
 
// 课程
typedef struct Course
{
    char name[30];
    int score;
} Course, *pCourse;
 
// 学生
typedef struct Student
{
    char number[30];
    char name[30];
    pCourse pC;
} Student, *pStudent;
 
// n是学生数, m是课程数
int n, m;
char courseName[30], studentName[30], course[20][30];
pStudent pS = NULL;

myIO.c

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
#pragma once
#include "def.c"
 
pStudent inputStudentInfo(void);
void printStudentInfo(pStudent pS);
 
// 录入学生信息
pStudent inputStudentInfo(void)
{
    int i, j;
    printf("Please input the number of students and courses: ");
    scanf("%d %d", &n, &m);
    printf("Please input the name of courses: ");
    for (i = 0; i < m; i++)
    {
        scanf("%s", course[i]);
    }
    pStudent pS = (pStudent)malloc(sizeof(Student) * n);
    if (!pS)
        return NULL;
    printf("Please input the info: \n");
    for (i = 0; i < n; i++)
    {
        pS[i].pC = (pCourse)malloc(sizeof(Course) * m);
        if (!pS[i].pC)
            return NULL;
        scanf("%s %s", pS[i].name, pS[i].number);
        for (j = 0; j < m; j++)
        {
            strcpy(pS[i].pC[j].name, course[j]);
            scanf("%d", &pS[i].pC[j].score);
        }
    }
    return pS;
}
 
// 打印所有学生信息
void printStudentInfo(pStudent pS)
{
    int i, j;
    // 打印标题
    printf("Name\tnumber\t");
    for (i = 0; i < m - 1; i++)
        printf("%s\t", course[i]);
    printf("%s\n", course[i]);
    // 显示信息
    for (i = 0; i < n; i++)
    {
        printf("%s\t%s\t", pS[i].name, pS[i].number);
        for (j = 0; j < m - 1; j++)
            printf("%d\t", pS[i].pC[j].score);
        printf("%d\n", pS[i].pC[j].score);
    }
}

file.c

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
#pragma once
#include "def.c"
Status saveStudentInfo(pStudent pS);
Status saveStudentInfo(pStudent pS)
{
    FILE *fp;
    int i, j;
    char filename[30], str[100] = "student number";
    printf("please input the filename: ");
    scanf("%s", filename);
    fp = fopen(filename, "w");
    if (!fp)
        return ERROR;
    for (i = 0; i < m; i++)
    {
        strcat(str, " ");
        strcat(str, course[i]);
    }
    strcat(str, "\n");
    for (i = 0; i < n; i++)
    {
        strcat(str, pS[i].name);
        strcat(str, " ");
        strcat(str, pS[i].number);
        for (j = 0; j < m; j++)
        {
            char score[30];
            itoa(pS[i].pC[j].score, score, 10);
            strcat(str, " ");
            strcat(str, score);
        }
        strcat(str, "\n");
    }
    fputs(str, fp);
    fclose(fp);
    return OK;
}

menu.c

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
100
101
102
103
104
105
106
107
108
109
#pragma once
#include "def.c"
#include "myIO.c"
#include "file.c"
#include "function.c"
void menu();
void select();
// 菜单
void menu()
{
    printf("------------------------------------\n");
    printf("|                Menu              |\n");
    printf("|              1. input            |\n");
    printf("|              2. show             |\n");
    printf("|              3. save             |\n");
    printf("|              4. sort             |\n");
    printf("|              5. modify           |\n");
    printf("|              6. count            |\n");
    printf("|              0. exit             |\n");
    printf("------------------------------------\n");
}
 
void select()
{
    int branch;
    while (TRUE)
    {
        system("cls");
        menu();
        printf("[Input]: ");
        scanf("%d", &branch);
        if (!branch)
            break;
        switch (branch)
        {
        case 1:
        {
            pS = inputStudentInfo();
            if (pS == NULL)
                printf("input error! please input again\n");
            else
                printf("Input success!\n");
            system("pause");
            break;
        }
        case 2:
        {
            printStudentInfo(pS);
            system("pause");
            break;
        }
        case 3:
        {
            if (OK == saveStudentInfo(pS))
                printf("Save success!\n");
            else
                printf("Save fail!\n");
            system("pause");
            break;
        }
        case 4:
        {
            sort(pS);
            printf("sort success\n");
            system("pause");
            break;
        }
        case 5:
        {
            int res = modify(pS);
            if (res)
            {
                printf("change success!\n");
            }
            else
            {
                printf("change fail!\n");
            }
            system("pause");
            break;
        }
        case 6:
        {
            int choose;
            // 输入1 显示每门课程最高成绩信息
            // 输入2 显示每门课程平均成绩信息
            printf("choose 1 for the highest score: \n ");
            printf("choose 2 for the average score: \n");
            printf("[Input]: ");
            scanf("%d", &choose);
            if (choose == 1)
            {
                showMax(pS);
            }
            else if (choose == 2)
            {
                showAverage(pS);
            }
            else
            {
                // 输入非法提示信息
                printf("Input error!\n");
            }
            system("pause");
            break;
        }
        }
    }
}

function.c

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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "def.c"
 
void sort(pStudent pS);
void Swap(pStudent s1, pStudent s2);
void showAverage(pStudent pS);
void showMax(pStudent pS);
Status modify(pStudent pS);
 
// 按课程成绩排序
void sort(pStudent pS)
{
    int courseNumber, i, j, k;
    char courseName[30];
    printf("please input the course name which you want to sort: ");
    scanf("%s", courseName);
    for (courseNumber = 0; courseNumber < m; courseNumber++)
        if (strcmp(course[courseNumber], courseName) == 0)
            break;
    // 如果找不到课程,则认为是按总分排序
    if (courseNumber == m)
    {
        printf("Sort as total score: \n");
        // 选择排序
        for (i = 0; i < n - 1; i++)
        {
            int flag = i;
            for (j = i + 1; j < n; j++)
            {
                int totalScore_1 = 0, totalScore_2 = 0;
                for (k = 0; k < m; k++)
                {
                    totalScore_1 += pS[j].pC[k].score;
                    totalScore_2 += pS[flag].pC[k].score;
                }
                if (totalScore_1 > totalScore_2)
                {
                    flag = j;
                }
            }
            Swap(&pS[i], &pS[flag]);
        }
    }
    else
    {
        // 选择排序
        for (i = 0; i < n - 1; i++)
        {
            int flag = i;
            for (j = i + 1; j < n; j++)
            {
                if (pS[j].pC[courseNumber].score > pS[flag].pC[courseNumber].score)
                {
                    flag = j;
                }
            }
            Swap(&pS[i], &pS[flag]);
        }
    }
}
 
// 修改学生信息
Status modify(pStudent pS)
{
    // 密码是1314
    char password[30] = "1314", psd[30];
    char number[30];
    int score, i, j;
    printf("please input password: ");
    scanf("%s", psd);
    // 密码正确才继续,否则返回ERROR
    if (strcmp(password, psd) == 0)
    {
        printf("please input the student's number: ");
        scanf("%s", number);
        for (i = 0; i < n; i++)
        {
            // 找到学生则继续,否则返回ERROR
            if (strcmp(pS[i].number, number) == 0)
            {
                printf("please input the course and score one by one: \n");
                scanf("%s %d", courseName, &score);
                for (j = 0; j < m; j++)
                {
                    // 找到课程才继续,否则返回ERROR
                    if (strcmp(pS[i].pC[j].name, courseName) == 0)
                    {
                        // 修改课程成绩
                        pS[i].pC[j].score = score;
                        return OK;
                    }
                }
                return ERROR;
            }
        }
        return ERROR;
    }
    else
        return ERROR;
}
 
// 输出各课程最高分的学生
void showMax(pStudent pS)
{
    int i, j, max;
    for (i = 0; i < m; i++)
    {
        max = 0;
        for (j = 0; j < n; j++)
        {
            if (pS[j].pC[i].score > pS[max].pC[i].score)
                max = j;
        }
        printf("%s\t%s\t%s\t%d\n", course[i], pS[max].name, pS[max].number, pS[max].pC[i].score);
    }
}
 
// 显示各课程的平均成绩
void showAverage(pStudent pS)
{
    int i, j;
    double ave;
    for (i = 0; i < m; i++)
    {
        ave = 0;
        for (j = 0; j < n; j++)
        {
            ave += pS[j].pC[i].score;
        }
        printf("%s\t%.2lf\n", course[i], ave / n);
    }
}
 
void Swap(pStudent s1, pStudent s2)
{
    int i;
    char studentName[30], number[30];
    // 交换姓名
    strcpy(studentName, s1->name);
    strcpy(s1->name, s2->name);
    strcpy(s2->name, studentName);
    // 交换学号
    strcpy(number, s1->number);
    strcpy(s1->number, s2->number);
    strcpy(s2->number, number);
    // 交换成绩
    for (i = 0; i < m; i++)
    {
        int temp = s1->pC[i].score;
        s1->pC[i].score = s2->pC[i].score;
        s2->pC[i].score = temp;
    }
}

以上就是本文的全部内容,希望对大家的学习有所帮助。

分享到:

相关信息

  • C++ AVLTree高度平衡的二叉搜索树深入分析

    一、AVL树的概念 二、AVL树节点的定义 三、AVL树的插入 四、AVL树的旋转 1.左单旋 2.右单旋 3.左右双旋 4.右左双旋 五、进行验证 六、AVLTree的性能...

    2023-03-09

  • idea构建web项目的超级详细教程

    1、idea构建web项目 1、新建一个空项目 2、新建java模块,名为webDemo1 3、选择webDemo1右键,选择Add Framework Support 4、在WEB-INF下新建文件夹classes和lib 5、打开项目结构(Project Structure) 6、项目配置 7、模...

    2023-03-09

系统教程栏目

栏目热门教程

人气教程排行

站长推荐

热门系统下载