[C language] Simple score management system (Codeblocks)

[C language] Simple score management system (Codeblocks)


Preface

The school’s program design exercises, as soon as the school starts, let us make a simple score management system. Everyone is stupid.
This project is divided into five different files to achieve. The following will post one by one
. The file format in the score.txt is:
(for example )
Student number grade score
01 A 100


ps: only for learning and communication reference

1. Main menu program

The code is as follows (using switch to call each module):

/*实验内容:

—已知59人的程序设计基础课程成绩,
完成:

(1)定义数组存储成绩

(2)从文件读取数据实现存储

(3)按照成绩升序排列输出成绩

(4)按照优、良、中、及格、不及格进行分类输出成绩。

(5)输入一等级,输出该等级内的所有成绩。

(6)设计主程序菜单,完成上述各算法的测试调用

—实验要求:

    完成上述问题的:算法分析与设计,程序编写和测试。

    并提交最终实验报告和程序源码。*/



/* ※@rain_HYS※ 编写,仅供参考学习交流还有交作业使用 */



#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#define N 59                     /*总人数不超过59*/
void SaveScore();
void RankScore();
void ClassifyScore();
void SearchScore();

struct student    /*定义结构体存储成绩*/
{
    
    
    int num;
    char stname[20];
    int score;
}stu[N];

int main(void)
{
    
    
    int allnum;                   /*主程序读取用户输入数字*/
    char tf;
    printf("\n-------      BUA ※ 成绩管理系统 ※ rain_hys编写        -------\n"
           "\n-------            该程序目前具有以下功能:             -------\n"
           "\n-------             (1)从文件存入成绩                 -------\n"
           "\n-------        (2)按照成绩升序排列输出成绩            -------\n"
           "\n-------(3)按照优、良、中、及格、不及格进行分类输出成绩 -------\n"
           "\n-------    (4)输入一等级,输出该等级内的所有成绩      -------\n");                 /*程序菜单,后期可改*/
    do
    {
    
    
        printf("\n请输入想要运行的程序的数字编号:\n");
        scanf("%d",&allnum);
        switch(allnum)
        {
    
    
        case 1:
            SaveScore();
            break;
        case 2:
            RankScore();
            break;
        case 3:
            ClassifyScore();
            break;
        case 4:
            SearchScore();
            break;
        case 813:
            printf("@rain_HYS\n"
                   "恭喜你发现彩蛋!!\n");
            break;
        default:
            printf("输入错误字符,请重新运行程序!\n");
            break;
        }
        printf("是否重新运行程序?(Y&N)\n");
        scanf("%s",&tf);
    }
    while(tf == 'Y' || tf == 'y');
    return 0;
}

2. Save data from file

code show as below:

#include <stdio.h>
#include <stdlib.h>
#define N 59

struct student    /*定义结构体存储成绩*/
{
    
    
    int num;
    char stname[20];
    int score;
}stu[N];                     /*总人数不超过59*/

void SaveScore()
{
    
    
    FILE *fp;        /*定义头文件*/
    int i;
    if((fp=fopen("成绩.txt","r"))==NULL)     /*打开路径文件*/
    {
    
    
        printf("文件打开失败,请检查输入路径是否正确!\n");
        exit (0);
    }
    for(i=0; i<N; i++)     /*文件处理(逐个读入和处理数据)*/
    {
    
    
        fscanf(fp,"%d %s %d",&stu[i].num, &stu[i].stname, &stu[i].score);          /*从文件读入成绩保存到变量*/
        printf("%d %s %d\n",stu[i].num, stu[i].stname, stu[i].score);             /*输出成绩到屏幕*/
    }
    printf("成绩录入完毕!\n");

3. Sort the output results in ascending order (bubble sort)

The code is as follows (drawing from the bubbling sorting algorithm of the CSDN boss, and finally successfully written it ※ gratifying, congratulating, gratifying, gratifying ※):

#include<stdio.h>
#include<stdlib.h>
#define N 59

struct student    /*学生结构体*/
{
    
    
    int num;
    char stname[20];
    int score;
} stu[N];

void RankScore()
{
    
    
    FILE *fp;          /*定义头文件*/
    int i,j,k;        /*定义整型变量*/
    struct student t;
    if(stu[1].num == 0)              /*提示用户先运行程序:从文件存入成绩*/
    {
    
    
        printf("Warning!\n"
               "你还未存入文件!\n"
               "请先运行功能1:从文件存入成绩\n");
        return 0;
    }
    printf("成绩由小到大排序:\n");
    for(i=0; i<N-1; i++)                    /*外层for循环*/
    {
    
    
        k=i;                                /*把i的值赋给k*/
        for(j=i+1; j<N; j++)            /*内层for循环*/
        {
    
    
            if(stu[j].score<stu[k].score)/*挑出分数低的*/
            {
    
    
                k=j;            /*把相应的j赋值给k*/
            }
        }
        t=stu[k];                /*把成绩低的放到前面*/
        stu[k]=stu[i];
        stu[i]=t;
    }
    for(i=0; i<N; i++)             /*循环输出成绩*/
    {
    
    
        printf("%d %s %d\n",stu[i].num,stu[i].stname,stu[i].score);
    }
}

4. Classify and output the results according to excellent, good, intermediate, pass, and fail

code show as below:

#include<stdio.h>
#include<stdlib.h>
#define N 59

struct student    /*学生结构体*/
{
    
    
    int num;
    char stname[20];
    int score;
} stu[N];

void ClassifyScore()
{
    
    
    FILE *fp;          /*定义头文件*/
    int i;
    if(stu[1].num == 0)              /*提示用户先运行程序:从文件存入成绩*/
    {
    
    
        printf("Warning!\n"
               "你还未存入文件!\n"
               "请先运行功能1:从文件存入成绩\n");
        return 0;
    }
    printf("输出优秀名单:\n");
    for(i=0; i<N; i++)
    {
    
    
        if(stu[i].score >= 90)
        {
    
    
            printf("%d %s %d\n",stu[i].num, stu[i].stname, stu[i].score);
        }
    }
    printf("输出良好名单:\n");
    for(i=0; i<N; i++)
    {
    
    
        if(stu[i].score >= 80 && stu[i].score < 90)
        {
    
    
            printf("%d %s %d\n",stu[i].num, stu[i].stname, stu[i].score);
        }
    }
    printf("输出中等名单:\n");
    for(i=0; i<N; i++)
    {
    
    
        if(stu[i].score >= 70 && stu[i].score < 80)
        {
    
    
            printf("%d %s %d\n",stu[i].num, stu[i].stname, stu[i].score);
        }
    }
    printf("输出及格名单:\n");
    for(i=0; i<N; i++)
    {
    
    
        if(stu[i].score >= 60 && stu[i].score < 70)
        {
    
    
            printf("%d %s %d\n",stu[i].num, stu[i].stname, stu[i].score);
        }
    }
    printf("输出不及格名单:\n");
    for(i=0; i<N; i++)
    {
    
    
        if(stu[i].score < 60)
        {
    
    
            printf("%d %s %d\n",stu[i].num, stu[i].stname, stu[i].score);
        }
    }
}

5. Enter a level and output all the results in that level

code show as below:

#include<stdio.h>
#include<stdlib.h>
#define N 59

struct student    /*学生结构体*/
{
    
    
    int num;
    char stname[20];
    int score;
} stu[N];

void SearchScore()
{
    
    
    FILE *fp;          /*定义头文件*/
    int i;
    char usernum;     /*用户的输入字母*/
    if(stu[1].num == 0)              /*提示用户先运行程序:从文件存入成绩*/
    {
    
    
        printf("Warning!\n"
               "你还未存入文件!\n"
               "请先运行功能1:从文件存入成绩\n");
        return 0;
    }
    printf("请输入想要查找的等级:(从A~E)\n");
    scanf("%s",&usernum);

    if(usernum == 'A' || usernum == 'a')
    {
    
    
        printf("输出优秀名单:\n");
        for(i=0; i<N; i++)
        {
    
    
            if(stu[i].score >= 90)
            {
    
    
                printf("%d %s %d\n",stu[i].num, stu[i].stname, stu[i].score);
            }
        }
    }

    else if(usernum == 'B'|| usernum == 'b')
    {
    
    
        printf("输出良好名单:\n");
        for(i=0; i<N; i++)
        {
    
    
            if(stu[i].score >= 80 && stu[i].score < 90)
            {
    
    
                printf("%d %s %d\n",stu[i].num, stu[i].stname, stu[i].score);
            }
        }
    }

    else if(usernum == 'C'|| usernum == 'c')
    {
    
    
        printf("输出中等名单:\n");
        for(i=0; i<N; i++)
        {
    
    
            if(stu[i].score >= 70 && stu[i].score < 80)
            {
    
    
                printf("%d %s %d\n",stu[i].num, stu[i].stname, stu[i].score);
            }
        }
    }

    else if(usernum == 'D'|| usernum == 'd')
    {
    
    
        printf("输出及格名单:\n");
        for(i=0; i<N; i++)
        {
    
    
            if(stu[i].score >= 60 && stu[i].score < 70)
            {
    
    
                printf("%d %s %d\n",stu[i].num, stu[i].stname, stu[i].score);
            }
        }
    }

    else if(usernum == 'E'|| usernum == 'e')
    {
    
    
        printf("输出不及格名单:\n");
        for(i=0; i<N; i++)
        {
    
    
            if(stu[i].score < 60)
            {
    
    
                printf("%d %s %d\n",stu[i].num, stu[i].stname, stu[i].score);
            }
        }
    }

    else
    {
    
    
        printf("输入错误字符,请重新输入!\n");
    }

}



to sum up

Successfully run! Insert picture description here
It's not easy. It's not easy
. Only the screenshots of function 1 will be posted here.
Thank you CSDN big guy @份大碗宽面 for your inspiration!
Learning to code is really not an easy task, if there is something badly written, please forgive me

Guess you like

Origin blog.csdn.net/rain_HYS/article/details/115270960