Student performance management system (C language has structure implementation)

This article introduces a simple student management system implemented by C language. If the system uses structure arrays to manage student performance data, the practice of this system can have a better understanding of the relevant knowledge of the structure and improve logical thinking ability.

1. Functions to be realized

1. First run

The system runs, and the following interface opens. List the system help menu (that is, the command menu), prompting for commands

p9qbzZj.png

At the beginning, the grades have not been entered, so the command L cannot list the grades. It should prompt "The grade list is empty! Please use the command T to enter the student grades first." Similarly, when inputting other grade processing commands, it will also be processed accordingly.

p9qqRkn.png

2. Record entry

Enter the command T and call the Type subfunction to record the grades. The interface prompts to enter the number of students

p9qq5lT.png

Enter 3, prompt to enter the grades of 3 courses of 3 students, list the header of the report card "Student Number Chinese Mathematics English", and prompt the student number: 1

p9qqH0J.png

Enter the 3 course grades of student No. 1, separated by spaces, and press Enter to end. Prompt student number: 2

p9qLktI.png

Enter the 3 course grades of student No. 2, separated by spaces, and press Enter to end. Prompt student number: 3

p9qLec8.png

Enter the 3 course grades of student No. 3, separated by spaces, and press Enter to end. The Type sub-function call ends and returns. Prompts for commands.

p9qLlAs.png

3. Display the entered grades

Input the command L and call the List subfunction to output the score sheet. The call of the List subfunction ends and returns. Prompts for commands.

p9qOlrD.png

4. Calculate the average

Enter command A, call the Average subfunction to calculate the average score, and prompt "The average score has been calculated. Please use command L to view it." The Average subfunction call ends and returns. Prompts for commands.

p9qOrZQ.png

Input the command L and call the List subfunction to output the score sheet. The call of the List subfunction ends and returns. Prompt for commands

p9qORzV.png

5. Sort average score

Input the command P, call the Sort sub-function to sort the records of each student according to the average score from high to low, and prompt "Sorting is completed. Please use command L to view." Sort sub-function call ends and returns. Prompts for commands.

Input the command L and call the List subfunction to output the score sheet. The call of the List subfunction ends and returns. Prompts for commands.

p9qXEy8.png

6. Query student grades

Enter the command S, call the Search sub-function to query the student's grades, and prompt "Enter the student number to be queried".

Enter 2 to find the grade of student No. 2 and output it. The Search sub-function is called and returns. Prompts for commands.

p9qXQWq.png

7. Qingping

Enter the command C to execute the clear screen function statement system("clear");

p9qXGOU.png

8. Show help menu

Enter the command H to call the Help subfunction to display the help menu. The help sub-function is called and returns. Prompts for commands.

p9qXdYR.png

9. System

Enter the command Q to exit the system.

p9qxYm8.png

Two, implement the code

(1) All codes in one file (v1)

main.c

#include <stdio.h>
#include <stdlib.h>

struct student
{
    
    
    int num;
    float chinese;
    float math;
    float waiyu;
    float average;
};
/**********函数声明*********/
void Help();//帮助菜单显示函数
void Type(struct student *pstu);//成绩录入
void List(struct student *pstu);//输出成绩表
void Averange(struct student *pstu);//平均分
void Sort(struct student *pstu);//按平均分由高到底排序
void Search(struct student *pstu); //查询学生成绩
void Clear();   //清屏


int manNum = 0;//学生数

int main()
{
    
    

    char opt;   //命令变量
    Help();//首次运行显示帮助菜单
    struct student stu[10];//定义结构体数组


    while (1)
    {
    
    
        printf("\n");
        printf("请输入命令=");
        if(scanf("%c",&opt)){
    
    
            //printf("scanf出现EOF错误!\n");
            getchar();//吃掉空格
        }
        switch (opt)
        {
    
    
            case 'H':Help();break;//显示帮助菜单
            case 'T':Type(stu);break;//成绩录入
            case 'A':Averange(stu);;break;
            case 'L':List(stu);break;//列出成绩表
            case 'P':Sort(stu);break;//按平均成绩由高到低拍排序
            case 'S':Search(stu);break;//按学号查询学生成绩
            case 'C':Clear();break;//清屏
            case 'Q':return 0;break; //退出系统
            default:printf("输入命令有误!\n");break;
        }
    }
    return 0;
}

void Help()  //帮助菜单显示函数 H
{
    
    
    printf("\n");
    for(int i=1;i<=46;i++){
    
    
        printf("*");
    }
    printf("\n");
    printf("  *      学生成绩管理系统———帮助菜单       *  \n");
    for(int i=1;i<=46;i++){
    
    
        printf("*");
    }
    printf("\n");
    printf("  *         H = 显示帮助菜单               *  \n");
    printf("  *         T = 成绩录入                   *  \n");
    printf("  *         A = 计算学生平均分             *  \n");
    printf("  *         L = 列出成绩表                 *  \n");
    printf("  *         P = 按平均成绩由高到低排序     *  \n");
    printf("  *         S = 按学号查询学生成绩         *  \n");
    printf("  *         C = 清屏                       *  \n");
    printf("  *         Q = 退出系统                   *  \n");
    for(int i=1;i<=46;i++){
    
    
        printf("*");
    }
    printf("\n");
    printf("  * Copyright <C> 2023.05.19  By Sunqk5665 *  \n");
    for(int i=1;i<=46;i++){
    
    
        printf("*");
    }
    printf("\n");

}
void Type(struct student *pstu)//成绩录入 T
{
    
    
    printf("请输入学生人数:");
    scanf("%d",&manNum);
    printf("请输入%d名学生的三门课成绩:\n",manNum);
    printf("学号 语文 数学 外语\n");
    // for(int i = 0; i < manNum; i++){
    
    
    //     scanf("%d  %f  %f  %f",&pstu[i].num,&pstu[i].chinese,&pstu[i].math,&pstu[i].waiyu);
    //     //getchar();
    // }
    for(int i = 0; i < manNum; i++){
    
    
        pstu[i].num = i+1;
        printf("%d   ",pstu[i].num);
        scanf("%f  %f  %f",&pstu[i].chinese,&pstu[i].math,&pstu[i].waiyu);
        //getchar();
    }
    getchar();//吃最后一次输入后的回车
}
void List(struct student *pstu)//输出成绩表 L
{
    
    
    if(manNum == 0){
    
    
        printf("成绩为空!请先使用命令 T 录入学生成绩。\n");
        return;//函数到此终止
    }
    printf("学号 语文 数学 外语 平均分\n");
    for(int i=0; i<manNum; i++){
    
    
        printf(" %d   %.1f %.1f %.1f %.1f\n",pstu[i].num,pstu[i].chinese,pstu[i].math,pstu[i].waiyu,pstu[i].average);
    }
}
void Averange(struct student *pstu)//平均分 A
{
    
    
    if(manNum == 0){
    
    
        printf("成绩为空!请先使用命令 T 录入学生成绩。\n");
        return;//函数到此终止
    }
    for(int i=0; i<manNum; i++){
    
    
        pstu[i].average = (pstu[i].chinese + pstu[i].math + pstu[i].waiyu)/3;
    }
    printf("平均分已计算完,请使用命令 L 查看。\n");
}
void Sort(struct student *pstu) //按平均分由高到底排序 P
{
    
    
    if(manNum == 0){
    
    
        printf("成绩为空!请先使用命令 T 录入学生成绩。\n");
        return;//函数到此终止
    }
    struct student stTemp;//交换中转
    for(int i = 1; i < manNum; i++){
    
    
        for(int j = 0; j < manNum-i; j++){
    
    
            if (pstu[j].average < pstu[j+1].average){
    
    
                stTemp = pstu[j];
                pstu[j] = pstu[j+1];
                pstu[j+1] = stTemp;
            }
        }
    }
    printf("完成排序,请使用 L 查看。\n");
}
void Search(struct student *pstu) //按学号查询学生成绩 S
{
    
    
    if(manNum == 0){
    
    
        printf("成绩为空!请先使用命令 T 录入学生成绩。\n");
        return;//函数到此终止
    }
    int stuNum;

    printf("请输入要查询的学生学号:");
    scanf("%d",&stuNum);
    if(stuNum <= 0 || stuNum >=manNum){
    
    
        printf("输入学号有误!\n");
    }
    getchar();
    for(int i=0; i<manNum; i++){
    
    
        if(pstu[i].num == stuNum){
    
    
            printf("学号 语文 数学 外语 平均分\n");
            printf(" %d   %.1f %.1f %.1f %.1f\n",pstu[i].num,pstu[i].chinese,pstu[i].math,pstu[i].waiyu,pstu[i].average);
        }
    }   

}
void Clear()//清屏 C
{
    
    
    system("clear"); //"clear"为Linux终端清屏命令(实质是向上翻页)
}

(2) Sub-file writing (v2)

main.c

#include <stdio.h>
#include <stdlib.h>
#include "help.h"
#include "score.h"
#include "clear.h"


// int flag = 0;
int manNum = 0;//学生数
struct student stu[20]; //结构体数组定义

int main()
{
    
    
    char opt;   //命令
    Help(); //首次运行显示帮助菜单
    while (1)
    {
    
    
        printf("\n");
        printf("请输入命令=");
        if(scanf("%c",&opt)){
    
    
            //printf("scanf出现EOF错误!\n");
            getchar();//吃掉空格
        }
        switch (opt)
        {
    
    
            case 'H':Help();break;           //显示帮助菜单
            case 'T':Type(stu);break;        //成绩录入
            case 'A':Averange(stu);;break;   //计算平均分
            case 'L':List(stu);break;        //列出成绩表
            case 'P':Sort(stu);break;        //按平均成绩由高到低拍排序
            case 'S':Search(stu);break;      //按学号查询学生成绩
            case 'C':Clear();break;          //清屏
            case 'Q':return 0;break;         //退出系统
            default:
                printf("输入命令有误!\n");break;
        }

    }
    return 0;
}

help.h

#ifndef _SCORE_H_
#define __SCORE_H_
#include <stdio.h>
/****函数声明***/
void Help();//帮助菜单显示函数

#endif

clear.h

#ifndef _CLEAR_H_
#define _CLEAR_H_
#include <stdlib.h>
/****函数声明***/
void Clear();   //清屏

#endif

score.h

#ifndef _SCORE_H_
#define _SCORE_H_
#include <stdio.h>
#include <ctype.h>

/****学生结构体***/
struct student
{
    
    
    int num;
    float chinese;
    float math;
    float waiyu;
    float average;
};
/****函数声明***/
void Type(struct student *pstu);//成绩录入
void List(struct student *pstu);//输出成绩表
void Averange(struct student *pstu);//平均分
void Sort(struct student *pstu);//按平均分由高到底排序
void Search(struct student *pstu); //查询学生成绩

#endif

help.c

#include "help.h"

void Help()//帮助菜单显示函数 H
{
    
    
    printf("\n");
    for(int i=1;i<=46;i++){
    
    
        printf("*");
    }
    printf("\n");
    printf("  *      学生成绩管理系统———帮助菜单       *  \n");
    for(int i=1;i<=46;i++){
    
    
        printf("*");
    }
    printf("\n");
    printf("  *         H = 显示帮助菜单               *  \n");
    printf("  *         T = 成绩录入                   *  \n");
    printf("  *         A = 计算学生平均分             *  \n");
    printf("  *         L = 列出成绩表                 *  \n");
    printf("  *         P = 按平均成绩由高到低排序     *  \n");
    printf("  *         S = 按学号查询学生成绩         *  \n");
    printf("  *         C = 清屏                       *  \n");
    printf("  *         Q = 退出系统                   *  \n");
    for(int i=1;i<=46;i++){
    
    
        printf("*");
    }
    printf("\n");
    printf("  *   Copyright <C> 2023.05.19 By 孙启凯   *  \n");
    for(int i=1;i<=46;i++){
    
    
        printf("*");
    }
    printf("\n");
}

clear.c

#include "clear.h"

void Clear()//清屏 C
{
    
    
    system("clear"); //"clear"为Linux终端清屏命令(实质是向上翻页)
}

score.c

#include "score.h"
extern int manNum;

void Type(struct student *pstu)//成绩录入 T
{
    
    
    printf("请输入学生人数:");
    scanf("%d",&manNum);
    printf("请输入%d名学生的三门课成绩:\n",manNum);
    printf("学号 语文 数学 外语\n");

    for(int i = 0; i < manNum; i++){
    
    
        pstu[i].num = i+1;
        printf("%d   ",pstu[i].num);
        scanf("%f  %f  %f",&pstu[i].chinese,&pstu[i].math,&pstu[i].waiyu);
        //getchar();
    }
    getchar();//吃最后一次输入后的回车

}
void List(struct student *pstu)//输出成绩表 L
{
    
    
    if(manNum == 0){
    
    
        printf("成绩为空!请先使用命令 T 录入学生成绩。\n");
        return;//函数到此终止
    }
    printf("学号 语文 数学 外语 平均分\n");
    for(int i=0; i<manNum; i++){
    
    
        printf(" %d   %.1f %.1f %.1f %.1f\n",pstu[i].num,pstu[i].chinese,pstu[i].math,pstu[i].waiyu,pstu[i].average);
    }
}
void Averange(struct student *pstu)//平均分 A
{
    
    
    if(manNum == 0){
    
    
        printf("成绩为空!请先使用命令 T 录入学生成绩。\n");
        return;//函数到此终止
    }
    for(int i=0; i<manNum; i++){
    
    
        pstu[i].average = (pstu[i].chinese + pstu[i].math + pstu[i].waiyu)/3;
    }
    printf("平均分已计算完,请使用命令 L 查看。\n");
}
void Sort(struct student *pstu) //按平均分由高到底排序 P
{
    
    
    if(manNum == 0){
    
    
        printf("成绩为空!请先使用命令 T 录入学生成绩。\n");
        return;//函数到此终止
    }
    struct student stTemp;//交换中转
    for(int i = 1; i < manNum; i++){
    
    
        for(int j = 0; j < manNum-i; j++){
    
    
            if (pstu[j].average < pstu[j+1].average){
    
    
                stTemp = pstu[j];
                pstu[j] = pstu[j+1];
                pstu[j+1] = stTemp;
            }
        }
    }
    printf("完成排序,请使用 L 查看。\n");
}
void Search(struct student *pstu) //按学号查询学生成绩 S
{
    
    
    if(manNum == 0){
    
    
        printf("成绩为空!请先使用命令 T 录入学生成绩。\n");
        return;//函数到此终止
    }
    int stuNum;

    printf("请输入要查询的学生学号:");
    scanf("%d",&stuNum);
    getchar();
    if(stuNum>0 && stuNum<=manNum){
    
    
        for(int i=0; i<manNum; i++){
    
    
            if(pstu[i].num == stuNum){
    
    
                printf("学号 语文 数学 外语 平均分\n");
                printf(" %d   %.1f %.1f %.1f %.1f\n",pstu[i].num,pstu[i].chinese,pstu[i].math,pstu[i].waiyu,pstu[i].average);
            }
        }
    }
    else{
    
    
        printf("输入的学号非法,请仔细核对!!\n");
    }
}

Makefile

CC=gcc
CFLAGS=-c -g
OBJS=help.o clear.o score.o main.o
a.out:$(OBJS)
	$(CC) $(OBJS) -o $@
$(OBJS):%.o:%.c
	$(CC) $(CFLAGS) $^ -o $@
.PHONY:clean
clean:
	rm *.o a.out

Please add a picture description

Guess you like

Origin blog.csdn.net/weixin_43624626/article/details/130900392