-
Notifications
You must be signed in to change notification settings - Fork 0
/
golf_handicap_calculator.c
301 lines (249 loc) · 7.71 KB
/
golf_handicap_calculator.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "methods.h"
#define NUM_SCORES (5)
int main() {
char db[5] = "\\\\";
printf("\n\t\tWelcome to the Golf Handicap Calculator (C)\n");
printf("\n");
printf("\t\t # # \n");
printf("\t\t # GHC # \n");
printf("\t\t # # \n");
printf("\t\t # # \n");
printf("\t\t %s // \n", db);
printf("\t\t %s // \n", db);
printf("\t\t %s // \n", db);
printf("\t\t %s // \n", db);
printf("\t\t %s// \n", db);
printf("\t\t // \n");
printf("\t\t //%s \n", db);
printf("\t\t // %s \n", db);
printf("\t\t // %s \n", db);
printf("\t\t // %s \n", db);
printf("\t\t // %s \n", db);
printf("\t\t (###// (*) %s###) \n", db);
printf("\n");
int option = 0;
while (1) {
while (1) {
printf("Select An Option:\n");
printf("1) Record Score and Add Differential to List\n");
printf("2) Calculate Handicap\n");
printf("3) Exit\n");
printf("\nEnter: ");
scanf("%d", &option);
if (option == 1 || option == 2) {
break;
}
if (option == 3) {
return 0;
}
}
if (option == 1) {
printf("Select The Number of Holes Played:\n");
int holes_played = 0;
while (1) {
printf("1) 9 Holes\n");
printf("2) 18 Holes\n");
printf("3) Exit\n");
printf("\nEnter: ");
scanf("%d", &holes_played);
if (holes_played == 1 || holes_played == 2) {
break;
}
if (holes_played == 3) {
return 0;
}
printf("Handicap Calculator: Invalid Input - %d\n", holes_played);
}
printf("\nWhat Is The Course Rating Of the Course You Played?\n");
printf("Rating: ");
float rating = 0.0;
scanf("%f", &rating);
printf("\nWhat Is The Slope Of the Course You Played?\n");
printf("Slope: ");
float slope = 0.0;
scanf("%f", &slope);
printf("\nWhat Was Your Score Today?\n");
printf("Score: ");
float score = 0.0;
scanf("%f", &score);
if (holes_played == 1) {
float nine_differential = calculate_differential_nine(rating, slope, score);
record_differential_nine("nine_hole_differentials", nine_differential);
printf("Differential (%f) has been recorded.\n", nine_differential);
} else {
float eighteen_differential = calculate_differential_eighteen(rating, slope, score);
record_differential_eighteen("eighteen_hole_differentials", eighteen_differential);
printf("Differential (%f) has been recorded.\n", eighteen_differential);
}
}
else {
// File Reading, Saving/Writing Score into file, Checking for 5 scores and discerning
// which 5 scores are the best.
printf("Please Select Your Preferred Handicap: \n");
int handicap_option = 0;
while (1) {
printf("1) 9-Hole Handicap\n");
printf("2) 18-Hole Handicap\n");
printf("3) Exit\n");
printf("Enter: ");
scanf("%d", &handicap_option);
if (handicap_option == 1 || handicap_option == 2) {
break;
}
if (handicap_option == 3) {
return 0;
}
}
////////////////////////////////////////////////////
if (handicap_option == 1) {
float nine_index = fetch_nine_handicap_index("nine_hole_differentials");
printf("\n9-Hole Handicap: %.2f\n\n", nine_index);
if (nine_index == 0.0) {
printf("You do not have enough scores to calculate and accurate 9-hole handicap.\n");
}
} else {
float eighteen_index = fetch_eighteen_handicap_index("eighteen_hole_differentials");
printf("\n18-Hole Handicap: %.2f\n\n", eighteen_index);
if (eighteen_index == 0.0) {
printf("You do not have enough scores to calculate and accurate 18-hole handicap.\n");
}
}
}
}
return 0;
} // Main()
float calculate_differential_nine(float rating, float slope, float score) {
float adjusted_rating = rating / 2;
return (score - adjusted_rating) * (113 / slope);
} // calculate_differential_nine()
float calculate_differential_eighteen(float rating, float slope, float score) {
return (score - rating) * (113 / slope);
} // calculate_differential_eighteen()
void record_differential_nine(char* filename, float diff) {
FILE* write_pointer = fopen(filename, "a");
if (write_pointer == NULL) {
printf("Differential could not be recorded. (Line 150)\n");
return;
}
fprintf(write_pointer, "%f\n", diff);
fclose(write_pointer);
write_pointer = NULL;
} // record_differential_nine
void record_differential_eighteen(char* filename, float diff) {
FILE* write_pointer = fopen(filename, "a");
if (write_pointer == NULL) {
printf("Differential could not be recorded. (Line 162)\n");
return;
}
fprintf(write_pointer, "%f\n", diff);
fclose(write_pointer);
write_pointer = NULL;
} // record_differential_eighteen
float fetch_nine_handicap_index(char* filename) {
FILE* read_pointer = fopen(filename, "r");
if (read_pointer == NULL) {
printf("Differential could not be fetched. (Line 174)\n");
}
int line_count = 0;
float diff = 0.0;
int scan_ret = 0;
while (1) {
scan_ret = fscanf(read_pointer, "%f", &diff);
if (scan_ret == EOF) {
break;
}
line_count++;
}
if (line_count < 5) {
fclose(read_pointer);
read_pointer = NULL;
return 0.0;
}
fseek(read_pointer, 0, SEEK_SET);
float arr[line_count];
int i = 0;
while (1) {
scan_ret = fscanf(read_pointer, "%f", &diff);
if (scan_ret == EOF) {
break;
}
arr[i] = diff;
i++;
}
float* sorted_arr = malloc(line_count * sizeof(float));
sorted_arr = array_sort(arr, line_count);
float arr_sum = 0.0;
for (int i = 0; i < NUM_SCORES; i++) {
arr_sum += sorted_arr[i];
}
float index = arr_sum / NUM_SCORES;
fclose(read_pointer);
read_pointer = NULL;
return index;
} // fetch_nine_handicap_index
float fetch_eighteen_handicap_index(char* filename) {
FILE* read_pointer = fopen(filename, "r");
if (read_pointer == NULL) {
printf("Differential could not be fetched. (Line 227)\n");
}
int line_count = 0;
float diff = 0.0;
int scan_ret = 0;
while (1) {
scan_ret = fscanf(read_pointer, "%f", &diff);
if (scan_ret == EOF) {
break;
}
line_count++;
}
if (line_count < NUM_SCORES) {
fclose(read_pointer);
read_pointer = NULL;
return 0.0;
}
fseek(read_pointer, 0, SEEK_SET);
float arr[line_count];
int i = 0;
while (1) {
scan_ret = fscanf(read_pointer, "%f", &diff);
if (scan_ret == EOF) {
break;
}
arr[i] = diff;
i++;
}
float* sorted_arr = malloc(line_count * sizeof(float));
sorted_arr = array_sort(arr, line_count);
float arr_sum = 0.0;
for (int i = 0; i < NUM_SCORES; i++) {
arr_sum += sorted_arr[i];
}
float index = arr_sum / NUM_SCORES;
fclose(read_pointer);
read_pointer = NULL;
return index;
} // fetch_eighteen_handicap_index
void swap(float* a, float* b) {
float temp = *a;
*a = *b;
*b = temp;
} // swap()
float* array_sort(float* arr, int size) {
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(&arr[j], &arr[j + 1]);
}
}
}
return arr;
} // array_sort()
void print_arr(float* arr, int size) {
printf("\n");
for (int i = 0; i < size; i++) {
printf("%1f ", arr[i]);
}
}