diff options
| author | Sadeep Madurange <smadurange@users.noreply.github.com> | 2021-12-12 17:00:18 +0800 |
|---|---|---|
| committer | Sadeep Madurange <smadurange@users.noreply.github.com> | 2021-12-12 17:00:18 +0800 |
| commit | cda2a45de836beb9e2760a1956ce1898bcf404df (patch) | |
| tree | f349156311f31b6cf55c8001e448a077da8f1a92 /4/9.c | |
| parent | 4c2a037165c21ac8fd1007824be72a2a8d61e74e (diff) | |
| download | k&r-exercises-cda2a45de836beb9e2760a1956ce1898bcf404df.tar.gz | |
4.9
Diffstat (limited to '4/9.c')
| -rw-r--r-- | 4/9.c | 31 |
1 files changed, 8 insertions, 23 deletions
@@ -155,31 +155,13 @@ void clear() { ; } -int pushb = 0; -int pushbval = 0; - -int getch() { - if (pushb) { - pushb = 0; - return pushbval; - } - return (bufp > 0) ? buf[--bufp] : getchar(); -} +int getch() { return (bufp > 0) ? buf[--bufp] : getchar(); } void ungetch(int c) { - pushbval = c; - pushb = 1; -} - -void ungets(char s[]) { - int len, i; - len = strlen(s); - if (bufp + len - 1 >= BUFSIZE) - printf("ungets: too many characters\n"); - else { - for (i = len; i >= 0; i--) - buf[bufp++] = s[i]; - } + if (bufp >= BUFSIZE) + printf("ungetch: too many characters\n"); + else + buf[bufp++] = c; } int getop(char s[]) { @@ -189,6 +171,9 @@ int getop(char s[]) { ; s[1] = 0; + if (c == EOF) + return EOF; + // operators or line feed if (c == '\n' || c == '+' || c == '*' || c == '/' || c == '%') return c; |
