学生成绩管理系统

#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <stdlib.h>
typedef struct student
{
    char stuCode[100];
    char stuName[50][100];
    int stuScore[3][100];
    int sumScore[100];
} STUDENT;
STUDENT stu[100];                         //学生信息的结构体数组
 
 
void menu(void);                          //供给用户选择的图形页面菜单
void input(STUDENT stu[], int n);         //输入数据:学号、姓名、各科分数
void output(STUDENT stu[], int n);        //输出数据:学号、姓名、各科分数、总分、平均分
void scoreSortLow(STUDENT stu[], int n);  //总分低到高排名
void scoreSortHigh(STUDENT stu[], int n); //总分高到低排名
void percentage(STUDENT stu[], int n);    //输出各科成绩段百分比
void searchCode(STUDENT stu[], int n);    //搜索学号
void searchName(STUDENT stu[], int n);    //搜索姓名
void stuNameSort(STUDENT stu[], int n);   //按字典顺序对学生姓名排序
void stuCodeSort(STUDENT stu[], int n);
int inputChoice(void); //输入菜单中需要实现的功能选项
int legalInput(void);  //检验合法性、输入学生人数
 
 
int inputChoice(void)  //键入需要实现的功能选项
{
    int n, check;
    printf("Input your choice:\n");
    check = scanf("%d", &n);
    while (check != 1 || n < 1 || n > 11)
    {
        while (getchar() != '\n')
            ;
        printf("Input Error! Please enter again:\n");
        check = scanf("%d", &n);
    }
    return n;
}
 
 
void menu(void)
{
    int n;
    printf("\n\n");                                     //与上次的输出结果保持间隔,便于美观
    printf(" _____________________________________\n"); //菜单输出
    printf(" |              MENU                 |\n");
    printf(" |       1.Input data                |\n");
    printf(" |       2.Output data               |\n");
    printf(" |       3.Search data               |\n");
    printf(" |       4.Search name               |\n");
    printf(" |       5.Sort high to low          |\n");
    printf(" |       6.Sort low to high          |\n");
    printf(" |       7.Sort by name              |\n");
    printf(" |       8.Sort by code              |\n");
    printf(" |       9.Percentage of courses     |\n");
    printf(" |       10.Quit                     |\n");
    printf(" |___________________________________|\n");
    printf("  Please choose your function:(1-11):"); //功能选择
    printf("\n");
}
 
 
void input(struct student stu[], int n) //读入学号姓名、各科成绩
{
    int i, j;
    for (i = 0; i < n; i++)
    {
        printf("Please input the number and name of student%d\n", i + 1);
        printf("The number should include 6 figures.(Example:110110)\n");
        scanf("%s", stu[i].stuCode);
        while (strlen(stu[i].stuCode) != 6 ||
                stu[i].stuCode[0] < '0' || stu[i].stuCode[0] > '9' ||
                stu[i].stuCode[1] < '0' || stu[i].stuCode[1] > '9' ||
                stu[i].stuCode[2] < '0' || stu[i].stuCode[2] > '9' ||
                stu[i].stuCode[3] < '0' || stu[i].stuCode[3] > '9' ||
                stu[i].stuCode[4] < '0' || stu[i].stuCode[4] > '9' ||
                stu[i].stuCode[5] < '0' || stu[i].stuCode[5] > '9')
        {
            printf("Please input a valid number! \n");
            scanf("%s", stu[i].stuCode);
        }
        scanf("%s", stu[i].stuName);
        printf("Please input the scores of student%d by the order of Chinese、mathematics and English\n", i);
        printf("Example:096 099 100(no more than 100)\n");
        scanf("%d%d%d", stu[i].stuScore[0], stu[i].stuScore[1], stu[i].stuScore[2]);
        printf("%s***%s***%d***%d***%d\n", stu[i].stuCode, stu[i].stuName, *stu[i].stuScore[0], *stu[i].stuScore[1], *stu[i].stuScore[2]);
    }
    for (i = 0; i < n; i++)
    {
        *stu[i].sumScore = *stu[i].stuScore[0] + *stu[i].stuScore[1] + *stu[i].stuScore[2];
        printf("%d\n", *stu[i].sumScore);
    }
}
 
 
void output(STUDENT stu[], int n) //输出信息
{
    int i, j, k;
    printf("____________________________________________________________________________________________________________________\n");
    printf("|                                                  Report                                                           |\n");
    printf("|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|\n");
    printf("|        ID      |    name   | Chinese grade  |    Math grade   |   English grade   |      Sum      |    Average    |\n");
    printf("|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|\n");
    for (i = 0; i < n; i++)
    {
        printf("|%11s%15s%13d%17d%18d%19.2f%19.2f\n", stu[i].stuCode,
               stu[i].stuName, *stu[i].stuScore[0], *stu[i].stuScore[1],
               *stu[i].stuScore[2],
               (*stu[i].stuScore[0] + *stu[i].stuScore[1] + *stu[i].stuScore[2]) * 1.0,
               (*stu[i].stuScore[0] + *stu[i].stuScore[1] + *stu[i].stuScore[2]) / 3.0);
    }
}
 
 
int legalInput(void) //合法化输入,防御式编程
{
    int n, check;
    printf("Input the number of students:\n");
    check = scanf("%d", &n);
    while (check != 1 || n < 1 || n > 13)
    {
        while (getchar() != '\n')
            ;
        printf("Input Error! Please enter again:\n");
        check = scanf("%d", &n);
    }
    return n;
}
//已检验
 
 
void stuNameSort(STUDENT stu[], int n) //按照姓名字典顺序排序
{
    int i, j;
    struct student temp;
    for (i = 0; i < n - 1; i++)
    {
        for (j = i + 1; j < n; j++)
        {
            if (strcmp(stu[i].stuName, stu[j].stuName) > 0)
            {
                temp = stu[i];
                stu[i] = stu[j];
                stu[j] = temp;
            }
        }
    }
    for (i = 0; i < n; i++)
    {
        printf("%s      %s\n", stu[i].stuCode, stu[i].stuName);
    }
}
 
 
void stuCodeSort(STUDENT stu[], int n) //按学号顺序排名
{
    int i, j;
    struct student temp;
    for (i = 0; i < n - 1; i++)
    {
        for (j = i + 1; j < n; j++)
        {
            if (strcmp(stu[i].stuCode, stu[j].stuCode) > 0)
            {
                temp = stu[i];
                stu[i] = stu[j];
                stu[j] = temp;
            }
        }
    }
    for (i = 0; i < n; i++)
    {
        printf("%s      %s\n", stu[i].stuCode, stu[i].stuName);
    }
}
 
 
void searchCode(STUDENT stu[], int n) //按学号查找
{
    int i, j, flag = 0;
    char key[100];
    printf("Please input the code of the student:");
    scanf("%s", key);
    for (i = 0; i < n; i++)
    {
        if (strcmp(key, stu[i].stuCode) == 0)
        {
            printf("The student is %s,%s", stu[i].stuName, stu[i].stuCode);
            flag = 1;
        }
    }
    while (flag == 0)
    {
        printf("Not found!");
        flag = 1;
    }
}
 
 
void searchName(STUDENT stu[], int n) //按姓名查找
{
    int i, j, flag = 0;
    char key[100];
    printf("Please input the name of the student:");
    scanf("%s", key);
    for (i = 0; i < n; i++)
    {
        if (strcmp(key, stu[i].stuName) == 0)
        {
            printf("The student is %s,%s\n", stu[i].stuName, stu[i].stuCode);
            flag = 1;
        }
    }
    while (flag == 0)
    {
        printf("Not found!");
        flag = 1;
    }
}
 
 
void percentage(STUDENT stu[], int n) //输出百分比
{
    float percent[3][5] = {};
    int i, j, k;
    for (i = 0; i < 3; i++)
    {
        for (j = 0; j < n; j++)
        {
            if (*stu[j].stuScore[i] < 60)
            {
                percent[i][0]++;
            }
            if (*stu[j].stuScore[i] >= 60 && *stu[j].stuScore[i] < 70)
            {
                percent[i][1]++;
            }
            if (*stu[j].stuScore[i] >= 70 && *stu[j].stuScore[i] < 80)
            {
                percent[i][2]++;
            }
            if (*stu[j].stuScore[i] >= 80 && *stu[j].stuScore[i] < 90)
            {
                percent[i][3]++;
            }
            if (*stu[j].stuScore[i] >= 90 && *stu[j].stuScore[i] <= 100)
            {
                percent[i][4]++;
            }
        }
    }
    printf("语文———————数学——————英语\n");
    printf("___________\n");
    for (i = 0; i < 3; i++)
    {
        printf("不及格:%f\n", percent[i][0] / n);
        printf("及格:%f\n", (percent[i][1] / n));
        printf("中等:%f\n", (percent[i][2] / n));
        printf("良好:%f\n", (percent[i][3] / n));
        printf("优秀:%f\n", (percent[i][4] / n));
        printf("___________\n");
    }
}
 
 
void scoreSortHigh(STUDENT stu[], int n) //总分高到低
{
    int i, j;
    STUDENT temp;
    for (i = 0; i < n - 1; i++)
    {
        for (j = i + 1; j < n; j++)
        {
            if (*stu[j].sumScore > *stu[i].sumScore)
            {
                temp = stu[i];
                stu[i] = stu[j];
                stu[j] = temp;
            }
        }
    }
    printf("The rank from high to low is:\n");
    for (i = 0; i < n; i++)
    {
        printf("no.%d:   %s\n", i + 1, stu[i].stuName);
    }
}
 
 
void scoreSortLow(STUDENT stu[], int n) //总分低到高
{
    int i, j;
    STUDENT temp;
    for (i = 0; i < n - 1; i++)
    {
        for (j = i + 1; j < n; j++)
        {
            if (*stu[j].sumScore < *stu[i].sumScore)
            {
                temp = stu[i];
                stu[i] = stu[j];
                stu[j] = temp;
            }
        }
    }
    printf("The rank from low to high is:\n");
    for (i = 0; i < n; i++)
    {
        printf("no.%d:   %s\n", i + 1, stu[i].stuName);
    }
}
 
 
int main(void)
{
    int n, m;
    n = legalInput();
    while (1)
    {
        menu();
        m = inputChoice();
        switch (m)
        {
        case 1:
            input(stu, n);
            system("pause");
            system("cls");
            break;
        case 2:
            output(stu, n);
            system("pause");
            system("cls");
            break;
        case 3:
            searchCode(stu, n);
            system("pause");
            system("cls");
            break;
        case 4:
            searchName(stu, n);
            system("pause");
            system("cls");
            break;
        case 5:
            scoreSortHigh(stu, n);
            system("pause");
            system("cls");
            break;
        case 6:
            scoreSortLow(stu, n);
            system("pause");
            system("cls");
            break;
        case 7:
            stuNameSort(stu, n);
            system("pause");
            system("cls");
            break;
        case 8:
            stuCodeSort(stu, n);
            system("pause");
            system("cls");
            break;
        case 9:
            percentage(stu, n);
            system("pause");
            system("cls");
            break;
        case 10:
            printf("Thanks for using!(感谢相遇捏!天天开心哦!) XD~");
            system("pause");
            return 0;
        default:
            return 0;
        }
    }
    return 0;
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值