-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
396 lines (334 loc) · 8.85 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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <getopt.h>
#include <time.h>
#define STORAGE_FILENAME "app.dat"
/* Structure to hold an equation (a + b = c) */
typedef struct
{
int a;
int b;
int c;
char operation;
} equation_t;
/* Structure to store a date */
typedef struct
{
int year;
int month;
int day;
} date_t;
/* Structure to maintain program state */
typedef struct
{
equation_t eq;
date_t date;
int solved;
} state_t;
/* Function to get today's date */
void get_today_date(date_t *today)
{
time_t t;
struct tm tm;
t = time(NULL);
tm = *localtime(&t);
today->year = tm.tm_year + 1900;
today->month = tm.tm_mon + 1;
today->day = tm.tm_mday;
}
/* Function to check if a given date is today */
int is_today(date_t date)
{
date_t today;
get_today_date(&today);
return (date.day == today.day &&
date.month == today.month &&
date.year == today.year);
}
/* Function for safe user input */
void myinput(const char *input_format, void *buffer)
{
while (scanf(input_format, buffer) != 1)
{
while (getchar() != '\n'); /* Clear input buffer */
printf("Invalid input! Try again: ");
}
}
/* Load previous state from file */
void load_state(state_t *state)
{
FILE *file;
file = fopen(STORAGE_FILENAME, "r");
if (file == NULL)
{
printf("No previous state found. Starting fresh.\n");
state->solved = 0;
state->eq.a = 0;
state->eq.b = 0;
state->eq.c = 0;
state->eq.operation = 0;
state->date.year = 0;
state->date.month = 0;
state->date.day = 0;
return;
}
fread(state, sizeof(state_t), 1, file);
fclose(file);
}
/* Save the current state to file */
void save_state(state_t *state)
{
FILE *file;
get_today_date(&state->date);
file = fopen(STORAGE_FILENAME, "w");
if (file == NULL)
{
printf("Error opening file for writing!\n");
exit(1);
}
fwrite(state, sizeof(state_t), 1, file);
fclose(file);
}
int is_valid_operation(char op)
{
switch (op)
{
case '+':
return 1;
break;
case '-':
return 1;
break;
case '*':
return 1;
break;
case '/':
return 1;
break;
default:
return 0;
}
}
char input_operation()
{
char operation;
int is_valid;
/* Prompt until a valid operation (+ or -) is provided */
while (1) {
printf("Choose arithmetic operation (+, -, *, /): ");
myinput(" %c", &operation); /* The space before %c is to skip any whitespace */
is_valid = is_valid_operation(operation);
if (is_valid)
{
return operation;
}
printf("Invalid operation. Please enter ('+', '-', '*', '/').\n");
};
}
int input_difficulty()
{
int difficulty;
/* Prompt until a valid difficulty (number of digits, minimum 1) is provided */
do {
printf("Set difficulty (number of digits, must be >= 1): ");
myinput("%d", &difficulty);
if (difficulty < 1) {
printf("Difficulty must be at least 1 digit.\n");
}
} while (difficulty < 1);
return difficulty;
}
void handle_operation(equation_t* eq)
{
switch (eq->operation)
{
case '+':
eq->c = eq->a + eq->b;
break;
case '-':
eq->c = eq->a - eq->b;
break;
case '*':
eq->c = eq->a * eq->b;
break;
case '/':
eq->c = eq->a / eq->b;
break;
default:
exit(1);
}
}
void display_equation(equation_t eq)
{
printf("New equation for today: %d %c %d = ?\n", eq.a, eq.operation, eq.b);
}
/* Generate a new equation based on difficulty and operation preferences */
void new_task(state_t *state)
{
char operation;
int difficulty, mod, iter;
equation_t new_eq;
operation = input_operation();
difficulty = input_difficulty();
/* Compute modulus based on difficulty (e.g., difficulty 3 => mod = 1000) */
mod = 1;
for (iter = 0; iter < difficulty; iter++)
{
mod *= 10;
}
/* Seed the random number generator (ideally do this once in your program) */
srand(time(NULL));
/* Generate random operands within the computed range */
new_eq.a = rand() % mod;
new_eq.b = rand() % mod;
new_eq.operation = operation;
/* Compute the result based on the selected operation */
handle_operation(&new_eq);
/* Update state and save the new equation */
state->eq = new_eq;
state->solved = 0;
save_state(state);
display_equation(new_eq);
}
/* Function to attempt solving the equation */
void attempt_task(state_t *state)
{
int input = -1;
int attempts = 4;
while (input != state->eq.c && attempts > 0)
{
printf("%d %c %d = ", state->eq.a, state->eq.operation, state->eq.b);
myinput("%d", &input);
if (input == state->eq.c)
{
printf("Correct!\n");
state->solved = 1;
save_state(state);
break;
}
else
{
printf("WRONG! Try again\n");
}
attempts--;
}
}
/* Function to zero out a file */
void zero_file(const char *filename, size_t size)
{
FILE *file;
char buffer[8];
size_t iter, remaining;
file = fopen(filename, "w");
if (file == NULL)
{
printf("Error opening file\n");
return;
}
memset(buffer, 0, sizeof(buffer));
remaining = size;
for (iter = 0; iter < (remaining / sizeof(buffer)); iter++)
{
fwrite(buffer, sizeof(buffer), 1, file);
}
fwrite(buffer, 1, remaining % sizeof(buffer), file);
fclose(file);
}
void new_addition_task(state_t *state, char operation, int digits)
{
int difficulty, mod, iter;
equation_t new_eq;
operation = operation;
difficulty = digits;
/* Compute modulus based on difficulty (e.g., difficulty 3 => mod = 1000) */
mod = 1;
for (iter = 0; iter < difficulty; iter++)
{
mod *= 10;
}
/* Seed the random number generator (ideally do this once in your program) */
srand(time(NULL));
/* Generate random operands within the computed range */
new_eq.a = rand() % mod;
new_eq.b = rand() % mod;
new_eq.operation = operation;
/* Compute the result based on the selected operation */
handle_operation(&new_eq);
/* Update state and save the new equation */
state->eq = new_eq;
state->solved = 0;
save_state(state);
display_equation(new_eq);
}
/* Handle command-line arguments */
void handle_cli(int argc, char *argv[])
{
int opt;
char *task_arg = NULL;
char operation;
int digits = 0;
while ((opt = getopt(argc, argv, "hcdt:")) != -1)
{
switch (opt)
{
case 'h':
printf("Usage: %s [-c] [-d] [-t operation:digits]\n", argv[0]);
printf("Options:\n");
printf(" -c Zero \"%s\" file\n", STORAGE_FILENAME);
printf(" -d Delete \"%s\" file\n", STORAGE_FILENAME);
printf(" -t OP:DIGITS Create new task with operation OP and numbers with DIGITS digits\n");
break;
case 'c':
zero_file(STORAGE_FILENAME, sizeof(state_t));
break;
case 'd':
if (remove(STORAGE_FILENAME) == 0)
printf("Deleted file: %s\n", STORAGE_FILENAME);
else
printf("Error deleting file: %s\n", STORAGE_FILENAME);
break;
case 't':
task_arg = optarg;
if (sscanf(task_arg, "%c:%d", &operation, &digits) == 2)
{
state_t state;
new_addition_task(&state, operation, digits);
}
else
{
fprintf(stderr, "Invalid format for -t. Use: operation:digits (e.g., +:3)\n");
exit(EXIT_FAILURE);
}
break;
case '?':
fprintf(stderr, "Unknown option -%c!\n", optopt);
break;
default:
fprintf(stderr, "Usage: %s [-c] [-d] [-t OP:DIGITS]\n", argv[0]);
exit(EXIT_FAILURE);
}
}
}
/* Main function */
int main(int argc, char *argv[])
{
state_t state;
if (argc > 1)
{
handle_cli(argc, argv);
return 0;
}
load_state(&state);
printf("%02d-%02d-%04d\n", state.date.day, state.date.month, state.date.year);
printf("Welcome back!\n");
if (state.solved)
{
new_task(&state);
}
else
{
attempt_task(&state);
}
return 0;
}