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

 更新时间:2022年3月1日 13:10  点击:213 作者:和谐创新

前言:

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

程序要求

说明

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

代码

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

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

main.c

#include "menu.c"

int main()
{
    select();
    return 0;
}

def.c

// 相同头文件只包含一次,后不赘述
#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

#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

#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

#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

#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;
    }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持猪先飞。

原文出处:https://blog.csdn.net/hexiechuangxin/article/details/1203193

[!--infotagslink--]

相关文章

  • C语言实现放烟花的程序

    这篇文章主要为大家详细介绍了C语言实现放烟花的程序,有音乐播放,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-02-23
  • C语言中的字符(char)详细讲解

    本篇文章主要介绍C语言中char的知识,并附有代码实例,以便大家在学习的时候更好的理解,有需要的可以看一下...2020-04-25
  • 详解如何将c语言文件打包成exe可执行程序

    这篇文章主要介绍了详解如何将c语言文件打包成exe可执行程序,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-25
  • python实现学生通讯录管理系统

    这篇文章主要为大家详细介绍了python实现学生通讯录管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-02-25
  • C语言中free函数的使用详解

    free函数是释放之前某一次malloc函数申请的空间,而且只是释放空间,并不改变指针的值。下面我们就来详细探讨下...2020-04-25
  • C语言中计算正弦的相关函数总结

    这篇文章主要介绍了C语言中计算正弦的相关函数总结,包括正弦和双曲线正弦以及反正弦的函数,需要的朋友可以参考下...2020-04-25
  • 详解C语言中的rename()函数和remove()函数的使用方法

    这篇文章主要介绍了详解C语言中的rename()函数和remove()函数的使用方法,是C语言入门学习中的基础知识,需要的朋友可以参考下...2020-04-25
  • C语言中求和、计算平均值、方差和标准差的实例

    这篇文章主要介绍了C语言中求和、计算平均值、方差和标准差的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-12-10
  • C语言的基本语法详解

    本篇文章主要讲解C语言 基本语法,这里提供简单的示例和代码来详细讲解C语言的基本语法,开始学习C语言的朋友可以看一下,希望能够给你带来帮助...2021-09-18
  • 护卫神 主机管理系统使用说明(MSSQL管理)

    护卫神·主机管理系统该版本支持在Windows Server 200320082012,含32位和64位,直接开设配置WEB站、FTP站,以及SQL Server和MySQL,是您开设和管理虚拟主机的绝好帮手。但是对于新用户可能在使用上有一些困难,因此请仔细阅读如下说明文档...2016-01-27
  • C语言中send()函数和sendto()函数的使用方法

    这篇文章主要介绍了C语言中send()函数和sendto()函数的使用方法,是C语言入门学习中的基础知识,需要的朋友可以参考下...2020-04-25
  • C语言实现从文件读入一个3*3数组,并计算每行的平均值

    今天小编就为大家分享一篇C语言实现从文件读入一个3*3数组,并计算每行的平均值,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-04-25
  • 使用C语言操作文件的基本函数整理

    这篇文章主要介绍了使用C语言操作文件的基本函数整理,包括创建和打开以及关闭文件的操作方法,需要的朋友可以参考下...2020-04-25
  • C语言中memcpy 函数的用法详解

    这篇文章主要介绍了C语言中memcpy 函数的用法详解的相关资料,需要的朋友可以参考下...2020-04-25
  • C语言中查找字符在字符串中出现的位置的方法

    这篇文章主要介绍了C语言中查找字符在字符串中出现的位置的方法,分别是strchr()函数和strrchr()函数的使用,需要的朋友可以参考下...2020-04-25
  • C语言菜鸟基础教程之a++与++a

    很多同学在学习c语言的时候是不是会碰到a++和++a都有甚么作用啊。今天我们就来探讨下...2020-04-25
  • 详解C语言中const关键字的用法

    这篇文章主要对C语言中const关键字的用法进行了详细的分析介绍,需要的朋友可以参考下...2020-04-25
  • C语言实现时间戳转日期的算法(推荐)

    下面小编就为大家带来一篇C语言实现时间戳转日期的算法(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-04-25
  • C语言之整数划分问题(递归法)实例代码

    这篇文章主要介绍了C语言之整数划分问题(递归法)实例代码的相关资料,需要的朋友可以参考下...2020-04-25
  • c实现linux下的数据库备份

    本文给大家简单介绍下c实现linux下的数据库备份的方法和具体的源码,十分的实用,有需要的小伙伴可以参考下。...2020-04-25