Highscore Table in C
Highscore Table in C
Hi,
This is the source code of a highscore table
The program stores 10 highscores in a table,
can add scores to this table and the program
them, it's based on a simple linked list and
bubble sort algorythm.
in C.
you
sorts
a
p = (SCORE*)malloc(sizeof(SCORE));
fread(&name, sizeof(char), 30, highscore);
fread(&scr, sizeof(int), 1, highscore);
strcpy(p->name, name);
p->score = scr;
scores[i] = p;
//printf("%s %d\n", scores[i]->name, scores[i]->score);
}
fclose(highscore);
return scores;
}
void save_highscore(SCORE** scores) {
FILE*highscore;
highscore = fopen("highscore.dat", "w");
int i;
for (i = 0; i < 11; ++i) {
fwrite(&scores[i]->name, sizeof(char), 30, highscore);
fwrite(&scores[i]->score, sizeof(int), 1, highscore);
}
//qsort(scores, 11, sizeof(SCORE*), vergleich);
//bubble_sort(scores, 11);
fclose(highscore);
}
void add_score(char* name, int score) {
SCORE** scores;
scores = get_scores();
strcpy(scores[10]->name, name);
scores[10]->score = score;
bubble_sort(scores, 11);
save_highscore(scores);
}
void highscore() {
SCORE** scores;
scores = get_scores();
int i;
for (i = 0; i < 10; ++i) {
printf("%s %d\n", scores[i]->name, scores[i]->score);
free(scores[i]);
}
}
-2-