This repository has been archived by the owner on Sep 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ranking.c
128 lines (72 loc) · 2 KB
/
Ranking.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "Ranking.h"
void loadRanking( RankNode *ranking, int *n ){
FILE *fp = fopen( RANKING_DATA_FILE, "r" );
if( fp != NULL ){
int i;
for( i = 0; i < 10; i++ ){
if( fscanf( fp, "%s %d", ranking[i].name, &(ranking[i].score) ) == EOF ){
break;
}
}
*n = i;
fclose( fp );
}else{
*n = 0;
}
}
int isRankedIn( int score ){
RankNode ranking[10];
int length;
loadRanking( ranking, &length );
if( length < 10 ) return 1;
for( int i = 0; i < length; i++ ){
if( ranking[i].score > score ) return 1;
}
return 0;
}
void joinToRanking( char *name, int score ){
RankNode ranking[11];
int length;
loadRanking( ranking, &length );
ranking[length].score = score;
strcpy( ranking[length].name, name );
length++;
sortRanking( ranking, length );
FILE *fp = fopen( RANKING_DATA_FILE, "w" );
if( fp == NULL ){
printf( "ERROR: Cannot open ranking file: %s\n", RANKING_DATA_FILE );
exit( EXIT_FAILURE );
}
for( int i = 0; i < length; i++ ){
if( i >= 10 ) break;
fprintf( fp, "%s %d\n", ranking[i].name, ranking[i].score );
}
fclose( fp );
}
void sortRanking( RankNode *ranking, int length ){
for( int i = 0; i < length; i++ ){
for( int j = 0; j < length - 1; j++ ){
if( ranking[j].score > ranking[j+1].score ){
char n[20];
strcpy( n, ranking[j+1].name );
int s = ranking[j+1].score;
strcpy( ranking[j+1].name, ranking[j].name );
ranking[j+1].score = ranking[j].score;
strcpy( ranking[j].name, n );
ranking[j].score = s;
}
}
}
}
void renderRanking( void ){
RankNode ranking[10];
int length;
loadRanking( ranking, &length );
for( int i = 0; i < length; i++ ){
char d[30];
sprintf( d, "%s %ds", ranking[i].name, ranking[i].score );
if( i < 4 ) RenderText( d, 200, 100 + 40 * i );
else if( i < 7 ) RenderText( d, 380, 100 + 40 * ( i - 4 ) );
else RenderText( d, 600, 100 + 40 * ( i - 7 ) );
}
}