-
Notifications
You must be signed in to change notification settings - Fork 0
/
math.c
333 lines (284 loc) · 7.51 KB
/
math.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// Very simple command line arithmatic drills
#include "my_io.h"
#include "math.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <stdbool.h>
#include <limits.h>
#include <math.h>
// returns a random value between a and b
int rand_range(int a, int b)
{
if (a > b) {
int t = a;
a = b;
b = t;
}
return (rand() % (b - a + 1)) + a;
}
// a prompt for each test. checks negative numbers on b, and puts "()" around them.
void prompt_test(int a, int b, char op, const char* eq)
{
if (b < 0) printf("%d %c (%d)%s", a, op, b, eq);
else printf("%d %c %d%s", a, op, b, eq);
}
// addition test
bool test_add(int a, int b)
{
prompt_test(a, b, '+', " = ");
return a + b == get_int();
}
// subtraction test
bool test_sub(int a, int b)
{
prompt_test(a, b, '-', " = ");
return a - b == get_int();
}
// multiplication test
bool test_mul(int a, int b)
{
prompt_test(a, b, '*', " = ");
return a * b == get_int();
}
// calculate the greatest common divisor
int gcd(int a, int b)
{
a = abs(a);
b = abs(b);
if (a < b) {
int t = a;
a = b;
b = t;
}
while (b > 0) {
int r = a % b;
a = b;
b = r;
}
return a;
}
// gcd test
bool test_gcd(int a, int b)
{
printf("gcd(%d, %d) = ", a, b);
return gcd(a, b) == get_int();
}
// returns thesign (1, -1, or 0) of "a"
int sign(int x)
{
return (x > 0) - (x < 0);
}
// tests ability to reduce a fraction
int div_reduce(int a, int b)
{
bool x = true;
printf("reduce ");
prompt_test(a, b, '/', ":\n");
int sign_result = sign(a) * sign(b);
a = abs(a);
b = abs(b);
int gcd_ab = gcd(a, b);
x &= test_gcd(a, b);
int n;
if (x) {
printf("numerator = ");
n = get_int();
}
if (a % b != 0) {
x &= a / gcd_ab == abs(n);
int d;
if (x) {
printf("denominator = ");
d = get_int();
}
x &= b / gcd_ab == abs(d);
x &= sign_result == sign(n) * sign(d);
} else {
x &= sign_result * a / gcd_ab == n;
}
return x;
}
// tests ability to find the remainder of integer division
int div_remainder(int a, int b)
{
printf("remainder = ");
return abs(a) % abs(b) == get_int();
}
// tests the ability to find the proper fraction form of a division operation.
int div_proper(int a, int b)
{
bool x = true;
int sign_result = sign(a) * sign(b);
a = abs(a);
b = abs(b);
int r = a % b;
x &= div_remainder(a, b);
if (a < b) x &= div_reduce(sign_result * r, b);
else x &= div_reduce(r, b);
return x;
}
// divison test. Options (0: REMAINDER, 1: PROPER, 2: IMPROPER)
bool test_div(int a, int b, enum div_options option)
{
// keeps track of the correctness of the answer.
bool x = true;
// if you devide by 0
if (b == 0) {
const char* ans = "undefined";
enum {LEN = 10};
char s[LEN];
get_string(s, LEN);
for (int i = 0; i < LEN; i++) x &= ans[i] == s[i];
// if the reduce only flag is set
} else if (option == REDUCE) x = div_reduce(a, b);
// if the remainder or proper flags are set.
else {
// get the whole part of the result
prompt_test(a, b, '/', " (whole part) = ");
x = (int) (a / b) == get_int();
if ((x == 0) || (a % b == 0)) ; // do nothing
else if (option == REMAINDER) x &= div_remainder(a, b);
else if (option == PROPER) x &= div_proper(a, b);
}
return x;
}
// Generates a selection mask
int test_option_mask()
{
int mask = 0;
do {
mask = prompt_int_ranged("Test Addition (1, 0): ", 0, 1);
mask += prompt_int_ranged("Test Subtraction (1, 0): ", 0, 1) << 1;
mask += prompt_int_ranged("Test Multiplication (1, 0): ", 0, 1) << 2;
mask += prompt_int_ranged("Test Division (1, 0): ", 0, 1) << 3;
mask += prompt_int_ranged("Test GCD (1, 0): ", 0, 1) << 4;
if (mask == 0) printf("(please select at least one test)\n");
} while (mask == 0);
return mask;
}
// saves stats to file
#pragma warning (disable : 4996)
void save_stats(char* stats, char* filename)
{
FILE* file;
file = fopen(filename, "a");
if (file != NULL) {
fputs(stats, file);
fclose(file);
}
}
// Tests users arithmetic skills.
void math_drill(struct settings s, char* name)
{
int c = 0;
time_t t = time(NULL);
time_t t_now = time(NULL);
int t_delta = 1;
srand((int)t);
if (s.questions_max > 0) printf("\nPlease complete the next %d questions:\n", s.questions_max);
else printf("\n");
for (bool answer = true; answer == true && (s.questions_max == 0 || c != s.questions_max);) {
int a = rand_range(s.left_min, s.left_max);
int b = rand_range(s.right_min, s.right_max);
//printf("%d, %d, %d, %d, a: %d, b: %d\n", left_min, left_max, right_min, right_max, a, b); // debug
// randomly choose a test based on option selection
int op = 0;
do op = 1 << (rand() % s.question_types);
while ((op & s.op_mask) == 0);
// select the test based on the binary rep of op
switch (op) {
case 0b00001: answer = test_add(a, b); break;
case 0b00010: answer = test_sub(a, b); break;
case 0b00100: answer = test_mul(a, b); break;
case 0b01000: answer = test_div(a, b, s.div_options); break;
case 0b10000: answer = test_gcd(a, b); break;
}
if (answer) {
c++;
t_now = time(NULL);
// Let the player know some stats every 10 questions
t_delta = (int)(t_now - t) + 1;
if (s.show_stats != 0 && c % s.show_stats == 0)
printf(
"\n%d correct in %d\' %d\". %d answers per minute.\n",
c, t_delta / 60, t_delta % 60, 60 * c / t_delta
);
}
}
// Create a timestamp for today's date
struct tm* timeinfo = localtime(&t_now);
char* timestamp = asctime(timeinfo);
if (timestamp != NULL) timestamp[strlen(timestamp) - 1] = '\0';
// Save stats to file and print it to cmd
enum { STATS_SIZE = 400 };
char stats[STATS_SIZE];
snprintf(
stats, STATS_SIZE,
"%s: %s: %d correct in %d\' %d\". %d answers per minute.\n",
name, timestamp, c, t_delta / 60, t_delta % 60, 60 * c / t_delta
);
save_stats(stats, "stats.txt");
printf("\n");
fputs(stats, stdout);
}
void init_string(char* string, int string_size)
{
for (int i = 0; i < string_size; i++) {
string[i] = ' ';
}
string[string_size - 1] = '\0';
}
void menu()
{
printf("Welcome to Math Drills!\n");
struct settings s;
s.op_mask = 0b01111; // used to mask specific test types
s.left_min = -10;
s.left_max = 10;
s.right_min = -10;
s.right_max = 10;
s.questions_max = 20;
s.options = 0;
s.show_stats = 10; // show test stats every "show_stats" questions
s.div_options = REMAINDER; // default divison test type is proper fractions
s.question_types = 5;
enum { NAME_SIZE = 50 };
char name[NAME_SIZE] = "";
init_string(name, NAME_SIZE);
prompt_string("Enter Your Name: ", name, NAME_SIZE);
enum { QUIT = -1 };
do {
printf("(%d) Quit,\t", QUIT);
s.options = prompt_int(
"(1) Start,\t(2) Test Options,\n"
"(3) Min,\t(4) Max,\t(5) Question Limit,\n"
"(6) Stats,\t(7) Edit Name,\t(8) Division Options: "
);
switch (s.options) {
case QUIT: printf("Goodbye!\n"); break;
case 1:
math_drill(s, name);
break;
case 2: s.op_mask = test_option_mask(); break;
case 3: s.left_min = prompt_int("Left Min: "); s.right_min = prompt_int("Right Min: "); break;
case 4: s.left_max = prompt_int("Left Max: "); s.right_max = prompt_int("Right Max: "); break;
case 5: s.questions_max = prompt_int("Question Limit (0 to disable): "); break;
case 6: s.show_stats = prompt_int("Show stats after every X questions (0 to disable): "); break;
case 7: prompt_string("Enter Your Name: ", name, NAME_SIZE); break;
case 8:
s.div_options = prompt_int_ranged(
"(0) integer with remainder, (1) proper fraction, (2) reduce only: ",
REMAINDER, REDUCE
);
break;
default: printf("(invalid input)\n");
}
} while (s.options != QUIT);
}
int main()
{
menu();
return 0;
}