【C语言】学生成绩链表的录入

输入10个学生5门课的成绩,分别用函数实现下列功能:

1>计算每个学生的平均分。

2>计算每门课的平均分。

3>找出所有50个分数中最高的分数所对应的学生和课程。

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

typedef struct student
{	
	char name[15];
	float Chinese;
	float English;
	float math;
	float Physics;
	float Chemistry;
	struct student *next;
}Stu,*PStu;

PStu creat(PStu	head)
{
	PStu p1 =NULL;
	PStu p2 =NULL;
	int n = 0;
	p1 =(PStu)malloc(sizeof(Stu));
	p2 = p1;
	 if(p1==NULL)        //节点开辟不成功  
    {  
        printf ("\nCann't create it, try it again in a moment!\n");  
        return NULL;  
    }
	 else
	 {
		printf("请输入名字,语文成绩,英语成绩,数学成绩,物理成绩,化学成绩\n");
		scanf("%s %f %f %f %f %f",&(p1->name),&(p1->Chinese),&(p1->English),&(p1->math),\
			&(p1->Physics),&(p1->Chemistry));
	 }
	 while(strcmp(p1->name,"end"))
	{
		if(head == NULL)
		{
			head  = p1;
			p2->next = NULL;
		}
		else 
			p2->next = p1;
		p2 = p1;
		p1 = (PStu)malloc(sizeof(Stu));
		printf("请输入名字,语文成绩,英语成绩,数学成绩,物理成绩,化学成绩\n");
		scanf("%s %f %f %f %f %f",&p1->name,&p1->Chinese,&p1->English,&p1->math,\
		&p1->Physics,&p1->Chemistry);

	}
	p2->next=NULL;
	free(p1);
	p1 = NULL;
	return head;
}

void print(	PStu head)/*出以head为头的链表各节点的值*/  
{  
    PStu temp; 
	printf ("\nNow , These records are:\n");  
    temp=head;/*取得链表的头指针*/  
	 

    while(temp!=NULL)/*只要是非空表*/  
    {  
		printf("姓名:%s\n语文%f\n英语%f\n数学%f\n物理%f\n化学%f\n",temp->name,\
			temp->Chinese,temp->English,temp->math,temp->Physics,temp->Chemistry);/*输出链表节点的值*/  
        temp=temp->next;/*跟踪链表增长*/  
    }  
}  

void avScore(PStu head)
{	
	float aScore;
	PStu temp;
	temp = head;
	while(temp != NULL)
	{
		aScore = (temp->Chinese + temp->English + temp->math + temp->Physics + temp->Chemistry)/5;
		printf("%s的平均成绩为:%f\n",temp->name,aScore);
		temp=temp->next;
	}
}

void avClass(PStu head)
{
	float aChinese = 0;
	float aEnglish = 0;
	float amath = 0;
	float aPhysics = 0;
	float aChemistry = 0;
	int count = 0;
	PStu temp = head;
	while(temp != NULL)
	{
		aChinese += temp->Chinese;
		aEnglish += temp->English;
		amath += temp->math;
		aPhysics += temp->Physics;
		aChemistry += temp->Chemistry;
		count++;
		temp=temp->next;
	}
	printf("语文平均分是:%f\n英语平均分是:%f\n数学平均分是:%f\n物理平均分是:%f\n化学平均分是:%f\n",\
		   aChinese/count,aEnglish/count,amath/count,aPhysics/count,aChemistry/count);
}

void maxScore(PStu head)	//出现共同最高分为第一个出现的为准:
{
	float mScore = 0;
	char work[20] = {0};
	char name[10];
	PStu temp = head;
	mScore = temp->Chinese;
	while(temp != NULL)
	{	
		strcpy(name,temp->name);
		if(temp->Chinese > mScore)
		{	
			mScore = temp->Chinese;
			strcpy(work,"chinese");
			/*work = "chinese";*/
		}
		else if(temp->English >mScore)
		{
			mScore = temp->English;
			strcpy(work,"english");
			/*work = "english";*/
		}
		else if(temp->math >mScore)
		{
			mScore = temp->math;
			strcpy(work,"math");
			/*work = "math";*/
		}
		else if(temp->Physics >mScore)
		{
			mScore = temp->Physics;
			strcpy(work,"physics");
			/*work = "physics";*/
		}
		else if(temp->Chemistry >mScore)
		{
			mScore = temp->Chemistry;
			strcpy(work,"chemistry");
			/*work = "chemistry";*/
		}
		temp = temp->next;
	}
	printf("同学%s的%s成绩为所有成绩中的最高分:%f\n",name,work,mScore);
}
int main()
{	
	PStu head  = NULL;
	head = creat(head);
	print(head);
	avScore(head);
	avClass(head);
	maxScore(head);
	return 0;
}


本文出自 “剩蛋君” 博客,请务必保留此出处http://memory73.blog.51cto.com/10530560/1682910

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值