diff options
author | zlg <zlg@zlg.space> | 2013-08-13 04:37:02 -0500 |
---|---|---|
committer | zlg <zlg@zlg.space> | 2013-08-13 04:37:02 -0500 |
commit | fcbbe1a17fcb9fa129b737ddb8d72683ec9bfc3b (patch) | |
tree | e164081e82c00b980a6e1e4c15da582505c37997 | |
parent | Solve Exercise 4-14: Swap Macro (diff) | |
download | knr-fcbbe1a17fcb9fa129b737ddb8d72683ec9bfc3b.tar.gz knr-fcbbe1a17fcb9fa129b737ddb8d72683ec9bfc3b.tar.bz2 knr-fcbbe1a17fcb9fa129b737ddb8d72683ec9bfc3b.tar.xz knr-fcbbe1a17fcb9fa129b737ddb8d72683ec9bfc3b.zip |
Solve Exercise 5-1: fixed getint()
-rw-r--r-- | README.mdown | 30 | ||||
-rw-r--r-- | ch5/5-01_getint-fixed.c | 65 |
2 files changed, 82 insertions, 13 deletions
diff --git a/README.mdown b/README.mdown index f5d961a..4b12335 100644 --- a/README.mdown +++ b/README.mdown @@ -1,16 +1,20 @@ These files are my solutions to Kernighan and Ritchie's *The C Programming -Language, 2nd Edition*. My aim is to deepen my understanding of C, using only -the concepts covered in content that comes before each exercise. That means no -"cheating" by using standard library features that aren't covered in the book -yet. As a result, many of my solutions will probably not be "the best", but -suitable for the goal at hand: *learning*. +Language, 2nd Edition*. The aim is to deepen my understanding of C, using only +the concepts covered in content that comes before each exercise. That means +no "cheating" by using standard library features that aren't covered in the +book yet. According to [`comp.lang.c`][clc], this is known as *Category-0 +compliant*. As a result, many of my solutions will probably not be "the best", +but suitable for the goal at hand: *learning*. -I chose this route because as I was reading the K&R, I didn't feel like I was -learning anything new, but I knew C was known for its simplicity and efficiency. -If I wasn't learning anything about efficiency and simplifying my programs, then -why was I learning C? I tackled a few exercises and found that I was beginning -to learn a few things I hadn't ran into before. +I chose this route because as I read through the K&R, I didn't feel like I +was learning anything new. C is known for its simplicity and efficiency, so +I thought, "If I'm not learning anything about efficiency and simplifying +my programs, then why am I learning C?" I tackled a few exercises and found +that I was beginning to learn a few things I hadn't ran into before, and this +repository was born. -Critique is welcome, but please keep in mind the limitations I've outlined in -the first paragraph. Also note that some incomplete implementations are noted, -which I intend on revisiting. Any suggestions and advice are welcome. +Critique is welcome, but please keep in mind the limitations I've outlined in +the first paragraph. Some incomplete implementations are noted, which I intend +on revisiting or annotating. Any questions, suggestions, and advice are welcome. + +[clc]: http://clc-wiki.net/wiki/K%26R2_solutions:Ancillary:Category_numbers diff --git a/ch5/5-01_getint-fixed.c b/ch5/5-01_getint-fixed.c new file mode 100644 index 0000000..8d53ba6 --- /dev/null +++ b/ch5/5-01_getint-fixed.c @@ -0,0 +1,65 @@ +#include <stdio.h> +#include <ctype.h> + +/* The C Programming Language: 2nd Edition + * + * Exercise 5-1: As written, getint treats a + or - not followed by a digit as + * a valid representation of zero. Fix it to push such a character back on the + * input. + */ + +#define BUFSIZE 100 + +int getch(void); +void ungetch(int); +int getint(int *); + +char buf[BUFSIZE]; +int bufp = 0; + +int main() { + int foo; + int *bar = &foo; + while (getint(bar) > 0) { + printf("%d\n", foo); + } + return 0; +} + +int getint(int *pn) { + int c, sign; + + while (isspace(c = getch())) { + } + if (!isdigit(c) && c != EOF && c != '+' && c != '-') { + ungetch(c); + return 0; + } + sign = (c == '-') ? -1 : 1; + if (c == '+' || c == '-') { + c = getch(); + } + if (!isdigit(c)) { + return 0; + } + for (*pn = 0; isdigit(c); c = getch()) { + *pn = 10 * *pn + (c - '0'); + } + *pn *= sign; + if (c != EOF) { + ungetch(c); + } + return c; +} + +int getch(void) { + return (bufp > 0) ? buf[--bufp] : getchar(); +} + +void ungetch(int c) { + if (bufp >= BUFSIZE) { + printf("ungetch: Too many characters.\n"); + } else { + buf[bufp++] = c; + } +} |