输入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
原文地址:http://memory73.blog.51cto.com/10530560/1682910