您好,登錄后才能下訂單哦!
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 name[NAMELEN+1]; /* 姓名 */
char code[CODELEN+1]; /* 號碼 */
int score[JUDEGNUM]; /* 各裁判給的成績 */
int total; /* 總成級 */
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)
{
int i;
fprintf(fpt,"%s\n",sturecord->name);
fprintf(fpt,"%s\n",sturecord->code);
for(i=0;i
fprintf(fpt,"%d\n",sturecord->score[i]);
return ;
}
/* 顯示運動員記錄 */
void ShowAthleteRecord(struct AthleteScore *rpt)
{
int i;
printf("\nName : %s\n",rpt->name);
printf("Code : %s\n",rpt->code);
printf("score :\n");
for(i=0;i
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);
return ;
}
while(GetRecord(fp,&s)!=0)
{
ShowAthleteRecord(&s);
}
fclose(fp);
return;
}
/* 構造鏈表 */
struct LinkNode *CreatLinklist(char *fname)
{
FILE *fp;
struct AthleteScore s;
struct LinkNode *p,*u,*v,*h;
int i;
if((fp=fopen(fname,"r"))==NULL)
{
printf("Can't open file %s.\n",fname);
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;
p=(struct LinkNode *)malloc(sizeof(struct LinkNode));
}
free(p);
fclose(fp);
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;
}
return;
}
/* 按運動員姓名查找記錄 */
int SearchbyName(char *fname, char *key)
{
FILE *fp;
int c;
struct AthleteScore s;
system("cls");/*clrscr();*/
if((fp=fopen(fname,"r"))==NULL)
{
printf("Can't open file %s.\n",fname);
return 0;
}
c=0;
while(GetRecord(fp,&s)!=0)
{
if(strcmp(s.name,key)==0)
{
ShowAthleteRecord(&s);
c++;
}
}
fclose(fp);
if(c==0)
printf("The athlete %s is not in the file %s.\n",key,fname);
return 1;
}
/* 按運動員號碼查找記錄 */
int SearchbyCode(char *fname, char *key)
{
FILE *fp;
int c;
struct AthleteScore s;
system("cls");/*clrscr();*/
if((fp=fopen(fname,"r"))==NULL)
{
printf("Can't open file %s.\n",fname);
return 0;
}
c=0;
while(GetRecord(fp,&s)!=0)
{
if(strcmp(s.code,key)==0)
{
ShowAthleteRecord(&s);
c++;
break;
}
}
fclose(fp);
if(c==0)
printf("The athlete %s is not in the file %s.\n",key,fname);
return 1;
}
void InsertRecord()
{
FILE *fp;
char c,i,j,n;
struct AthleteScore s;
system("cls");/*clrscr();*/
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);
for(i=0;i
{
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);
}
fclose(fp);
}
}
fclose(fp);
return;
}
int main()
{
int i,j,n;
char c;
char buf[BUFFSIZE];
while(1)
{
system("cls");/*clrscr();*/
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();
break;
case 'n': /* 按運動員的姓名尋找記錄 */
printf("Please input the athlete's name:\n");
scanf("%s",buf);
SearchbyName(filename,buf);
getch();
break;
case 'c': /* 按運動員的號碼尋找記錄 */
printf("Please input the athlete's code:\n");
scanf("%s",buf);
SearchbyCode(filename,buf);
getch();
break;
case 'l': /* 列出所有運動員記錄 */
Listathleteinfo(filename);
getch();
break;
case 's': /* 按總分從高到低排列顯示 */
if((head=CreatLinklist(filename))!=NULL)
OutputLinklist(head);
getch();
break;
case 'q':
return 1;
default:
break;
}
}
return 1;
}
#include
#include
#include
#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 name[NAMELEN+1]; /* 姓名 */
char code[CODELEN+1]; /* 號碼 */
int score[JUDEGNUM]; /* 各裁判給的成績 */
int total; /* 總成級 */
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)
{
int i;
fprintf(fpt,"%s\n",sturecord->name);
fprintf(fpt,"%s\n",sturecord->code);
for(i=0;i
fprintf(fpt,"%d\n",sturecord->score[i]);
return ;
}
/* 顯示運動員記錄 */
void ShowAthleteRecord(struct AthleteScore *rpt)
{
int i;
printf("\nName : %s\n",rpt->name);
printf("Code : %s\n",rpt->code);
printf("score :\n");
for(i=0;i
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);
return ;
}
while(GetRecord(fp,&s)!=0)
{
ShowAthleteRecord(&s);
}
fclose(fp);
return;
}
/* 構造鏈表 */
struct LinkNode *CreatLinklist(char *fname)
{
FILE *fp;
struct AthleteScore s;
struct LinkNode *p,*u,*v,*h;
int i;
if((fp=fopen(fname,"r"))==NULL)
{
printf("Can't open file %s.\n",fname);
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;
p=(struct LinkNode *)malloc(sizeof(struct LinkNode));
}
free(p);
fclose(fp);
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;
}
return;
}
/* 按運動員姓名查找記錄 */
int SearchbyName(char *fname, char *key)
{
FILE *fp;
int c;
struct AthleteScore s;
system("cls");/*clrscr();*/
if((fp=fopen(fname,"r"))==NULL)
{
printf("Can't open file %s.\n",fname);
return 0;
}
c=0;
while(GetRecord(fp,&s)!=0)
{
if(strcmp(s.name,key)==0)
{
ShowAthleteRecord(&s);
c++;
}
}
fclose(fp);
if(c==0)
printf("The athlete %s is not in the file %s.\n",key,fname);
return 1;
}
/* 按運動員號碼查找記錄 */
int SearchbyCode(char *fname, char *key)
{
FILE *fp;
int c;
struct AthleteScore s;
system("cls");/*clrscr();*/
if((fp=fopen(fname,"r"))==NULL)
{
printf("Can't open file %s.\n",fname);
return 0;
}
c=0;
while(GetRecord(fp,&s)!=0)
{
if(strcmp(s.code,key)==0)
{
ShowAthleteRecord(&s);
c++;
break;
}
}
fclose(fp);
if(c==0)
printf("The athlete %s is not in the file %s.\n",key,fname);
return 1;
}
void InsertRecord()
{
FILE *fp;
char c,i,j,n;
struct AthleteScore s;
system("cls");/*clrscr();*/
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);
for(i=0;i
{
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);
}
fclose(fp);
}
}
fclose(fp);
return;
}
int main()
{
int i,j,n;
char c;
char buf[BUFFSIZE];
while(1)
{
system("cls");/*clrscr();*/
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();
break;
case 'n': /* 按運動員的姓名尋找記錄 */
printf("Please input the athlete's name:\n");
scanf("%s",buf);
SearchbyName(filename,buf);
getch();
break;
case 'c': /* 按運動員的號碼尋找記錄 */
printf("Please input the athlete's code:\n");
scanf("%s",buf);
SearchbyCode(filename,buf);
getch();
break;
case 'l': /* 列出所有運動員記錄 */
Listathleteinfo(filename);
getch();
break;
case 's': /* 按總分從高到低排列顯示 */
if((head=CreatLinklist(filename))!=NULL)
OutputLinklist(head);
getch();
break;
case 'q':
return 1;
default:
break;
}
}
return 1;
}
代碼已經完成,可以自行運行
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。