今天考完试了,有点无聊,随意写了个成绩管理系统,正常使用没啥问题,但就是不能保存和不能识别误输入,bug可能也还有一些,慢慢改吧

写了主要是想锻炼一下综合代码能力,写完之后发现自己的水平实在是太低,这个系统的效率也是非常的低下,代码十分冗长复杂(长达711行),也只实现了非常基础的功能,希望大佬勿喷……

代码就放这了,希望不要滥用嗷~

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

typedef struct subject
{
    char name[20]; //科目名称
    double point; //学分
}SUBJECT;

typedef struct student
{
    char name[20]; //学生姓名
    char id[20]; //学号
    double score[20]; //成绩单
}STUDENT;

void resizecmd();
void initialize();
void setcmdcolor();
void resetcmdcolor();
void function();
void function1();
void function2();
void function3();
void function4();
void function5();
void function6();

STUDENT student[100];
SUBJECT subject[20];
int studentnum = 0;
int subjectnum = 0;
int nightmode = 0; //0为非夜间模式,1为夜间模式

void resizecmd()
{
    system("mode 120,30");
}

void setcmdcolor()
{
    system("COLOR f0");
    nightmode = 0;
}

void resetcmdcolor()
{
    system("COLOR 0f");
    nightmode = 1;
}

void initialize(int first)
{
    if (first)
    {
        resizecmd();
        setcmdcolor();
    }
    system("cls");
    printf("\n\n\n\n\n");
    printf("         ┌────────────────────────────────────────────────────────────────────────────────────────────────────┐\n");
    printf("         │                                                                                                    │\n");
    printf("         │                                        学生信息管理系统                                            │\n");
    printf("         │                                            Ver.1.0                                                 │\n");
    printf("         │                                       (退出后信息会丢失)                                           │\n");
    printf("         │                                                                                                    │\n");
    printf("         │                                                                                                    │\n");
    printf("         │                                                                                                    │\n");
    printf("         │                  1.录入学生信息                                2.查看学生列表                      │\n");
    printf("         │                  3.录入学生成绩                                4.统计学生成绩                      │\n");
    if (nightmode)
    {
        printf("         │                  5.关闭夜间模式                                6.科目管理                          │\n");
    }
    else
    {
        printf("         │                  5.打开夜间模式                                6.科目管理                          │\n");
    }
    printf("         │                  7.退出系统                                                                        │\n");
    printf("         │                                                                                                    │\n");
    printf("         │                                                                                                    │\n");
    printf("         │                                                                                                    │\n");
    printf("         │                                   (请输入选择你要进行的操作)                                       │\n");
    printf("         │                                                                                                    │\n");
    printf("         └────────────────────────────────────────────────────────────────────────────────────────────────────┘\n");
    function();
}

void function()
{
    int operation;
    scanf("%d", &operation);
    switch (operation)
    {
        case 1: function1(); break;
        case 2: function2(); break;
        case 3: function3(); break;
        case 4: function4(); break;
        case 5: function5(); break;
        case 6: function6(); break;
        case 7: system("exit"); return;
    }
    initialize(0);
}

void function1()
{
    system("cls");
    printf("请输入学生姓名,输入exit停止录入: ");
    char name[20];
    scanf("%s", name);
    if (strcmp(name, "exit") == 0)
    {
        return;
    }
    int i, place = -1;
    for (i = 0; i < studentnum; i++)
    {
        if (strcmp(name, student[i].name) == 0)
        {
            place = i;
        }
    }
    if (place != -1)
    {
        printf("系统中已存在此姓名:%s,学号为%s,请确认是否继续录入\n1.继续录入\t2.重新录入 ", student[place].name, student[place].id);
        int tmp;
        scanf("%d", &tmp);
        while (tmp != 1 && tmp != 2)
        {
            printf("输入错误,请重新输入,请确认是否继续录入\n1.继续录入\t2.重新录入 ");
            scanf("%d", &tmp);
        }
        if (tmp == 2)
        {
            function1();
            return;
        }
    }
    char id[20];
    printf("请输入学号: ");
    scanf("%s", id);
    place = -1;
    for (i = 0; i < studentnum; i++)
    {
        if (strcmp(id, student[i].id) == 0)
        {
            place = i;
        }
    }
    while (place != -1)
    {
        printf("系统中已存在此学号:%s,姓名为%s,请重新输入: ", student[place].id, student[place].name);
        scanf("%s", id);
        place = -1;
        for (i = 0; i < studentnum; i++)
        {
            if (strcmp(id, student[i].id) == 0)
            {
                place = i;
            }
        }
    }
    strcpy(student[studentnum].name, name);
    strcpy(student[studentnum].id, id);
    for (i = 0; i < 20; i++)
    {
        student[studentnum].score[i] = -1;
    }
    studentnum++;
    function1();
}

void function2()
{
    system("cls");
    printf("姓名\t\t学号\n");
    int i, j;
    for (i = 0; i < studentnum; i++)
    {
        for (j = 0; j < studentnum - i - 1; j++)
        {
            if (strcmp(student[j].id, student[j + 1].id) > 0)
            {
                struct student tmp;
                tmp = student[j];
                student[j] = student[j + 1];
                student[j + 1] = tmp;
            }
        }
    }
    for (i = 0; i < studentnum; i++)
    {
        printf("%s\t\t%s\n", student[i].name, student[i].id);
    }
    printf("是否需要修改信息?\n1.是\t2.否 ");
    int tmp;
    scanf("%d", &tmp);
    while (tmp != 1 && tmp != 2)
    {
        printf("输入错误,请重新输入,是否需要修改信息?\n1.是\t2.否 ");
        scanf("%d", &tmp);
    }
    if (tmp == 1)
    {
        printf("请输入需要修改的学生学号: ");
        char id[20];
        scanf("%s", id);
        int place = -1;
        for (i = 0; i < studentnum; i++)
        {
            if (strcmp(id, student[i].id) == 0)
            {
                place = i;
            }
        }
        while (place == -1)
        {
            printf("未找到该学号,请重新输入: ");
            scanf("%s", id);
            place = -1;
            for (i = 0; i < studentnum; i++)
            {
                if (strcmp(id, student[i].id) == 0)
                {
                    place = i;
                }
            }
        }
        printf("查找成功,原姓名为%s,如需修改,请输入新姓名,如无需修改,请输入0,如需删除,请输入del: ", student[place].name);
        char name[20];
        scanf("%s", name);
        if (strcmp(name, "del") == 0)
        {
            int d;
            for (d = place; d < studentnum - 1; d++)
            {
                student[d] = student[d + 1];
            }
            studentnum--;
            printf("删除成功,按任意键退出");
            getchar();
            getchar();
            return;
        }
        if (strcmp(name, "0") != 0)
        {
            strcpy(student[place].name, name);
        }
        printf("原学号为%s,如需修改,请输入新学号,如无需修改,请输入0: ", student[place].id);
        scanf("%s", id);
        if (strcmp(id, "0") != 0)
        {
            int pplace = -1, j;
            for (j = 0; j < studentnum; j++)
            {
                if (strcmp(id, student[i].id) == 0)
                {
                    pplace = j;
                }
            }
            while (pplace == -1)
            {
                printf("系统中已存在此学号:%s,姓名为%s,请重新输入: ", student[pplace].id, student[pplace].name);
                scanf("%s", id);
                pplace = -1;
                for (j = 0; j < studentnum; j++)
                {
                    if (strcmp(id, student[i].id) == 0)
                    {
                        pplace = j;
                    }
                }
            }
            strcpy(student[place].id, id);
        }
        printf("修改成功,修改后姓名为%s,学号为%s,按任意键退出", student[place].name, student[place].id);
        getchar();
        getchar();
    }
}

void function3()
{
    system("cls");
    printf("请输入录入的学生学号,输入exit退出录入: ");
    char id[20];
    scanf("%s", id);
    if (strcmp(id, "exit") == 0)
    {
        return;
    }
    int i, place = -1;
    for (i = 0; i < studentnum; i++)
    {
        if (strcmp(id, student[i].id) == 0)
        {
            place = i;
        }
    }
    while (place == -1)
    {
        printf("未找到该学生,请重新输入学号: ");
        scanf("%s", id);
        if (strcmp(id, "exit") == 0)
        {
            return;
        }
        place = -1;
        for (i = 0; i < studentnum; i++)
        {
            if (strcmp(id, student[i].id) == 0)
            {
                place = i;
            }
        }
    }
    if (subjectnum == 0)
    {
        printf("您还未录入科目,请先到主目录选择科目管理功能录入科目,按回车退出");
        getchar();
        getchar();
        return;
    }
    for (i = 0; i < studentnum; i++)
    {
        printf("请输入 %s 的 %s 成绩: ", student[place].name, subject[i].name);
        scanf("%lf", &student[place].score[i]);
    }
    printf("录入成功,按回车继续");
    getchar();
    getchar();
    function3();
    return;
}

void function4()
{
    system("cls");
    printf("请输入统计内容: 1.输出总成绩 2.输出单科成绩 3.输出学分加权总成绩 4.退出统计");
    int tmp;
    scanf("%d", &tmp);
    if (tmp == 4)
    {
        return;
    }
    else if (tmp == 1)
    {
        double *sum = (double *)malloc(sizeof(double) * studentnum);
        int i, j;
        for (i = 0; i < studentnum; i++)
        {
            sum[i] = 0;
            for (j = 0; j < subjectnum; j++)
            {
                sum[i] += student[i].score[j];
            }
        }
        system("cls");
        printf("姓名\t\t学号\t\t\t总成绩\n");
        for (i = 0; i < studentnum; i++)
        {
            printf("%s\t\t%s\t\t\t%.2lf\n", student[i].name, student[i].id, sum[i]);
        }
        printf("请输入操作: 1.从高到低排序 2.从低到高排序 3.退出 ");
        int tmp2;
        scanf("%d", &tmp2);
        while(tmp2 != 3)
        {
            if (tmp2 == 1)
            {
                int a, b;
                for(a = 0; a < studentnum; a++)
                {
                    for (b = 0; b < subjectnum - a - 1; b++)
                    {
                        if (sum[b] < sum[b + 1])
                        {
                            STUDENT tmps;
                            tmps = student[b];
                            student[b] = student[b + 1];
                            student[b + 1] = tmps;
                            double tmpsum = sum[b];
                            sum[b] = sum[b + 1];
                            sum[b + 1] = tmpsum;
                        }
                    }
                }
                system("cls");
                printf("姓名\t\t学号\t\t\t总成绩\t\t排名\n");
                for (i = 0; i < studentnum; i++)
                {
                    printf("%s\t\t%s\t\t\t%.2lf\t\t%d\n", student[i].name, student[i].id, sum[i], i + 1);
                }
                printf("请输入操作: 1.从高到低排序 2.从低到高排序 3.退出 ");
                scanf("%d", &tmp2);
            }
            else if (tmp2 == 2)
            {
                int a, b;
                for(a = 0; a < studentnum; a++)
                {
                    for (b = 0; b < subjectnum - a - 1; b++)
                    {
                        if (sum[b] > sum[b + 1])
                        {
                            STUDENT tmps;
                            tmps = student[b];
                            student[b] = student[b + 1];
                            student[b + 1] = tmps;
                            double tmpsum = sum[b];
                            sum[b] = sum[b + 1];
                            sum[b + 1] = tmpsum;
                        }
                    }
                }
                system("cls");
                printf("姓名\t\t学号\t\t\t总成绩\n");
                for (i = 0; i < studentnum; i++)
                {
                    printf("%s\t\t%s\t\t\t%.2lf\n", student[i].name, student[i].id, sum[i]);
                }
                printf("请输入操作: 1.从高到低排序 2.从低到高排序 3.退出 ");
                scanf("%d", &tmp2);
            }
        }
    }
    else if (tmp == 2)
    {
        int sub;
        printf("请输入你要统计的科目(科目前数字): \n");
        int i;
        for (i = 0; i < subjectnum; i++)
        {
            printf("%d.%s\t", i + 1, subject[i].name);
        }
        scanf("%d", &sub);
        sub--;
        printf("姓名\t\t学号\t\t\t成绩\n");
        for (i = 0; i < studentnum; i++)
        {
            printf("%s\t\t%s\t\t\t%.2lf\n", student[i].name, student[i].id, student[i].score[sub]);
        }
        printf("请输入操作: 1.从高到低排序 2.从低到高排序 3.退出 ");
        int tmp2;
        scanf("%d", &tmp2);
        while (tmp2 != 3)
        {
            if (tmp2 == 1)
            {
                int a, b;
                for(a = 0; a < studentnum; a++)
                {
                    for (b = 0; b < subjectnum - a - 1; b++)
                    {
                        if (student[b].score[sub] < student[b + 1].score[sub])
                        {
                            STUDENT tmps;
                            tmps = student[b];
                            student[b] = student[b + 1];
                            student[b + 1] = tmps;
                        }
                    }
                }
                system("cls");
                printf("姓名\t\t学号\t\t\t成绩\t\t排名\n");
                for (i = 0; i < studentnum; i++)
                {
                    printf("%s\t\t%s\t\t\t%.2lf\t\t%d\n", student[i].name, student[i].id, student[i].score[sub], i + 1);
                }
                printf("请输入操作: 1.从高到低排序 2.从低到高排序 3.退出 ");
                scanf("%d", &tmp2);
            }
            else if (tmp2 == 2)
            {
                int a, b;
                for(a = 0; a < studentnum; a++)
                {
                    for (b = 0; b < subjectnum - a - 1; b++)
                    {
                        if (student[b].score[sub] > student[b + 1].score[sub])
                        {
                            STUDENT tmps;
                            tmps = student[b];
                            student[b] = student[b + 1];
                            student[b + 1] = tmps;
                        }
                    }
                }
                system("cls");
                printf("姓名\t\t学号\t\t\t成绩\n");
                for (i = 0; i < studentnum; i++)
                {
                    printf("%s\t\t%s\t\t\t%.2lf\n", student[i].name, student[i].id, student[i].score[sub]);
                }
                printf("请输入操作: 1.从高到低排序 2.从低到高排序 3.退出 ");
                scanf("%d", &tmp2);
            }
        }
    }
    else if (tmp == 3)
    {
        double *sum = (double *)malloc(sizeof(double) * studentnum);
        int i, j;
        double bal = 0;
        for (i = 0; i < 20; i++)
        {
            bal += subject[i].point;
        }
        for (i = 0; i < studentnum; i++)
        {
            sum[i] = 0;
            for (j = 0; j < subjectnum; j++)
            {
                sum[i] += student[i].score[j] * (subject[j].point / bal);
            }
        }
        system("cls");
        printf("姓名\t\t学号\t\t\t总成绩\n");
        for (i = 0; i < studentnum; i++)
        {
            printf("%s\t\t%s\t\t\t%.2lf\n", student[i].name, student[i].id, sum[i]);
        }
        printf("请输入操作: 1.从高到低排序 2.从低到高排序 3.退出 ");
        int tmp2;
        scanf("%d", &tmp2);
        while(tmp2 != 3)
        {
            if (tmp2 == 1)
            {
                int a, b;
                for(a = 0; a < studentnum; a++)
                {
                    for (b = 0; b < subjectnum - a - 1; b++)
                    {
                        if (sum[b] < sum[b + 1])
                        {
                            STUDENT tmps;
                            tmps = student[b];
                            student[b] = student[b + 1];
                            student[b + 1] = tmps;
                            double tmpsum = sum[b];
                            sum[b] = sum[b + 1];
                            sum[b + 1] = tmpsum;
                        }
                    }
                }
                system("cls");
                printf("姓名\t\t学号\t\t\t总成绩\t\t排名\n");
                for (i = 0; i < studentnum; i++)
                {
                    printf("%s\t\t%s\t\t\t%.2lf\t\t%d\n", student[i].name, student[i].id, sum[i], i + 1);
                }
                printf("请输入操作: 1.从高到低排序 2.从低到高排序 3.退出 ");
                scanf("%d", &tmp2);
            }
            else if (tmp2 == 2)
            {
                int a, b;
                for(a = 0; a < studentnum; a++)
                {
                    for (b = 0; b < subjectnum - a - 1; b++)
                    {
                        if (sum[b] > sum[b + 1])
                        {
                            STUDENT tmps;
                            tmps = student[b];
                            student[b] = student[b + 1];
                            student[b + 1] = tmps;
                            double tmpsum = sum[b];
                            sum[b] = sum[b + 1];
                            sum[b + 1] = tmpsum;
                        }
                    }
                }
                system("cls");
                printf("姓名\t\t学号\t\t\t总成绩\n");
                for (i = 0; i < studentnum; i++)
                {
                    printf("%s\t\t%s\t\t\t%.2lf\n", student[i].name, student[i].id, sum[i]);
                }
                printf("请输入操作: 1.从高到低排序 2.从低到高排序 3.退出 ");
                scanf("%d", &tmp2);
            }
        }
    }
}

void function5()
{
    if (nightmode)
    {
        setcmdcolor();
    }
    else
    {
        resetcmdcolor();
    }
}

void function6()
{
    system("cls");
    printf("已有科目数:%d\n科目\t学分\n", subjectnum);
    int i;
    for (i = 0; i < subjectnum; i++)
    {
        printf("%s\t%.1lf\n", subject[i].name, subject[i].point);
    }
    printf("请输入操作: 1.增加科目 2.删除科目 3.退出科目管理");
    int tmp;
    scanf("%d", &tmp);
    while (tmp != 1 && tmp != 2 && tmp != 3)
    {
        printf("输入有误,请输入操作: 1.增加科目 2.删除科目 3.退出科目管理");
        scanf("%d", &tmp);
    }
    if (tmp == 1)
    {
        char sub[20];
        printf("请输入科目名称: ");
        scanf("%s", sub);
        int j, flag = 0;
        for (j = 0; j < subjectnum; j++)
        {
            if (strcmp(sub, subject[j].name) == 0)
            {
                flag = 1;
                printf("科目有重复,请重新输入: ");
                scanf("%s", sub);
            }
        }
        while(flag)
        {
            flag = 0;
            for (j = 0; j < subjectnum; j++)
            {
                if (strcmp(sub, subject[j].name) == 0)
                {
                    flag = 1;
                    printf("科目有重复,请重新输入: ");
                    scanf("%s", sub);
                }
            }
        }
        printf("请输入科目 %s 的学分: ", sub);
        int point;
        scanf("%d", &point);
        subject[subjectnum].point = point;
        strcpy(subject[subjectnum++].name, sub);
        printf("添加成功,输入任意键继续");
        getchar();
        getchar();
        function6();
        return;
    }
    else if (tmp == 2)
    {
        printf("请输入需要删除科目,无需删除请输exit: ");
        char sub[20];
        scanf("%s", sub);
        if (strcmp(sub, "exit") != 0)
        {
            int place = -1;
            for (i = 0; i < subjectnum; i++)
            {
                if (strcmp(subject[i].name, sub) == 0)
                {
                    place = i;
                }
            }
            if (place == -1)
            {
                printf("未找到科目,删除失败,按任意键继续");
                getchar();
                getchar();
                function6();
                return;
            }
            else
            {
                for (i = place; i < subjectnum; i++)
                {
                    subject[i] = subject[i + 1];
                }
                subjectnum--;
                printf("删除成功,按任意键继续");
                getchar();
                getchar();
                function6();
                return;
            }
        }
        else
        {
            function6();
            return;
        }
    }
    else
    {
        return;
    }
}

int main()
{
    initialize(1);
    return 0;
}
分类: 算法