C 歌手大赛实例
有十个选手参加歌唱比赛,十个裁判对每个选手的演唱进行打分,去掉一个最高分,去掉一个最低分,剩下分数的平均值最为每位选手的最后成绩 要求:1、采用结构体定义每个选手的参赛编号,姓名以及十个裁判打出的分数; 2、从键盘输入每个选手的相关信息:参赛编号,姓名和十个得分; 3、计算每位选手的最后得分,并按分数由高到低排序; 4、查询功能:输入参赛选手的编号或姓名,便可输出该选手的得分以及排名;
源码如下:
#include <stdio.h> void ShowSingerData(); void AddNewSingeData(); #define USER_LEN 16 #define SOCRE_LEN 10 #define MAX_SINGER 10 struct s_Singer { float avgScore ; // 歌手平均分 int score[SOCRE_LEN] ; // 歌手分数记录 int num; // 参赛编号 char name[ USER_LEN ] ; // 歌手名字 struct s_Singer* pNext ; struct s_Singer* pPrev ; }; static int g_CurSingerNum ; // 当前歌手编号 static struct s_Singer g_Singers[MAX_SINGER]; // 参赛的歌手 static struct s_Singer* g_SingerList; // 比赛后的歌手排名表 void ShowCmd() { char cmd = '\0'; while(true) { printf("welcom The Singing Contest\n"); printf("\t'a'-add new singer information\n"); printf("\t's'-show The Singing Contest List\n"); printf("\t'q'-exit!\n"); fflush(stdin); if( (cmd=getchar())!=EOF) { if( cmd == 'q' ) { break; } switch (cmd) { case 'a': AddNewSingeData();break; case 's': ShowSingerData(); break; } } } } void ShowSingerData() { printf("num\tavg\tname\n"); static struct s_Singer* t ; t = g_SingerList; while( t != NULL ) { printf( "%d\t%0.2f\t%s\n",t->num,t->avgScore,t->name ); t=t->pNext; } } void AddSinger( struct s_Singer *pSinger ) { if( g_SingerList != NULL ) { struct s_Singer* t =g_SingerList; while( t!=NULL && t->pNext ) { if( t->avgScore < pSinger->avgScore ) { break; } t = t->pNext; } if( t != g_SingerList && t->pNext!=NULL ) { pSinger->pPrev = t->pPrev; pSinger->pNext = t; t->pPrev->pNext = pSinger; t->pPrev = pSinger; } else { if( t == g_SingerList ) { if( g_SingerList->avgScore < pSinger->avgScore ) { pSinger->pNext = t; t->pPrev = pSinger; g_SingerList = pSinger; } else { g_SingerList->pNext = pSinger; pSinger->pPrev = g_SingerList; } } else { if( t->avgScore>pSinger->avgScore) { t->pNext=pSinger; pSinger->pPrev = t; pSinger->pNext = NULL; } } } } else { g_SingerList = pSinger; return; } } void DealLowHigh(struct s_Singer *pSinger) { int l=0,low=pSinger->score[0]; int h=0,hight=pSinger->score[0]; for( int i=0;i<SOCRE_LEN;++i ) { if( pSinger->score[i]<low ) { low = pSinger->score[i]; l=i; } if( pSinger->score[i]>hight ) { hight = pSinger->score[i]; h=i; } } pSinger->score[l] = 0 ; pSinger->score[h] = 0 ; } void CacluateAvg( struct s_Singer *pSinger ) { int sum = 0; for( int i=0;i<SOCRE_LEN;++i ) { sum+=pSinger->score[i]; } pSinger->avgScore = 1.0F*sum/(SOCRE_LEN-2); } void AddNewSingeData() { if( g_CurSingerNum>=MAX_SINGER ) { printf("sorry! the singing contest has overed!\n"); return; } printf("Plz,enter the singer's information\n"); struct s_Singer* pSinger = &g_Singers[g_CurSingerNum]; pSinger->num = ++g_CurSingerNum; fflush(stdin); if( scanf_s("%s %d %d %d %d %d %d %d %d %d %d", pSinger->name, USER_LEN, &pSinger->score[0], &pSinger->score[1], &pSinger->score[2], &pSinger->score[3], &pSinger->score[4], &pSinger->score[5], &pSinger->score[6], &pSinger->score[7], &pSinger->score[8], &pSinger->score[9]) != EOF ) { DealLowHigh( pSinger ); CacluateAvg( pSinger ); AddSinger( pSinger ); } } int main(int argc, char* argv[]) { ShowCmd(); return 0; }
首发地址:爱游巴士