Data_Structure/作業/unit0/bowling_2.c

68 lines
1.7 KiB
C
Raw Normal View History

2025-01-20 21:30:53 +08:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct game {
char name[8]; // <20><><EFBFBD>ɪ̩m<CCA9>W
int score; // <20>o<EFBFBD><6F>
} GAME;
int main() {
GAME *bowling = NULL;
int total = 0;
int i, j;
char input[32];
printf("<EFBFBD>гv<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>J<EFBFBD><EFBFBD><EFBFBD>ɪ̪<EFBFBD><EFBFBD><EFBFBD><EFBFBD>T...\n");
while (1) {
printf("<EFBFBD><EFBFBD><EFBFBD>J<EFBFBD><EFBFBD>%d<><64><EFBFBD><EFBFBD><EFBFBD>ɪ̦W<CCA6>r <20>o<EFBFBD><6F> => ", total + 1);
if (fgets(input, sizeof(input), stdin) == NULL) {
break;
}
if (strcmp(input, "\n") == 0) {
break;
}
GAME *temp = realloc(bowling, (total + 1) * sizeof(GAME));
if (temp == NULL) {
printf("Memory allocation error\n");
free(bowling);
return 1;
}
bowling = temp;
sscanf(input, "%s %d", bowling[total].name, &bowling[total].score);
total++;
}
printf("\n<EFBFBD><EFBFBD><EFBFBD>Z<EFBFBD>ƦW\n============\n");
// Sorting
for (i = 0; i < total - 1; i++) {
for (j = 0; j < total - i - 1; j++) {
if (bowling[j].score < bowling[j+1].score) {
GAME temp = bowling[j];
bowling[j] = bowling[j+1];
bowling[j+1] = temp;
}
}
}
// Printing results
for (i = 0; i < total; i++) {
if (i == 0)
printf("%d. %s %d <20>a<EFBFBD>x\n", i + 1, bowling[i].name, bowling[i].score);
else if (i == 1)
printf("%d. %s %d <20>ȭx\n", i + 1, bowling[i].name, bowling[i].score);
else if (i == 2)
printf("%d. %s %d <20>u<EFBFBD>x\n", i + 1, bowling[i].name, bowling[i].score);
else
printf("%d. %s %d\n", i + 1, bowling[i].name, bowling[i].score);
}
free(bowling);
return 0;
}