-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
152 lines (126 loc) · 4.39 KB
/
main.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "lib/medal_analysis.h"
#include "lib/file_db.h"
#include "windows.h"
int calculateTotal(const struct Country* country) {
return country->gold + country->silver + country->bronze;
}
int calculatePoints(const struct Country* country) {
return country->gold * 3 + country->silver * 2 + country->bronze;
}
void printCountry(const struct Country countries[], int count) {
printf(
"\n%-10s%-20s%-10s%-10s%-10s%-15s%-10s\n",
"Rank", "Country", "Gold", "Silver", "Bronze", "Total Medals", "Points"
);
printf("----------------------------------------------------------------------------------------\n");
for (int i = 0; i < count; i++) {
printf("%-10d%-20s%-10d%-10d%-10d%-15d%-10d\n",
countries[i].rank,
countries[i].name,
countries[i].gold,
countries[i].silver,
countries[i].bronze,
countries[i].total,
countries[i].points
);
}
}
void getCountry(struct Country* country) {
printf("Enter country name: ");
scanf("%49s", country->name); // Limit input to 49 characters
printf("Enter number of gold medals: ");
scanf("%d", &country->gold);
printf("Enter number of silver medals: ");
scanf("%d", &country->silver);
printf("Enter number of bronze medals: ");
scanf("%d", &country->bronze);
country->total = calculateTotal(country);
country->points = calculatePoints(country);
}
int compareCountries(const void* a, const void* b) {
const struct Country* countryA = (const struct Country*)a;
const struct Country* countryB = (const struct Country*)b;
if (countryB->points != countryA->points)
return countryB->points - countryA->points;
// If points are equal, sort by gold medals
if (countryB->gold != countryA->gold)
return countryB->gold - countryA->gold;
// If gold medals are equal, sort by silver medals
if (countryB->silver != countryA->silver)
return countryB->silver - countryA->silver;
// If silver medals are equal, sort by bronze medals
return countryB->bronze - countryA->bronze;
}
void clrscr() {
system("@cls||clear");
}
void assignRanks(struct Country countries[], int count) {
for (int i = 0; i < count; i++) {
if (i > 0 && countries[i].points == countries[i - 1].points &&
countries[i].gold == countries[i - 1].gold &&
countries[i].silver == countries[i - 1].silver &&
countries[i].bronze == countries[i - 1].bronze) {
countries[i].rank = countries[i - 1].rank;
}
else {
countries[i].rank = i + 1;
}
}
}
void printMenu(const int count) {
printf("\n\n===========================================\n");
printf("|| Welcome to the Medal Analysis Program ||\n");
printf("===========================================\n\n");
printf(" Total Countries: %d\n", count);
printf(" Main Menu\n");
printf(" 1. Add a country\n");
printf(" 2. View countries\n");
printf(" 3. Show Analytics\n");
printf(" 4. Clear Screen\n");
printf(" 5. Exit\n");
}
int main() {
SetConsoleTitle("Olympic Insides");
struct Country countries[MAX_COUNTRIES];
int count = 0;
char choice;
// Load data from file
count = loadFromFile(countries);
clrscr();
printMenu(count);
while (1) {
printf("\nEnter your choice (1..5) > ");
choice = getchar();
if (choice == '1') {
if (count >= MAX_COUNTRIES) {
printf("Maximum number of countries reached.\n");
continue;
}
getCountry(&countries[count]);
count++;
// Save data to file
saveToFile(countries, count);
}
else if (choice == '2') {
qsort(countries, count, sizeof(struct Country), compareCountries);
assignRanks(countries, count);
printf("\nMedal Table (Sorted by Points, then Gold, Silver, and Bronze):\n");
printCountry(countries, count);
}
else if (choice == '3') {
analyzeResults(countries, count);
}
else if (choice == '4') {
clrscr();
printMenu(count);
}
else if (choice == '5') {
break;
}
while (getchar() != '\n'); // Clear input buffer
}
return 0;
}