summaryrefslogtreecommitdiffstats
path: root/4/9.c
diff options
context:
space:
mode:
authorSadeep Madurange <smadurange@users.noreply.github.com>2021-12-12 17:00:18 +0800
committerSadeep Madurange <smadurange@users.noreply.github.com>2021-12-12 17:00:18 +0800
commitcda2a45de836beb9e2760a1956ce1898bcf404df (patch)
treef349156311f31b6cf55c8001e448a077da8f1a92 /4/9.c
parent4c2a037165c21ac8fd1007824be72a2a8d61e74e (diff)
downloadk&r-exercises-cda2a45de836beb9e2760a1956ce1898bcf404df.tar.gz
4.9
Diffstat (limited to '4/9.c')
-rw-r--r--4/9.c31
1 files changed, 8 insertions, 23 deletions
diff --git a/4/9.c b/4/9.c
index 355b29a..0a9fb26 100644
--- a/4/9.c
+++ b/4/9.c
@@ -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;