summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSadeep Madurange <smadurange@users.noreply.github.com>2021-12-21 18:19:50 +0800
committerSadeep Madurange <smadurange@users.noreply.github.com>2021-12-21 18:19:50 +0800
commit92701707e22651a74442b43387f07abf565844cd (patch)
treec1d9e1dbfc9a3dcf02a9ff7935450cfdb2bd148b
parent051151527b7e0e71d0d18836806ab60c14bba0d2 (diff)
downloadk&r-exercises-92701707e22651a74442b43387f07abf565844cd.tar.gz
4.11
-rw-r--r--4/11.c13
1 files changed, 5 insertions, 8 deletions
diff --git a/4/11.c b/4/11.c
index e33ac52..e6c0645 100644
--- a/4/11.c
+++ b/4/11.c
@@ -126,11 +126,9 @@ int getch() { return (bufp > 0) ? buf[--bufp] : getchar(); }
int getop(char s[]) {
int c, i, rc;
- static int pc;
+ static int pc = EOF;
- pc = EOF;
-
- if (pc != EOF) {
+ if (pc != EOF && pc != ' ' && pc != '\t') {
c = pc;
pc = EOF;
} else {
@@ -140,17 +138,16 @@ int getop(char s[]) {
}
// special characters and operators
- if (c == 0 || c == '+' || c == '*' || c == '/' || c == '%' || c == '\n' ||
- (c == '-' && !isdigit(c)) || c == EOF)
+ if (c == '+' || c == '*' || c == '/' || c == '%' || c == '\n' || c == EOF)
return c;
i = 0;
- if (isdigit(c) || c == '-' || c == '.') {
+ if (isdigit(c) || c == '.' || c == '-') {
while (isdigit((s[++i] = c = getch())) || c == '.')
;
pc = c;
- rc = NUM;
+ rc = i == 1 && s[0] == '-' ? '-' : NUM;
} else if (isalpha(c) && getch() == '=') {
s[++i] = c;
rc = '=';