栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

创建动态链表设计简单学生成绩信息管理系统

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

创建动态链表设计简单学生成绩信息管理系统

一、尾插法创建动态链表
二、(1)学生链表;(2)班级链表
1.1学生结构体

struct Student
{
        int Chinese_score;
        int Math_score;
        int English_score;
        int total_three_score;
        float ave_three_score;
        char *name;
        struct Student *next;
};

1.2尾插法创建学生动态链表

struct Student *insertStudentBehindHead(struct Student *head, struct Student *new)
{
        struct Student *p = head;
        if (p == NULL){
                head = new;
                return head;
        }

        while(p->next != NULL){
                p = p->next;
        }
        p->next = new;

        return head;
}

struct Student *createStudentLinkBehindHead(struct Class *head1, struct Student *head2)
{
        struct Student *new = NULL;
        int j;

        for(j = 0; j < head1->num_stu; j++){
                new = (struct Student *)malloc(sizeof(struct Student));
                new->name = (char *)malloc(128);

                getchar();
                printf("请输入第%d个学生姓名:n", (j+1));
                scanf("%s", new->name);
                printf("请输入语文成绩:n");
                scanf("%d", &(new->Chinese_score));
                printf("请输入数学成绩:n");
                scanf("%d", &(new->Math_score));
                printf("请输入英语成绩:n");
                scanf("%d", &(new->English_score));

                new->total_three_score = new->Chinese_score + new->Math_score + new->English_score;
                new->ave_three_score = (float)new->total_three_score/3;

                head2 = insertStudentBehindHead(head2, new);

        }

        return head2;
}

2.1班级结构体

struct Class
{
        int num_stu;
        int grade;
        int total_Chinese;
        int total_Math;
        int total_English;
        float ave_Chinese;
        float ave_Math;
        float ave_English;
        struct Student *stu_score;
        struct Student *stu_MaxTotalScore;
        struct Student *stu_MinTotalScore;
        struct Class *next;
};

2.2尾插法创建班级链表

struct Class *insertClassBehindHead(struct Class *head, struct Class *new)
{
        struct Class *p = head;
        if (p == NULL){
                head = new;
                return head;
        }

        while(p->next != NULL){
                p = p->next;
        }

        p->next = new;

        return head;
}

struct Class *createClassLinkBehindHead(struct Class *head1, struct Student *head2)
{
        struct Class *new1 = NULL;
        struct Student *pnew = NULL;
        struct Student *pnew1 = NULL;
        int num_grade = 0;
        int num_stu = 0;
        int maxTotal = 0;
        int minTotal = 0;
        int i;

        printf("请输入班级个数:n");
        scanf("%d", &num_grade);

        for(i = 0; i < num_grade; i++){
                printf("请输入班级%d学生人数:n", (i+1));
                scanf("%d", &num_stu);

                new1 = (struct Class *)malloc(sizeof(struct Class));
                new1->num_stu = num_stu;
                new1->grade = num_grade;
                head2 = createStudentLinkBehindHead(new1, head2);
                new1->stu_score = head2;

                maxTotal = getMaxTotalScore(head2);
                pnew = findMaxTotalScore(head2, maxTotal);
                new1->stu_MaxTotalScore = pnew;

                minTotal = getMinTotalScore(head2);
                pnew1 = findMinTotalScore(head2, minTotal);
                new1->stu_MinTotalScore = pnew1;

                head2 = NULL;
                head1 = insertClassBehindHead(head1, new1);
        }


        return head1;
}

2.3查找班级总分、分均分最高分、最低分的学生
2.3.1查找学生总分和平均分最高分、最低分

int getMaxTotalScore(struct Student *head)
{
        struct Student *p = head;
        int maxTotalScore = 0;
        maxTotalScore = head->total_three_score;
        while(p != NULL){
                if (p->total_three_score > maxTotalScore){
                        maxTotalScore = p->total_three_score;
                }
                p = p->next;
        }

        return maxTotalScore;

}

int getMinTotalScore(struct Student *head)
{
        struct Student *p = head;
        int minTotalScore = 0;
        minTotalScore = head->total_three_score;
        while(p != NULL){
                if (p->total_three_score < minTotalScore){
                        minTotalScore = p->total_three_score;
                }

                p = p->next;
        }

        return minTotalScore;
}

2.3.2班级中查找各班总分和平均分最大、最低的学生

struct Student *findMinTotalScore(struct Student *head, int minTotal)
{
        struct Student *p = head;

        while(p != NULL){
                if (p->total_three_score == minTotal){
                        return p;
                }

                p = p->next;
        }

        printf("未找到最小值n");

        return NULL;
}

struct Student *insertStudentBehindHead(struct Student *head, struct Student *new)
{
        struct Student *p = head;
        if (p == NULL){
                head = new;
                return head;
        }

        while(p->next != NULL){
                p = p->next;
        }
        p->next = new;

        return head;
}


2.3.3打印班级学生所有相关信息

void printStudentScoresOfClass(struct Class *head)
{
        struct Class *p = head;
        struct Class *p1 = head;
        int i;
        int j;

        if (p == NULL){
                printf("NULL errorn");
                return;
        }

        while(p != NULL){
                for(i = 0; i < head->grade; i++){
                        printf("*************************************************************************************************n");
                        printf("班级%d成绩信息:n", (i+1));
                        for(j = 0; j < p->num_stu; j++){
                                printf("学生姓名:%s,语文成绩:%d,数学成绩:%d,英语成绩:%d, 三科总分:%d,三科平均分:%fn", p->stu_score->name, p->stu_score->Chinese_score, p->stu_score->Math_score, p->stu_score->English_score, p->stu_score->total_three_score, p->stu_score->ave_three_score);

                                p->stu_score = p->stu_score->next;
                        }

                        printf("三科总分最高分为:%d,总分平均分最高分为:%f,学生姓名:%sn", p->stu_MaxTotalScore->total_three_score, p->stu_MaxTotalScore->ave_three_score, p->stu_MaxTotalScore->name);

                        printf("三科总分最低分为:%d,总分平均分最低分为:%f,学生姓名:%sn", p->stu_MinTotalScore->total_three_score, p->stu_MinTotalScore->ave_three_score, p->stu_MinTotalScore->name);

                        p = p->next;
                }
        }

}

3.主函数

int main(int argc, char *argv[])
{
        struct Class *head1 = NULL;
        struct Student *head2 = NULL;

        head1 = createClassLinkBehindHead(head1, head2);
        printStudentScoresOfClass(head1);

        return 0;
}

4.学生成绩信息管理系统设计全部代码

#include 
#include 

struct Student
{
        int Chinese_score;
        int Math_score;
        int English_score;
        int total_three_score;
        float ave_three_score;
        char *name;
        struct Student *next;
};

struct Class
{
        int num_stu;
        int grade;
        int total_Chinese;
        int total_Math;
        int total_English;
        float ave_Chinese;
        float ave_Math;
        float ave_English;
        struct Student *stu_score;
        struct Student *stu_MaxTotalScore;
        struct Student *stu_MinTotalScore;
        struct Class *next;
};

void printStudentScoresOfClass(struct Class *head)
{
        struct Class *p = head;
        struct Class *p1 = head;
        int i;
        int j;

        if (p == NULL){
                printf("NULL errorn");
                return;
        }

        while(p != NULL){
                for(i = 0; i < head->grade; i++){
                        printf("*************************************************************************************************n");
                        printf("班级%d成绩信息:n", (i+1));
                        for(j = 0; j < p->num_stu; j++){
                                printf("学生姓名:%s,语文成绩:%d,数学成绩:%d,英语成绩:%d, 三科总分:%d,三科平均分:%fn", p->stu_score->name, p->stu_score->Chinese_score, p->stu_score->Math_score, p->stu_score->English_score, p->stu_score->total_three_score, p->stu_score->ave_three_score);

                                p->stu_score = p->stu_score->next;
                        }

                        printf("三科总分最高分为:%d,总分平均分最高分为:%f,学生姓名:%sn", p->stu_MaxTotalScore->total_three_score, p->stu_MaxTotalScore->ave_three_score, p->stu_MaxTotalScore->name);

                        printf("三科总分最低分为:%d,总分平均分最低分为:%f,学生姓名:%sn", p->stu_MinTotalScore->total_three_score, p->stu_MinTotalScore->ave_three_score, p->stu_MinTotalScore->name);

                        p = p->next;
                }
        }

}
int getMaxTotalScore(struct Student *head)
{
        struct Student *p = head;
        int maxTotalScore = 0;
        maxTotalScore = head->total_three_score;
        while(p != NULL){
                if (p->total_three_score > maxTotalScore){
                        maxTotalScore = p->total_three_score;
                }
                p = p->next;
        }

        return maxTotalScore;

}

int getMinTotalScore(struct Student *head)
{
        struct Student *p = head;
        int minTotalScore = 0;
        minTotalScore = head->total_three_score;
        while(p != NULL){
                if (p->total_three_score < minTotalScore){
                        minTotalScore = p->total_three_score;
                }

                p = p->next;
        }

        return minTotalScore;
}

struct Student *findMaxTotalScore(struct Student *head, int maxTotal)
{
        struct Student *p = head;

        while(p != NULL){
                if (p->total_three_score == maxTotal){
                        return p;
                }
                p = p->next;

        }

        printf("未找到最大值n");

        return NULL;
}
struct Student *findMinTotalScore(struct Student *head, int minTotal)
{
        struct Student *p = head;

        while(p != NULL){
                if (p->total_three_score == minTotal){
                        return p;
                }

                p = p->next;
        }

        printf("未找到最小值n");

        return NULL;
}

struct Student *insertStudentBehindHead(struct Student *head, struct Student *new)
{
        struct Student *p = head;
        if (p == NULL){
                head = new;
                return head;
        }

        while(p->next != NULL){
                p = p->next;
        }
        p->next = new;

        return head;
}

struct Student *createStudentLinkBehindHead(struct Class *head1, struct Student *head2)
{
        struct Student *new = NULL;
        int j;

        for(j = 0; j < head1->num_stu; j++){
                new = (struct Student *)malloc(sizeof(struct Student));
                new->name = (char *)malloc(128);

                getchar();
                printf("请输入第%d个学生姓名:n", (j+1));
                scanf("%s", new->name);
                printf("请输入语文成绩:n");
                scanf("%d", &(new->Chinese_score));
                printf("请输入数学成绩:n");
                scanf("%d", &(new->Math_score));
                printf("请输入英语成绩:n");
                scanf("%d", &(new->English_score));

                new->total_three_score = new->Chinese_score + new->Math_score + new->English_score;
                new->ave_three_score = (float)new->total_three_score/3;

                head2 = insertStudentBehindHead(head2, new);

        }

        return head2;
}
struct Class *insertClassBehindHead(struct Class *head, struct Class *new)
{
        struct Class *p = head;
        if (p == NULL){
                head = new;
                return head;
        }

        while(p->next != NULL){
                p = p->next;
        }

        p->next = new;

        return head;
}

struct Class *createClassLinkBehindHead(struct Class *head1, struct Student *head2)
{
        struct Class *new1 = NULL;
        struct Student *pnew = NULL;
        struct Student *pnew1 = NULL;
        int num_grade = 0;
        int num_stu = 0;
        int maxTotal = 0;
        int minTotal = 0;
        int i;

        printf("请输入班级个数:n");
        scanf("%d", &num_grade);

        for(i = 0; i < num_grade; i++){
                printf("请输入班级%d学生人数:n", (i+1));
                scanf("%d", &num_stu);

                new1 = (struct Class *)malloc(sizeof(struct Class));
                new1->num_stu = num_stu;
                new1->grade = num_grade;
                head2 = createStudentLinkBehindHead(new1, head2);
                new1->stu_score = head2;

                maxTotal = getMaxTotalScore(head2);
                pnew = findMaxTotalScore(head2, maxTotal);
                new1->stu_MaxTotalScore = pnew;

                minTotal = getMinTotalScore(head2);
                pnew1 = findMinTotalScore(head2, minTotal);
                new1->stu_MinTotalScore = pnew1;

                head2 = NULL;
                head1 = insertClassBehindHead(head1, new1);
        }


        return head1;
}

int main(int argc, char *argv[])
{
        struct Class *head1 = NULL;
        struct Student *head2 = NULL;

        head1 = createClassLinkBehindHead(head1, head2);
        printStudentScoresOfClass(head1);

        return 0;
}                                                                                                                                                                                                                                                                  

5.运行代码测试


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/971526.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号