c语言问题,帮我想想办法

Python038

c语言问题,帮我想想办法,第1张

#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是一个我直接使用的数字,可用宏替代之。

最后,我没有释放内存。

lt=100

③i++

④%f

5、

①25

②arrA[0]

③arrA[1]

④i=2

⑤i<25

⑥arrA[i-2]

6、

① n = 0

②i<10

③arrA[n]

④n = i

7、

①dblA = 1

② i<=n

③dblA

④i,jc(i)

以下是经上机调试过的源程序,给你参考参考!

void main()

{

int i

double sum

sum=0i = 1

while(i<=100)

{

sum = sum + i

i++

}

printf("sum=%f\n",sum)

}

5、void main()

{

double arrA[25]

int i

arrA[0] = 1

arrA[1] = 1

for(i=2i<25i++ )

{

arrA[i] = arrA[i-2] + arrA[i-1]

}

for( i = 0i <25i++ )

{

printf("%8.0f",arrA[i])

if( (i+1) % 5 == 0 ) printf("\n")

}

}

6、void main()

{

int arrA[10] =

int n,i

n = 0

for( i = 1i<10i++ )

{

if( arrA[i] >arrA[n])

n = i

}

printf("The max is arrA[%d] = %d",n,arrA[n])

}

7、#include "stdafx.h"

double jc(int n)

{

double dblA = 1

int i

for( i = 1i<=ni++ )

dblA *= i

return dblA

}

void main()

{

int i

for( i = 1i <=20i++ )

printf("%d! = %f\n",i,jc(i))

}