C语言练习——双链表下学生成绩管理系统

导读:本篇文章讲解 C语言练习——双链表下学生成绩管理系统,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

学生和班级结构体初始化

学生:

struct Student
{
        int math;			//  数学
        int chinese;		//  语文
        int english;		//	英语
        int StudentID;		// 记录学生ID
        int sum;			// 记录总分

        struct Student *next;	// 学生链表节点
};

班级:

struct Class
{
        int classroom;				// 教室号

        struct Student *Bhead;		// 用于指向同个班级不同学生
        struct Class *next;			// 班级链表节点
};

学生链表创建(这里以尾插法为例子!):

struct Student *studentlink(struct Student *head,int studentnumber)
{
        struct Student *new;		//	创建链表节点
        struct Student *p = head;	//	拿到学生链表头节点
        int i;

        for(i=1;i<=studentnumber;i++)
        {		//	开辟节点空间
                new = (struct Student *)malloc(sizeof(struct Student));
                new->StudentID = i;
				
				//	判断是否是第一个节点,也就是头节点
                if(p == NULL)
                {
                        p = new;
                        head = p;
                }
                else
                {		//	尾插法
                        while( p->next != NULL)
                        {		
                                p = p->next;	// 让next指针到尾部指向NULL
                        }

                        p->next = new;	//	让尾部节点作为头节点
                }
        }
        return head;	// 返回头节点

}

班级链表创建:

struct Class *classlink(struct Class *head,int classnumber,int studentnumber)
{
        struct Class *p = head;
        struct Student *student;
        struct Class *new;
        int j;

        for(j=1;j<=classnumber;j++)
        {
        		//	每个班级里头个一名学生完成后要重新初始化链表
                student = NULL;
				//	开辟空间
                new = (struct Class*)malloc(sizeof(struct Class));
                new->classroom = j;
				//	创建这个学生的链表记录成绩
                student = studentlink(student,studentnumber);
                new->Bhead = student;	//	指向这个班级下一个学生的链表


                if( p == NULL)	//	判断是否为头节点
                {
                        p = new;
                        head = p;
                }
                else
                {		//	尾插法
                        while(p->next != NULL)
                        {
                                p = p->next;
                        }
                        p->next = new;	//	将尾部节点当做头节点开始录成绩
                }
        }
        return head;

}

输入数据到链表中:

struct Class *InPutData(struct Class *head)
{
        struct Student *p = NULL;	//	学生链表
        struct Class *q = head;		//	班级链表

        while( q != NULL)	//	开始扫描班级链表
        {
                p = q->Bhead;	//	用来指向这个班级的下一个学生链表

                while(p != NULL)	//	这个班级的学生链表
                {
                        printf("Please scanf %d Class the %d Student 成绩\n",q->classroom,p->StudentID);

                        printf("Please scanf chinese score: ");
                        scanf("%d",&(p->chinese));

                        printf("Please scanf math score: ");
                        scanf("%d",&(p->math));

                        printf("Please scanf english score: ");
                        scanf("%d",&(p->english));

                        putchar('\n');

                        p->sum = (p->chinese) + (p->math) + (p->english);

                        printf("------------------------------------------\n");

                        p = p->next;	//	录完开始下一个学生链表

                }
                q = q->next;	//	录完开始下一个班级链表
        }
        return head;	//	返回班级链表头节点
}

求班级里头最高分:

void MaxScore(struct Class *head)
{
        struct Class *p = head;
        struct Student *q = NULL;
        int max = 0;

        while( p != NULL)
        {
                q = p->Bhead;	//	开始指向这个班级下一个学生链表

                while( q != NULL)
                {
                        if( max < q->sum)
                        {
                                max = q->sum;
                        }
                        q = q->next;
                }

                p = p->next;	//	指向下一个班级链表
        }
        printf("All Class Score Max is : %d\n",max);
        printf("------------------------------------------\n");
}

求班级里头最低分:

void MinScore(struct Class *head)
{
        struct Student *q = NULL;
        struct Class *p = head;
        int min = 1000;

        while( p != NULL)
        {
                q = p->Bhead;	//	录完后指向下一个学生链表

                while( q != NULL)
                {
                        if( min > q->sum)
                        {
                                min = q->sum;
                        }
                        q = q->next;
                }

                p = p->next;	//	指向下一个班级链表
        }
        printf("All Class Score Min is : %d\n",min);
        printf("------------------------------------------\n");
}

求班级平均分:

void AverageScore(struct Class *head,int number)
{
        struct Student *p = NULL;
        struct Class *q = head;
        int count = 0;

        while( q != NULL)
        {
                p = q->Bhead;
                while( p != NULL)
                {
                        count += p->sum;	// 把这个班级的学生总分求和
                        p = p->next;	//	指向下一个学生链表
                }

                printf("The %d Class AverageScore  is : %d\n",q->classroom,count/number);
                printf("------------------------------------------\n");
                count = 0;	//	每个班级平均分算完,需要重置为 0,求下一个
                q = q->next;	//	指向下一个班级链表
        }
}

输入班级人数和每个班学生人数:

int init_class()
{
        int a;
        printf("请输入班级个数: ");
        scanf("%d",&a);
        return a;
}

int init_student()
{
        int b;
        printf("请输入每班学生个数: ");
        scanf("%d",&b);
        return b;
}

主函数部分:

int main()
{
        struct Class *classhead = NULL;
        int classnumber;
        int studentnumber;

        classnumber   = init_class();
        studentnumber = init_student();

        classhead = classlink(classhead,classnumber,studentnumber);	//	创建链表
        classhead = InPutData(classhead);	//	开始录入成绩

        MaxScore(classhead);	//	求最高分
        MinScore(classhead);	//	求最低分
        AverageScore(classhead,studentnumber);	// 	求平均分

        return 0;
}

代码演示:

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

struct Student
{
        int math;
        int chinese;
        int english;
        int StudentID;
        int sum;

        struct Student *next;
};

struct Class
{
        int classroom;

        struct Student *Bhead;
        struct Class *next;
};

struct Student *studentlink(struct Student *head,int studentnumber)
{
        struct Student *new;
        struct Student *p = head;
        int i;

        for(i=1;i<=studentnumber;i++)
        {
                new = (struct Student *)malloc(sizeof(struct Student));
                new->StudentID = i;

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

                        p->next = new;
                }
        }
        return head;

}

struct Class *classlink(struct Class *head,int classnumber,int studentnumber)
{
        struct Class *p = head;
        struct Student *student;
        struct Class *new;
        int j;

        for(j=1;j<=classnumber;j++)
        {
                student = NULL;
                new = (struct Class*)malloc(sizeof(struct Class));
                new->classroom = j;

                student = studentlink(student,studentnumber);
                new->Bhead = student;
                if( p == NULL)
                {
                        p = new;
                        head = p;
                }
                else
                {
                        while(p->next != NULL)
                        {
                                p = p->next;
                        }
                        p->next = new;
                }
        }
        return head;

}

struct Class *InPutData(struct Class *head)
{
        struct Student *p = NULL;
        struct Class *q = head;

        while( q != NULL)
        {
                p = q->Bhead;

                while(p != NULL)
                {
                        printf("Please scanf %d Class the %d Student 成绩\n",q->classroom,p->StudentID);

                        printf("Please scanf chinese score: ");
                        scanf("%d",&(p->chinese));

                        printf("Please scanf math score: ");
                        scanf("%d",&(p->math));

                        printf("Please scanf english score: ");
                        scanf("%d",&(p->english));

                        putchar('\n');

                        p->sum = (p->chinese) + (p->math) + (p->english);

                        printf("------------------------------------------\n");

                        p = p->next;

                }
                q = q->next;
        }
        return head;
}

void MaxScore(struct Class *head)
{
        struct Class *p = head;
        struct Student *q = NULL;
        int max = 0;

        while( p != NULL)
        {
                q = p->Bhead;

                while( q != NULL)
                {
                        if( max < q->sum)
                        {
                                max = q->sum;
                        }
                        q = q->next;
                }

                p = p->next;
        }
        printf("All Class Score Max is : %d\n",max);
        printf("------------------------------------------\n");
}

void MinScore(struct Class *head)
{
        struct Student *q = NULL;
        struct Class *p = head;
        int min = 1000;

        while( p != NULL)
        {
                q = p->Bhead;

                while( q != NULL)
                {
                        if( min > q->sum)
                        {
                                min = q->sum;
                        }
                        q = q->next;
                }

                p = p->next;
        }
        printf("All Class Score Min is : %d\n",min);
        printf("------------------------------------------\n");
}
void AverageScore(struct Class *head,int number)
{
        struct Student *p = NULL;
        struct Class *q = head;
        int count = 0;

        while( q != NULL)
        {
                p = q->Bhead;
                while( p != NULL)
                {
                        count += p->sum;
                        p = p->next;
                }

                printf("The %d Class AverageScore  is : %d\n",q->classroom,count/number);
                printf("------------------------------------------\n");
                count = 0;
                q = q->next;
        }
}

int init_class()
{
        int a;
        printf("请输入班级个数: ");
        scanf("%d",&a);
        return a;
}

int init_student()
{
        int b;
        printf("请输入每班学生个数: ");
        scanf("%d",&b);
        return b;
}

int main()
{
        struct Class *classhead = NULL;
        int classnumber;
        int studentnumber;

        classnumber   = init_class();
        studentnumber = init_student();

        classhead = classlink(classhead,classnumber,studentnumber);
        classhead = InPutData(classhead);

        MaxScore(classhead);
        MinScore(classhead);
        AverageScore(classhead,studentnumber);

        return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/68460.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!