原題,完成校際運(yùn)動(dòng)會(huì)成績(jī)管理功能,包括輸入比賽項(xiàng)目event及參賽學(xué)校名字name,輸入比賽成績(jī)score,參賽選手名字person,項(xiàng)目名次rank,計(jì)算總分sum,其中,sum為score加上項(xiàng)目的加分。 各項(xiàng)目名次取法有如下幾種:項(xiàng)目一:取前5名:第1名得分7,第2名得分5,第3名得分3,第4名得分2,第5名得分1;其余名次不加分。項(xiàng)目二:取前3名:第1名得分5,第2名得分3,第3名得分2;其余名次不加分。輸出第一所學(xué)校的score和sum。#include<stdio.h>#include<stdlib.h>#include<string.h>#define LEN sizeof(struct School)struct School{int event;?char name[20];?int? score;?char person[20];?int rank;?int sum;?struct School *next;};static int n;struct School *creat(void){struct School *head;?struct School *p1,*p2,*a[100];?n=0;?p1=p2=(struct School *)malloc(LEN);?head=NULL;?while(p1->event!=0)?{n=n+1;? if(n==1)a[0]=head=p1;? else p2->next=p1;? p2=p1;? p1=(struct School *)malloc(LEN);? printf("the event is:(put 0 end the putting of school):");? scanf("%d",&p1->event);? printf("school name:");? getchar();? gets(p1->name);? printf("school score:");? scanf("%d",&p1->score);? printf("person name:");? getchar();? gets(p1->person);? printf("school rank:");? scanf("%d",&p1->rank);? switch(p1->event)? {case 0:p1->sum=p1->score;break;?? case 1:??? {switch(p1->rank)???? {case 1:p1->sum=p1->score+7;break;????? case 2:p1->sum=p1->score+5;break;????? case 3:p1->sum=p1->score+3;break;????? case 4:p1->sum=p1->score+2;break;????? case 5:p1->sum=p1->score+1;break;????? default:p1->sum=p1->score;}}break;?? case 2:????? {switch(p1->rank)????? {case 1:p1->sum=p1->score+5;break;?????? case 2:p1->sum=p1->score+3;break;?????? case 3:p1->sum=p1->score+2;break;?????? default:p1->sum=p1->score;}}break;??? default:p1->sum=p1->score;? }? }? p2->next=NULL;? return(a[0]);??? }int main(){struct School *pt;pt=creat();printf("\nscore:%d\nsum:%d\n",pt->score,pt->sum);??? return 0;}輸入的score是123,event 為1,rank為1.輸出的score總是一個(gè)很大的數(shù)據(jù)15564562,輸出的sum 也很大,11235578.求教為什么?
2 回答
已采納

BlueCitizen
TA貢獻(xiàn)5條經(jīng)驗(yàn) 獲得超0個(gè)贊
哦,能進(jìn)入循環(huán),我說錯(cuò)了,抱歉。while(p1->event!=0)對(duì)于這個(gè)入口循環(huán),event并沒有賦值,那么系統(tǒng)會(huì)隨機(jī)給一個(gè)值,給什么樣的值看運(yùn)氣,一般會(huì)很大。
- 2 回答
- 0 關(guān)注
- 1754 瀏覽