C語言教育訓練C++教程這篇文章是介紹的:競技比賽打分系統源碼,這是一些小的程式,可能會用上,在這分享給大家!
#include<stdio.h>
#include<conio.h>
#include<stdlib.h >
#define JUDEGNUM 3 /* 裁判數 */
#define NAMELEN 20 /* 姓名最大字元數 */
#define CODELEN 10 /* 号碼最大字元數 */
#define FNAMELEN 80 /* 檔案名最大字元數 */
#define BUFFSIZE 128 /* 緩沖區最大字元數 */
char judgement[JUDEGNUM][NAMELEN+1] = {"judgementA","judgementB","judgementC"};
struct AthleteScore
{
char name[NAMELEN+1]; /* 姓名 */
char code[CODELEN+1]; /* 學号 */
int score[JUDEGNUM]; /* 各裁判給的成績 */
int total; /* 總成級 */
};
struct LinkNode
char code[CODELEN+1]; /* 号碼 */
struct LinkNode *next;
}*head; /* 連結清單首指針 */
int total[JUDEGNUM]; /* 各裁判給的總成績 */
FILE *filepoint; /* 檔案指針 */
char filename[FNAMELEN];/* 檔案名 */
/* 從指定檔案讀入一個記錄 */
int GetRecord(FILE *fpt,struct AthleteScore *sturecord)
char buf[BUFFSIZE];
int i;
if(fscanf(fpt,"%s",buf)!=1)
return 0; /* 檔案結束 */
strncpy(sturecord->name,buf,NAMELEN);
fscanf(fpt,"%s",buf);
strncpy(sturecord->code,buf,CODELEN);
for(i=0;i
fscanf(fpt,"%d",&sturecord->score[i]);
for(sturecord->total=0,i=0;i
sturecord->total+=sturecord->score[i];
return 1;
}
/* 對指定檔案寫入一個記錄 */
void PutRecord(FILE *fpt,struct AthleteScore *sturecord)
fprintf(fpt,"%s\n",sturecord->name);
fprintf(fpt,"%s\n",sturecord->code);
fprintf(fpt,"%d\n",sturecord->score[i]);
return ;
/* 顯示運動員記錄 */
void ShowAthleteRecord(struct AthleteScore *rpt)
printf("\nName : %s\n",rpt->name);
printf("Code : %s\n",rpt->code);
printf("score :\n");
printf(" %-15s : %4d\n",judgement[i],rpt->score[i]);
printf("Total : %4d\n",rpt->total);
/* 清單顯示運動員成績 */
void Listathleteinfo(char *fname)
FILE *fp;
struct AthleteScore s;
system("cls");
/*
clrscr();*/
if((fp=fopen(fname,"r"))==NULL)
printf("Can't open file %s.\n",fname);
while(GetRecord(fp,&s)!=0)
ShowAthleteRecord(&s);
fclose(fp);
return;
/* 構造連結清單 */
struct LinkNode *CreatLinklist(char *fname)
struct LinkNode *p,*u,*v,*h;
return NULL;
h=NULL;
p=(struct LinkNode *)malloc(sizeof(struct LinkNode));
while(GetRecord(fp,(struct AthleteScore *)p)!=0)
v=h;
while(v&&p->total<=v->total)
u=v;
v=v->next;
if(v==h)
h=p;
else
u->next=p;
p->next=v;
free(p);
return h;
/* 順序顯示連結清單各表元 */
void OutputLinklist(struct LinkNode *h)
system("cls");/*clrscr();*/
while(h!=NULL)
ShowAthleteRecord((struct AthleteScore *)h);
printf("\n");
while(getchar()!='\n');
h=h->next;
/* 按運動員姓名查找記錄 */
int SearchbyName(char *fname, char *key)
int c;
return 0;
c=0;
if(strcmp(s.name,key)==0)
c++;
if(c==0)
printf("The athlete %s is not in the file %s.\n",key,fname);
/* 按運動員号碼查找記錄 */
int SearchbyCode(char *fname, char *key)
if(strcmp(s.code,key)==0)
break;
void InsertRecord()
char c,i,j,n;
printf("Please input the athletes score record file's name: \n");
scanf("%s",filename);
if((fp=fopen(filename,"r"))==NULL)
printf("The file %s doesn't exit.\ndo you want to creat it? (Y/N) ",filename);
getchar();
c=getchar();
if(c=='Y'||c=='y')
fp=fopen(filename,"w");
printf("Please input the record number : ");
scanf("%d",&n);
printf("Input the athlete's name: ");
scanf("%s",&s.name);
printf("Input the athlete's code: ");
scanf("%s",&s.code);
for(j=0;j
printf("Input the %s mark: ",judgement[j]);
scanf("%d",&s.score[j]);
PutRecord(fp,&s);
int main()
int i,j,n;
char c;
while(1)
printf("\n-------------- Input a command -----------\n");
printf("| i : insert record to a file. |\n");
printf("| n : search record by name. |\n");
printf("| c : search record by code. |\n");
printf("| l : list all the records. |\n");
printf("| s : sort the records by total. |\n");
printf("| q : quit. |\n");
printf("------------------------------------------\n");
printf("Please input a command:\n");
scanf("%c",&c); /* 輸入選擇指令 */
switch(c)
case 'i':
InsertRecord();
getch();
case 'n': /* 按運動員的姓名尋找記錄 */
printf("Please input the athlete's name:\n");
scanf("%s",buf);
SearchbyName(filename,buf);
case 'c': /* 按運動員的号碼尋找記錄 */
printf("Please input the athlete's code:\n");
SearchbyCode(filename,buf);
case 'l': /* 列出所有運動員記錄 */
Listathleteinfo(filename);
case 's': /* 按總分從高到低排列顯示 */
if((head=CreatLinklist(filename))!=NULL)
OutputLinklist(head);
case 'q':
default:
#include