-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5b0bcf5
commit 23a1842
Showing
17 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
BINARY=minscanf | ||
|
||
include ../../Exercise.mk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# include <stdio.h> | ||
# include "minscanf.h" | ||
|
||
int main () | ||
{ | ||
int r; | ||
char type; | ||
|
||
if ((r = minscanf("type: %c", &type)) == EOF || r != 1) { | ||
printf("Error reading type. Format:\ntype t v0 v1 ... vn\nt - d: decimal int, f: float\n"); | ||
return 0; | ||
} | ||
if (type == 'f') { | ||
float v; | ||
double sum = 0; | ||
while ((r = minscanf("%f", &v)) != EOF && r == 1) | ||
printf("\t%.2f\n", sum += v); | ||
} | ||
else if (type == 'd') { | ||
int v; | ||
long int sum = 0; | ||
while ((r = minscanf("%d", &v)) != EOF && r == 1) | ||
printf("\t%ld\n", sum += v); | ||
} | ||
else | ||
printf("ERROR! Unrecognized type %c.\n", type); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# include <stdio.h> | ||
# include <stdarg.h> | ||
|
||
/* minscanf: minimal scanf with variable argument list */ | ||
int minscanf (char *fmt, ...) | ||
{ | ||
va_list ap; /* points to each annamed arg in turn */ | ||
char *p; | ||
int c, sr, r = 0; | ||
|
||
va_start(ap, fmt); /* make ap point to 1st unnamed arg */ | ||
for (p = fmt; *p; p++) { | ||
if (*p != '%') { | ||
if ((c = getchar()) == EOF) { | ||
va_end(ap); | ||
return EOF; | ||
} | ||
else if (c != *p) { | ||
va_end(ap); | ||
return r; | ||
} | ||
} | ||
else { | ||
switch (*++p) { | ||
case 'o': sr = scanf("%o", va_arg(ap, int*)); break; | ||
case 'd': sr = scanf("%d", va_arg(ap, int*)); break; | ||
case 'u': sr = scanf("%u", va_arg(ap, unsigned int*)); break; | ||
case 'e': | ||
case 'f': | ||
case 'g': sr = scanf("%f", va_arg(ap, float*)); break; | ||
case 'c': sr = scanf("%c", va_arg(ap, char*)); break; | ||
case '%': | ||
if (c == EOF) { | ||
va_end(ap); | ||
return EOF; | ||
} | ||
else if (c != '%') { | ||
va_end(ap); | ||
return r; | ||
} | ||
break; | ||
default: | ||
printf("ERROR! Unsupported conversion %%%c.\n", *p); | ||
va_end(ap); | ||
return EOF; | ||
} | ||
if (sr == EOF) { | ||
va_end(ap); | ||
return EOF; | ||
} | ||
r++; | ||
} | ||
} | ||
va_end(ap); /* clean up when done */ | ||
return r; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# ifndef MINSCANF_H | ||
# define MINSCANF_H | ||
|
||
/* minscanf: minimal scanf with variable argument list */ | ||
int minscanf (char *fmt, ...); | ||
|
||
# endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1 2 3 |
3 changes: 3 additions & 0 deletions
3
chapter_7.input_output/7_4.minscanf/tests/00_err_no_type.tout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Error reading type. Format: | ||
type t v0 v1 ... vn | ||
t - d: decimal int, f: float |
1 change: 1 addition & 0 deletions
1
chapter_7.input_output/7_4.minscanf/tests/01_err_wrong_type.tin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
type X 1 2 3 |
3 changes: 3 additions & 0 deletions
3
chapter_7.input_output/7_4.minscanf/tests/01_err_wrong_type.tout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Error reading type. Format: | ||
type t v0 v1 ... vn | ||
t - d: decimal int, f: float |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
type: d -100 101 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-100 | ||
1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
type: d -100 -10 1 12 123 1234567890 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
-100 | ||
-110 | ||
-109 | ||
-97 | ||
26 | ||
1234567916 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
type: f -100.5 101.05 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-100.50 | ||
0.55 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
type: f -.100 -.10 .1 .12 1.23 12.34567890 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
-0.10 | ||
-0.20 | ||
-0.10 | ||
0.02 | ||
1.25 | ||
13.60 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
1.00 | ||
3.00 |