#include #include #include typedef struct game { char name[8]; // 參賽者姓名 int score; // 得分 } GAME; int main() { GAME *bowling = NULL; int total = 0; int i, j; char input[32]; printf("請逐筆輸入參賽者的資訊...\n"); while (1) { printf("輸入第%d位參賽者名字 得分 => ", 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("配置記憶體錯誤\n"); free(bowling); return 1; } bowling = temp; sscanf(input, "%s %d", bowling[total].name, &bowling[total].score); total++; } printf("\n成績排名\n============\n"); 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; } } } for (i = 0; i < total; i++) { if (i == 0) printf("%d. %s %d 冠軍\n", i + 1, bowling[i].name, bowling[i].score); else if (i == 1) printf("%d. %s %d 亞軍\n", i + 1, bowling[i].name, bowling[i].score); else if (i == 2) printf("%d. %s %d 季軍\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; }