-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
820 lines (689 loc) · 20.5 KB
/
Copy pathmain.c
File metadata and controls
820 lines (689 loc) · 20.5 KB
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
int my_scanf(const char *format, ...);
// Test functions
void test_char_multiple();
void test_int_multiple();
void test_string_multiple();
void test_float_multiple();
void test_hex_multiple();
void test_binary();
void test_roman();
void test_word();
int main(void) {
// Redirect standard input to my own text file
freopen("lzbop.txt", "r", stdin);
// Run tests!
test_char_multiple();
test_int_multiple();
test_string_multiple();
test_float_multiple();
test_hex_multiple();
test_binary();
test_roman();
test_word();
}
// Core helper functions
int read_int(FILE *stream, int *value);
int read_char(FILE *stream, char *c);
int read_string(FILE *stream, char *str);
int read_double(FILE *stream, double *value);
int read_hex(FILE *stream, unsigned int *value);
int read_binary(FILE *stream, unsigned int *value);
int read_roman(FILE *stream, int *value);
int read_word(FILE *stream, char *str);
// Ancillary helper functions
void skip_whitespace(FILE *stream);
int is_whitespace(int c);
int is_digit(int c);
int is_hex_digit(int c);
int hex_to_int(int c);
int is_binary_digit(int c);
int roman_to_int(char c);
int is_roman_digit(int c);
int is_word_char(int c);
int my_scanf(const char *format, ...) {
va_list args;
va_start(args, format);
int count = 0; // Number of successful assignments
int i = 0; // Index into format string
while (format[i] != '\0') {
if (format[i] == '%') {
i++; // Move past '%'
switch (format[i]) {
case 'c': {
char *ptr = va_arg(args, char*);
if (read_char(stdin, ptr)) {
count++;
} else {
va_end(args);
return count;
}
break;
}
case 'd': {
int *ptr = va_arg(args, int*);
if (read_int(stdin, ptr)) {
count++;
} else {
va_end(args);
return count;
}
break;
}
case 's': {
char *ptr = va_arg(args, char*);
if (read_string(stdin, ptr)) {
count++;
} else {
va_end(args);
return count;
}
break;
}
case 'f': {
double *ptr = va_arg(args, double*);
if (read_double(stdin, ptr)) {
count++;
} else {
va_end(args);
return count;
}
break;
}
case 'x': {
unsigned int *ptr = va_arg(args, unsigned int*);
if (read_hex(stdin, ptr)) {
count++;
} else {
va_end(args);
return count;
}
break;
}
case '%': {
// Match literal '%'
int c = getc(stdin);
// Skip leading whitespace (like other format specifiers do)
while (is_whitespace(c)) {
c = getc(stdin);
}
// Check if the character is '%'
if (c == '%') {
// Successfully matched, continue.
// Don't increment count - we didn't assign to a variable
} else {
// Mismatch - matching failed
va_end(args);
return count;
}
break;
}
// Custom extensions
case 'b': {
unsigned int *ptr = va_arg(args, unsigned int*);
if (read_binary(stdin, ptr)) {
count++;
} else {
va_end(args);
return count;
}
break;
}
case 'r': {
int *ptr = va_arg(args, int*);
if (read_roman(stdin, ptr)) {
count++;
} else {
va_end(args);
return count;
}
break;
}
case 'w': {
char *ptr = va_arg(args, char*);
if (read_word(stdin, ptr)) {
count++;
} else {
va_end(args);
return count;
}
break;
}
default: {
// Unknown format specifier - stop processing
// This handles cases like %p, %n, %o, etc. that we haven't implemented
va_end(args);
return count;
}
}
i++;
} else if (is_whitespace(format[i])) {
skip_whitespace(stdin);
i++;
} else {
// Match literal character
int c = getchar();
if (c != format[i]) {
// Mismatch - return early
va_end(args);
return count;
}
i++;
}
}
va_end(args);
return count;
}
// CORE HELPER FUNCTIONS //
int read_char(FILE *stream, char *c) {
const int ch = getc(stream);
if (ch == EOF) {
return 0; // Failure - no character available
}
*c = (char)ch; // Store the characters at the pointer location
return 1; // Success
}
int read_int(FILE *stream, int *value) {
int c = getc(stream);
int sign = 1;
int result = 0;
int digit_found = 0;
// Step 1: Skip leading whitespace
while (is_whitespace(c)) {
c = getc(stream);
}
// Step 2: Check for optional sign
if (c == '-') {
sign = -1;
c = getc(stream);
} else if (c == '+') {
c = getc(stream);
}
// Step 3: Read digits
while (is_digit(c)) {
digit_found = 1;
result = result * 10 + (c - '0'); // Build the number
c = getc(stream);
}
// Step 4: Check if we found at least one digit
if (!digit_found) {
return 0; // Failure - no valid integer inputted
}
// Apply positive or negative sign and store result
*value = result * sign;
ungetc(c, stream); // We consumed one too many characters
return 1; // Success
}
int read_string(FILE *stream, char *str) {
int c = getc(stream);
int index = 0;
// Step 1: Skip leading whitespace
while (is_whitespace(c)) {
c = getc(stream);
}
// Step 2: Check for EOF before reading anything
if (c == EOF) {
return 0; // Failure - no string to read
}
// Step 3: Read characters until whitespace or EOF
while (c != EOF && !is_whitespace(c)) {
str[index] = (char)c;
index++;
c = getc(stream);
}
// Step 4: Null-terminate the string
str[index] = '\0';
// Step 5: Put back the whitespace/EOF character
if (c != EOF) {
ungetc(c, stream);
}
// Step 6: Check if we read at least one character
if (index == 0) {
return 0; // Failure - no characters read
}
return 1; // Success
}
int read_double(FILE *stream, double *value) {
int c = getc(stream);
int sign = 1;
double result = 0.0;
int digit_found = 0;
// Step 1: Skip leading whitespace
while (is_whitespace(c)) {
c = getc(stream);
}
// Step 2: Check for optional sign
if (c == '-') {
sign = -1;
c = getc(stream);
} else if (c == '+') {
c = getc(stream);
}
// Step 3: Read integer part
while (is_digit(c)) {
digit_found = 1;
result = result * 10.0 + (c - '0');
c = getc(stream);
}
// Step 4: Check for decimal point
if (c == '.') {
c = getc(stream);
double fraction = 0.1;
// Read fractional part
while (is_digit(c)) {
digit_found = 1;
result = result + (c - '0') * fraction;
fraction = fraction * 0.1;
c = getc(stream);
}
}
// Step 5: Check if we found at least one digit
if (!digit_found) {
return 0; // Failure - no valid float
}
// Step 6: Check for scientific notation (e or E)
if (c == 'e' || c == 'E') {
c = getc(stream);
int exp_sign = 1;
int exponent = 0;
int exp_digit_found = 0;
// Check for optional sign in exponent
if (c == '-') {
exp_sign = -1;
c = getc(stream);
} else if (c == '+') {
c = getc(stream);
}
// Read exponent digits
while (is_digit(c)) {
exp_digit_found = 1;
exponent = exponent * 10 + (c - '0');
c = getc(stream);
}
// If we had 'e' but no valid exponent, that's an error
if (!exp_digit_found) {
return 0;
}
// Apply exponent: result = result * 10^(exp_sign * exponent)
exponent = exponent * exp_sign;
// Calculate 10^exponent
double multiplier = 1.0;
if (exponent > 0) {
for (int i = 0; i < exponent; i++) {
multiplier *= 10.0;
}
} else if (exponent < 0) {
for (int i = 0; i < -exponent; i++) {
multiplier /= 10.0;
}
}
result = result * multiplier;
}
if (c != EOF) {
ungetc(c, stream); // We consumed one too many characters
}
// Apply sign and store result
*value = result * sign;
return 1; // Success
}
int read_hex(FILE *stream, unsigned int *value) {
int c = getc(stream);
unsigned int result = 0;
int digit_found = 0;
// Step 1: Skip leading whitespace
while (is_whitespace(c)) {
c = getc(stream);
}
// Step 2: Optional "0x" or "0X" prefix
if (c == '0') {
int next = getc(stream);
if (next == 'x' || next == 'X') {
// Valid 0x prefix, move to next character
c = getc(stream);
} else {
// Just a '0', not followed by 'x'
// The '0' is a valid hex digit
digit_found = 1;
result = 0;
c = next; // Process the character after '0'
}
}
// Step 3: Read hex digits
while (is_hex_digit(c)) {
digit_found = 1;
result = result * 16 + hex_to_int(c);
c = getc(stream);
}
// Step 4: Check if we found at least one digit
if (!digit_found) {
return 0; // Failure - no valid hex number
}
// Step 5: Put back the non-hex character
if (c != EOF) {
ungetc(c, stream);
}
*value = result;
return 1; // Success
}
int read_binary(FILE *stream, unsigned int *value) {
int c = getc(stream);
unsigned int result = 0;
int digit_found = 0;
// Skip leading whitespace
while (is_whitespace(c)) {
c = getc(stream);
}
// Optional '0b' prefix
if (c == '0') {
int next = getc(stream);
if (next == 'b' || next == 'B') {
c = getc(stream);
} else {
digit_found = 1;
result = 0;
c = next;
}
}
// Read binary digits
while (is_binary_digit(c)) {
digit_found = 1;
result = result * 2 + (c - '0'); // Convert from binary to decimal
c = getc(stream);
}
if (!digit_found) {
return 0;
}
if (c != EOF) {
ungetc(c, stream);
}
*value = result;
return 1;
}
int read_roman(FILE *stream, int *value) {
int c = getc(stream);
int result = 0;
int digit_found = 0;
char roman_str[100];
int index = 0;
// Skip leading whitespace
while (is_whitespace(c)) {
c = getc(stream);
}
// Read all Roman numeral characters into a string first
while (is_roman_digit(c)) {
digit_found = 1;
roman_str[index++] = (char)c;
c = getc(stream);
}
if (!digit_found) {
return 0;
}
roman_str[index] = '\0'; // Null terminate
// Now convert the string
for (int i = 0; i < index; i++) {
int current = roman_to_int(roman_str[i]);
// Look ahead to next character
if (i + 1 < index) {
int next = roman_to_int(roman_str[i + 1]);
// If current is less than next, it's subtractive (IV, IX, XL, etc.)
if (current < next) {
result += (next - current);
i++;
continue;
}
}
// Normal case: just add the value
result += current;
}
if (c != EOF) {
ungetc(c, stream);
}
*value = result;
return 1;
}
int read_word(FILE *stream, char *str) {
int c = getc(stream);
int index = 0;
// Skip leading whitespace
while (is_whitespace(c)) {
c = getc(stream);
}
if (c == EOF) {
return 0;
}
// Read word characters
while (c != EOF && is_word_char(c)) {
str[index] = (char)c;
index++;
c = getc(stream);
}
// Null terminate
str[index] = '\0';
if (c != EOF) {
ungetc(c, stream);
}
if (index == 0) {
return 0;
}
return 1; // Success
}
// ANCILLARY HELPER FUNCTIONS //
int is_whitespace(int c) {
return (c == ' ' || c == '\n' || c == '\t' \
|| c == '\r' || c == '\f' || c == '\v');
}
void skip_whitespace(FILE *stream) {
int c = getc(stream);
while (is_whitespace(c)) { // If c is still whitespace
c = getc(stream); // Keep reading
}
ungetc(c, stream); // Put back the first non whitespace character
}
int is_digit(int c) {
return (c >= '0' && c <= '9');
}
int is_hex_digit(int c) {
return (c >= '0' && c <= '9') || \
(c >= 'a' && c <= 'f') || \
(c >= 'A' && c <= 'F');
}
int hex_to_int(int c) {
if (c >= '0' && c <= '9') {
return c - '0';
} else if (c >= 'a' && c <= 'f') {
return c - 'a' + 10; // 'a' = 10, 'b' = 11, etc.
} else if (c >= 'A' && c <= 'F') {
return c - 'A' + 10; // 'A' = 10, 'B' = 11, etc.
}
return 0;
}
int is_binary_digit(int c) {
return (c == '0' || c == '1');
}
int roman_to_int(char c) {
switch (c) {
case 'I': return 1;
case 'V': return 5;
case 'X': return 10;
case 'L': return 50;
case 'C': return 100;
case 'D': return 500;
case 'M': return 1000;
default: return 0;
}
}
int is_roman_digit(int c) {
return (c == 'I' || c == 'V' || c == 'X' || c == 'L' || c == 'C' || c == 'D' || c == 'M');
}
// Only considers alphanumeric & underscore characters
int is_word_char(int c) {
return (c >= 'a' && c <= 'z') || \
(c >= 'A' && c <= 'Z') || \
(c >= '0' && c <= '9') || \
(c == '_');
}
// BELOW ARE MY TEST FUNCTIONS //
void test_char_multiple() {
printf("Testing multiple characters\n");
int test_cases = 62;
for (int i = 0; i < test_cases; i++) {
char c1;
char c2;
scanf("%c", &c1);
ungetc(c1, stdin); // reset so that my_scanf() reads same character
my_scanf("%c", &c2);
if (c1 == c2) {
printf("PASS (scanf = %c my_scanf = %c)\n", c1, c2);
} else {
printf("FAIL (scanf = %c my_scanf = %c\n", c1, c2);
}
}
printf("\n");
}
void test_int_multiple() {
printf("Testing multiple integers\n");
int test_cases = 18;
for (int i = 0; i < test_cases; i++) {
// Save file position
fpos_t pos;
fgetpos(stdin, &pos);
// Test scanf
int d1;
scanf("%d", &d1);
// Restore position
fsetpos(stdin, &pos);
// Test my_scanf
int d2;
my_scanf("%d", &d2);
// Compare & raise potential error
if (d1 == d2) {
if (!is_digit(d1)) {
printf("PASS (not an int)\n"); // Acknowledge noninteger input
}
printf("PASS (scanf = %d my_scanf = %d)\n", d1, d2);
} else {
printf("FAIL (scanf = %d my_scanf = %d)\n", d1, d2);
}
}
printf("\n");
}
void test_string_multiple() {
printf("Testing multiple strings\n");
// This is my third test function, so I expect scanf and my_scanf to read from third line of lzbop.txt
int num_tests = 4;
for (int i = 0; i < num_tests; i++) {
char *expected[] = {"Let's", "test", "string", "functionality."};
fpos_t pos;
fgetpos(stdin, &pos);
char s1[100];
int ret1 = scanf("%s", s1);
fsetpos(stdin, &pos);
char s2[100];
int ret2 = my_scanf("%s", s2);
if (ret1 == ret2 && strcmp(s1, s2) == 0 && strcmp(s1, expected[i]) == 0) {
printf("PASS (%s)\n", s2);
} else {
printf("FAIL (expected='%s', scanf='%s', my_scanf='%s'\n", expected[i], s1, s2);
}
}
printf("\n");
}
void test_float_multiple() {
printf("Testing multiple floats\n");
int num_tests = 5;
for (int i = 0; i < num_tests; i++) {
fpos_t pos;
fgetpos(stdin, &pos);
double f1;
int ret1 = scanf("%lf", &f1);
fsetpos(stdin, &pos);
double f2;
int ret2 = my_scanf("%f", &f2);
if (ret1 == ret2) {
// Compare floats with small tolerance for rounding errors
double diff = f1 - f2;
if (diff < 0) diff = -diff; // Convert difference to absolute value
if (diff < 0.0001) {
printf("PASS (%.6f)\n", f1);
} else {
printf("FAIL (scanf=%.6f, my_scanf=%.6f, diff=%.6f)\n", f1, f2, diff);
}
} else {
printf("FAIL (scanf returned %d, my_scanf returned %d)\n", ret1, ret2);
}
}
printf("\n");
}
void test_hex_multiple() {
printf("Testing multiple hex values\n");
int num_tests = 6;
for (int i = 0; i < num_tests; i++) {
fpos_t pos;
fgetpos(stdin, &pos);
unsigned int h1;
int ret1 = scanf("%x", &h1);
fsetpos(stdin, &pos);
unsigned int h2;
int ret2 = my_scanf("%x", &h2);
if (ret1 == ret2 && h1 == h2) {
printf("PASS (%u or 0x%x)\n", h1, h1);
} else if (ret1 == ret2 && ret1 == 0) {
printf("PASS (both failed to read)\n");
} else {
printf("FAIL (scanf=%u/0x%x (ret=%d), my_scanf=%u/0x%x (ret=%d))\n", h1, h1, ret1, h2, h2, ret2);
}
}
printf("\n");
}
void test_binary() {
printf("Testing %%b (binary - CUSTOM EXTENSION)\n");
int num_tests = 4;
for (int i = 0; i < num_tests; i++) {
unsigned int expected[] = {10, 13, 255, 7}; // 1010, 1101, 11111111, 111
unsigned int val;
int ret = my_scanf("%b", &val);
if (ret == 1 && val == expected[i]) {
printf("PASS (read %u = ", val);
// Print in binary for verification
for (int j = 7; j >= 0; j--) {
printf("%d", (val >> j) & 1);
}
printf(")\n");
} else {
printf("FAIL (got %u)\n", val);
}
}
printf("\n");
}
void test_roman() {
printf("Testing %%r (roman numerals - CUSTOM EXTENSION)\n");
int num_tests = 6;
for (int i = 0; i < num_tests; i++) {
int expected[] = {1, 4, 9, 58, 1994, 3999}; // I, IV, IX, LVIII, MCMXCIV, MMMCMXCIX
int val;
int ret = my_scanf("%r", &val);
if (ret == 1 && val == expected[i]) {
printf("PASS (read %d)\n", val);
} else {
printf("FAIL (got %d)\n", val);
}
}
printf("\n");
}
void test_word() {
printf("Testing %%w (word/identifier - CUSTOM EXTENSION)\n");
// Test 1: Simple word
char word1[100];
my_scanf("%w", word1);
printf("Test 1: '%s' (expected simple word)\n", word1);
// Test 2: Word with underscore
char word2[100];
my_scanf("%w", word2);
printf("Test 2: '%s' (expected word with underscore)\n", word2);
// Test 3: Word with numbers
char word3[100];
my_scanf("%w", word3);
printf("Test 3: '%s' (expected word with numbers)\n", word3);
}