在我们说c语言足球比赛积分程序每轮输出小组排名,大家可能都了解,有人想问比赛评分系统C语言,这到底是怎么一回事呢?让我们详细了解一下吧。
#include stdio.h
#include string.h
#include stdlib.h
typedef struct tagTeam {
struct tagTeam *next;
char name[256];
int wins;
int draws;
int losses;
int goal;
int drop;
int goalDiff;
int points;
} Team;
void updateTeam(Team *team, int goal, int drop)
{
team-wins += goal drop ? 1 : 0;
team-draws += goal == drop ? 1 : 0;
team-losses += goal drop ? 1 : 0;
team-goal += goal;
team-drop += drop;
team-goalDiff = team-goal - team-drop;
team-points = team-wins * 3 + team-draws;
}
Team* createEmptyTeam()
{
return (Team *)malloc(sizeof(Team));
}
Team* createTeam(const char *name, int goal, int drop)
{
Team *team = (Team *)malloc(sizeof(Team));
team-next = NULL;
strcpy(team-name, name);
team-wins = 0;
team-draws = 0;
team-losses = 0;
team-goal = 0;
team-drop = 0;
team-goalDiff = 0;
team-points = 0;
updateTeam(team, goal, drop);
return team;
}
void deleteTeam(Team *team)
{
free(team);
}
void appendTeam(Team **list, Team *team)
{
if (*list != NULL)
team-next = *list;
*list = team;
}
Team* findTeam(Team *list, const char *name)
{
Team *team = list;
while (team != NULL) {
if (strcmp(team-name, name) == 0)
break;
team = team-next;
}
return team;
}
int saveTeamList(const Team *list)
{
FILE *file;
const Team *team;
file = fopen("standing.bin", "wb");
if (file == NULL) {
printf("Failed to open the file standing.bin\n");
return 1;
}
team = list;
while (team != NULL) {
if (fwrite(team, sizeof(*team), 1, file) != 1) {
printf("Failed to save the team infomation!\n");
return 2;
}
team = team-next;
}
fclose(file);
return 0;
}
int loadTeamList(Team **list)
{
FILE *file;
Team team;
file = fopen("standing.bin", "rb");
if (file == NULL) {
printf("Failed to open the file standing.bin\n");
return 1;
}
while (1) {
Team *team = createEmptyTeam();
if (fread(team, sizeof(*team), 1, file) == 1) {
team-next = NULL;
appendTeam(list, team);
} else {
deleteTeam(team);
break;
}
}
fclose(file);
return 0;
}
void printTeam(const Team *team)
{
printf(
"Team: %s\n"
" wins: %d\n"
" draws: %d\n"
" losses: %d\n"
" goal: %d\n"
" drop: %d\n"
" goalDiff: %d\n"
" points: %d\n",
team-name, team-wins,
team-draws, team-losses,
team-goal, team-drop,
team-goalDiff, team-points
);
}
void printTeamList(const Team *list)
{
const Team *team = list;
int index = 1;
while (team != NULL) {
printf("%d ", index);
printTeam(team);
team = team-next;
index++;
}
}
void storeTeamInfo(
Team **list,
const char *name,
int goal,
int drop
)
{
Team *team = findTeam(*list, name);
if (team != NULL)
updateTeam(team, goal, drop);
else {
team = createTeam(name, goal, drop);
appendTeam(list, team);
}
}
void parseTeamInfo(
const char *matchResult,
char *aName,
int *goal,
int *drop,
char *bName
)
{
sscanf(matchResult, "%s %d:%d %s", aName, goal, drop, bName);
}
int main(void)
{
Team *teamList = NULL;
FILE *file;
char matchResult[1024];
file = fopen("MatchResult.txt", "r");
if (file == NULL) {
printf("Failed to open the file MatchResult.txt\n");
return 1;
}
// 注意,仅支持文件一行不超过1024个字符
// 同时,每一行的格式必须正确。
while (fgets(matchResult, 1024, file)) {
char aName[256], bName[256];
int goal, drop;
parseTeamInfo(matchResult, aName, goal, drop, bName);
storeTeamInfo(teamList, aName, goal, drop);
storeTeamInfo(teamList, bName, drop, goal);
}
fclose(file);
if (saveTeamList(teamList)) {
printf("Failed to save the team list!\n");
return 2;
}
teamList = NULL;
if (loadTeamList(teamList)) {
printf("Failed to load the team list!\n");
return 3;
}
printTeamList(teamList);
return 0;
}
------------------------
MatchResult.txt文件格式:
Abc 4:1 Def
Def 3:2 hij
限制:球队名中间不能加空格。 且球队名不长于256个字符。
如果球队名中间出现空格,可用“_”代替,或修改parseTeamInfo函数。
256是一个我直接使用的数字,可用宏替代之。
最后,我没有释放内存。
重新描述:
共N队,分为M组,每组K队。所以M*K等于N。
每组有2个种子队,共有2*M个种子队。
每个队有4个属性:
(1)所属洲
(2)是否种子队
(3)赛前排名(仅用于分组)
(4)小组赛积分
#include stdio.h
#define N 32
#define M 8
#define K (N/M)
char bisai[M][K][K];//共M组,每组K对两两比赛,对角线下区域记录3或1或0
//队结构
typedef struct _duiStruct{
char zhou;//所属洲,值为1 2 3 4 5
char isZhongzi;//是否种子队 1是 0非
int paiming;//赛前排名(仅用于分组)
int score;//小组赛积分
}duiStruct;
duiStruct dui[32];
void yilun(){
}
int main()
{
//给N个对随机生成洲号,赛前排名
//赛前排名前2*M的,分配到M组中
//其余的,分配到M组中
for(i=1; i=K; i++)
yilun();
return 0;
}
算法可以这样来想:
1.申请一个数组Team[8];
2.A队有7场比赛(额..是循环赛吗?),输入每场的比赛结果(可以假设胜为1,负为-1).
3.读入数据,用一个选择或是if语句来判断,是1则Team[0]+=3,是0则不做操作.
4.依3之例,依次读入余下6场比赛的结果并处理.
5.依(2,3,4)之例,处理余下7只队伍的比赛结果.
6.为数组Team排序,输出结果.
这还用大师来啊....这不是课后题么....还是谭的....
查相关练习题答案就行了....
算法可以这样来想:
1.申请一个数组Team[8];
2.A队有7场比赛(额..是循环赛吗?),输入每场的比赛结果(可以假设胜为1,负为-1).
3.读入数据,用一个选择或是if语句来判断,是1则Team[0]+=3,是0则不做操作.
4.依3之例,依次读入余下6场比赛的结果并处理.
5.依(2,3,4)之例,处理余下7只队伍的比赛结果.
6.为数组Team排序,输出结果.