生活随笔
收集整理的這篇文章主要介紹了
一阶段结束考核题(链表的嵌套使用)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
鏈表A,每個節(jié)點存放一個新的鏈表B1,B2,B3,B4,B5的頭結(jié)點。
場景:一個年級,相當(dāng)鏈表A
該年級5個班,每個班5個人,相當(dāng)于鏈表B1–B5
做一個學(xué)生成績管理系統(tǒng)
學(xué)生成績有語文 數(shù)學(xué) 英語
功能: 錄入成績 找最三科總分的最高分 最低分 算出平均分
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct people
{char* name
;int lesson
;int number
;int math
;int chinese
;int english
;int all
;struct people
* next
;
};struct class
{int class
;struct people
* firstpeople
;struct class
* next
;
};struct class
* insertfromclassbehind(struct class
* classhead
,struct class
* classnewnode
)
{struct class
* p
=NULL;p
=classhead
;if(classhead
==NULL){classhead
=classnewnode
;return classhead
;}struct people
* insertfrompeoplebefore(struct people
* peoplehead
,struct people
* peoplenewnode
)
{if(peoplehead
==NULL){peoplehead
=peoplenewnode
;}else{peoplenewnode
->next
=peoplehead
;peoplehead
=peoplenewnode
;}return peoplehead
;
}void linkPrintf(struct class
* head
)
{struct people
* p
=NULL;if(head
==NULL){printf("打印失敗,鏈表為空\n");}while(head
!=NULL){p
=head
->firstpeople
;while(p
!=NULL){printf("姓名:%s\n",p
->name
);printf("班級:%d\n",p
->lesson
);printf("學(xué)號:%d\n",p
->number
);printf("數(shù)學(xué):%d\n",p
->math
);printf("語文:%d\n",p
->chinese
);printf("英語:%d\n",p
->english
);printf("---------------------------------------------------------------------------------------------------\n");p
=p
->next
;}head
=head
->next
;}
}struct class
* creatnewlink(struct class
* classhead
,struct people
* peoplehead
,int classall
)
{struct class
* classnewnode
=NULL;struct people
* peoplenewnode
=NULL;while(classall
){int number
;classnewnode
=(struct class
*)malloc(sizeof(struct class
));classnewnode
->next
=NULL;classnewnode
->firstpeople
=NULL;printf("請輸入班級:\n");scanf("%d",&classnewnode
->class
);printf("請輸入該班的人數(shù):\n");scanf("%d",&number
);while(number
){peoplenewnode
=(struct people
*)malloc(sizeof(struct people
));peoplenewnode
->next
=NULL;peoplenewnode
->name
=(char*)malloc(128);memset(peoplenewnode
->name
,'\0',128);peoplenewnode
->lesson
=classnewnode
->class
;printf("請輸入姓名:\n");scanf("%s",peoplenewnode
->name
);
printf("請輸入學(xué)號:\n");scanf("%d",&peoplenewnode
->number
);printf("請輸入數(shù)學(xué)成績:\n");scanf("%d",&peoplenewnode
->math
);printf("請輸入語文成績:\n");scanf("%d",&peoplenewnode
->chinese
);printf("請輸入英語成績:\n");scanf("%d",&peoplenewnode
->english
);peoplenewnode
->all
=peoplenewnode
->english
+peoplenewnode
->math
+peoplenewnode
->chinese
;peoplehead
=insertfrompeoplebefore(peoplehead
,peoplenewnode
);number
--;}classnewnode
->firstpeople
=peoplehead
;peoplehead
=NULL;classhead
=insertfromclassbehind(classhead
,classnewnode
);classall
--;}return classhead
;
}void findmaxall(struct class
*head
)
{struct class
* p
=head
;struct people
* p2
=p
->firstpeople
;struct people
* max
=NULL;max
=p2
;if(p
==NULL){printf("參數(shù)不能為空!\n");}while(p
!=NULL){p2
=p
->firstpeople
;while(p2
!=NULL){if(max
->all
<=p2
->all
){max
=p2
;}p2
=p2
->next
;}p
=p
->next
;}printf("---------------------------------------------------------------------------------------------------\n");printf("總分最高為:%d,姓名:%s,班級:%d,學(xué)號:%d\n",max
->all
,max
->name
,max
->lesson
,max
->number
);printf("---------------------------------------------------------------------------------------------------\n");
}void findminall(struct class
*head
)
{struct class
* p
=head
;struct people
* p2
=p
->firstpeople
;struct people
* min
=NULL;min
=p2
;if(p
==NULL){printf("參數(shù)不能為空!\n");}while(p
!=NULL){p2
=p
->firstpeople
;while(p2
!=NULL){if(min
->all
>=p2
->all
){min
=p2
;}p2
=p2
->next
;}p
=p
->next
;}printf("總分最低為:%d,姓名:%s,班級:%d,學(xué)號:%d\n",min
->all
,min
->name
,min
->lesson
,min
->number
);printf("---------------------------------------------------------------------------------------------------\n");
}void findaverage(struct class
* head
)
{int mathall
,chineseall
,englishall
,peopleall
;mathall
=chineseall
=englishall
=peopleall
=0;struct people
* p
;if(head
==NULL){printf("鏈表為空錯誤\n");}while(head
!=NULL){p
=head
->firstpeople
;while(p
!=NULL){mathall
=p
->math
+mathall
;chineseall
=p
->chinese
+chineseall
;englishall
=p
->english
+englishall
;peopleall
++;p
=p
->next
;}head
=head
->next
;}printf("語文平均分:%f\n",(float)chineseall
/peopleall
);printf("數(shù)學(xué)平均分:%f\n",(float)mathall
/peopleall
);printf("英語平均分:%f\n",(float)englishall
/peopleall
);printf("---------------------------------------------------------------------------------------------------\n");
}
int main()
{struct class
* classhead
=NULL;struct people
* peoplehead
=NULL;int classall
;printf("請輸入班級總數(shù):\n");scanf("%d",&classall
);classhead
=creatnewlink(classhead
,peoplehead
,classall
);findmaxall(classhead
);findminall(classhead
);findaverage(classhead
);return 0;
}
總結(jié)
以上是生活随笔為你收集整理的一阶段结束考核题(链表的嵌套使用)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。