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

前言:

有朋友最近在做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;
    }
}

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

(0)

相关推荐

  • 学生成绩管理系统C语言代码实现

    C语言实现了学生成绩管理系统,可以进行学生成绩的增加,删除,更新,查询,计算和展示. 完整代码如下: #include<stdio.h> #include<stdlib.h> #include<string.h> typedef struct person //定义结构体 { char num[10]; //学号 char name[20]; //姓名 float cyuyan; //C语言成绩 float en; //物理学成绩 float ji; //原子物理成绩

  • C语言实现学生成绩管理系统

    设计一个学生成绩管理系统,实现对学生成绩的动态管理,实现对学生成绩的输入,显示,删除,查找,插入,最佳,保存,计算,排序等主要功能. 功能要求 1.每一条记录包括一个学生的学号.姓名.3门课成绩.平均成绩. 2.输入功能:可以一次完成若干条记录的输入. 3.显示功能:完成全部学生记录的显示. 4.查找功能:完成按姓名查找学生记录,并显示. 5.排序功能:按学生平均成绩进行排序. 6.插入功能:按平均成绩高低插入一条学生记录 7.删除功能:如果输入错误,可以删除学生记录: 8.退出. 代码: #i

  • C语言编写学生成绩管理系统

    用C语言编写学生成绩管理系统代码,供大家参考,具体内容如下 C语言实现学生成绩管理系统实战教学 https://www.jb51.net/article/154767.htm (1)给出所选课程设计题目以及本题目具体所要完成的功能要求说明. 1.课程设计题目:学生成绩管理系统 2.完成的功能要求: (1).主要实现的功能: 1---学生输入 2---学生插入 3---学生查询(按学号) 4---学生删除 5---学生输出 6---计算每名学生的平均分并输出 7---计算每科的平均分并输出 8--

  • C语言结构体版学生成绩管理系统

    本文实例为大家分享了C语言实现学生成绩管理系统的具体代码,供大家参考,具体内容如下 结构体版的学生成绩管理系统 主要功能有 按1 输入学生信息 按2 输出学生信息 按3 查询学生信息 按4 修改学生信息 按5 删除学生信息 按6 插入学生信息 按7 排序总成绩信息 学生信息主要有姓名,班级,学号,C语言成绩,高数成绩,大学英语成绩,控制台界面输出格式有点不美观. #include<stdio.h> #include<string.h> struct student { char n

  • C语言利用结构体数组实现学生成绩管理系统

    要求: 某班有最多不超过30人(具体人数由键盘输入)参加期末考试,最多不超过6门(具体门数由键盘输入).定义结构体类型描述学生信息,每个学生信息包括:学号.姓名.多门课的成绩.总成绩和平均成绩.用结构体数组作为函数参数,编程实现如下菜单驱动的学生成绩管理系统. (1) 录入每个学生的学号.姓名和各科考试成绩. (2) 计算每门课程的总分和平均分. (3) 计算每个学生的总分和平均分. (4) 按每个学生的总分由高到低排出名次表. (5) 按学号由小到大排出成绩表. (6) 按姓名的字典顺序排出成

  • C语言学生成绩管理系统设计

    本文实例为大家分享了C语言学生成绩管理系统的具体代码,供大家参考,具体内容如下 Ps:后加了个链表排序,用冒泡写的. /* Title : Student's score management system Author: nyist_xiaod Date : 2012.5.8 */ #include <stdio.h> #include <conio.h> #include <string.h> #include <stdlib.h> #define Pr

  • C语言学生成绩管理系统课程设计

    学生成绩管理系统是比较适合初学者的.它涵盖了c语言几乎知识.对于学了c语言的人,把这个课程设计搞好(当然自己能设计出来是要有很好的基础).不管自己能不能够完成,最重要的是能弄懂.参考其他资料,试着自己编写是不错的选择.这个课程设计也是我参照资料,自己编写的.自己适当地增加了一些功能.不过,编的不够那么专业吧. #include<stdio.h> #include<string.h> #include<stdlib.h> #define size 100 char* cl

  • C语言学生成绩管理系统源代码

    大学C语言实训课,C语言学生成绩管理系统,供大家参考,具体内容如下 #include<stdio.h> #include<string.h> #include<math.h> struct student { int num; char name[20]; float pingshi; float shiyan; float kaoshi; double zongping; }stu[4]; void main() { void a(); void b(); void

  • C语言实现学生成绩管理系统实战教学

    趁着放假无事,开始用C语言开发一些小的项目,巩固基础知识的同时学习新的知识. 学生成绩管理系统实现的功能有:成绩录入.学生成绩查询.删除.修改.通过文件保存等. 开发这样一个系统需要具备的知识:线性表(链表).文件操作.排序(如果需要成绩排序). 开发环境为VS2015:在Linux下没有conio.h的头文件,需要修改与getch()函数相关的代码. #include <stdio.h> #include <stdlib.h> #include <string.h>

  • C语言实现简单学生成绩管理系统

    本文实例为大家分享了C语言实现学生成绩管理系统的具体代码,供大家参考,具体内容如下 C语言小项目 实现一个学生成绩管理系统 系统功能: 1.实现所有学生成绩的录入(利用结构体数组),当输入字符为end时候,结束成绩的录入:  2.实现所有学生信息的输出  3.输入指定学生姓名,并能输出这名学生的信息  4.将学生成绩按照语文和数学成绩排序 思路: 1. 首先,先把这个小项目的框架打好.考虑要写几个文件,每一个文件里面实现怎样的功能.考虑到这个小项目的代码量以及程序的易读性,我决定将写三个文件.一

随机推荐