summaryrefslogtreecommitdiffstats
path: root/4/10.c
diff options
context:
space:
mode:
authorSadeep Madurange <smadurange@users.noreply.github.com>2021-12-18 14:39:02 +0800
committerSadeep Madurange <smadurange@users.noreply.github.com>2021-12-18 14:39:02 +0800
commit67a788f38119f21b935d57c094314d8ef0d24e5d (patch)
treea4d111200a14f50354799a0c72be2d9bac1ede56 /4/10.c
parentd85198c87e33ab6a784e41733966e3f18011c681 (diff)
downloadk&r-exercises-67a788f38119f21b935d57c094314d8ef0d24e5d.tar.gz
4.10
Diffstat (limited to '4/10.c')
-rw-r--r--4/10.c25
1 files changed, 12 insertions, 13 deletions
diff --git a/4/10.c b/4/10.c
index 6581724..ef7493f 100644
--- a/4/10.c
+++ b/4/10.c
@@ -28,8 +28,6 @@ int main(int argc, char *argv[]) {
int type, op1, op2;
char s[MAXOP];
- printf("Press Ctrl+D to exit\n");
-
while (mgetline()) {
while ((type = getop(s)) != 0) {
switch (type) {
@@ -105,6 +103,7 @@ int main(int argc, char *argv[]) {
case '\n':
lout = pop();
printf("\t%.8g\n", lout);
+ break;
default:
printf("error: unknown command %s\n", s);
}
@@ -132,7 +131,7 @@ int mgetline() {
}
int getop(char s[]) {
- int i, c;
+ int i, c, rc;
while ((s[0] = c = line[idx++]) == ' ' || c == '\t')
;
@@ -140,8 +139,9 @@ int getop(char s[]) {
// special characters and operators
if (c == 0 || c == '+' || c == '*' || c == '/' || c == '%' || c == '\n' ||
- (c == '-' && isdigit(line[idx + 1])) || c == EOF)
+ (c == '-' && !isdigit(line[idx + 1])) || c == EOF) {
return c;
+ }
i = 0;
@@ -149,19 +149,18 @@ int getop(char s[]) {
while (isdigit((s[++i] = c = line[idx++])) ||
(c == '.' && isdigit(line[idx + 1])))
;
- return NUM;
- }
-
- if (isalpha(c) && line[idx + 1] == ' ' && line[idx + 2] == '=') {
+ rc = NUM;
+ } else if (isalpha(c) && line[idx + 1] == ' ' && line[idx + 2] == '=') {
idx += 2;
- return '=';
+ rc = '=';
+ } else {
+ while (isalnum(s[++i] = c = line[idx++]))
+ ;
+ rc = strcmp("LOUT", s) == 0 ? VAR : FUN;
}
- while (isalnum(s[++i] = c = line[idx++]))
- ;
s[i] = 0;
-
- return strcmp("LOUT", s) == 0 ? VAR : FUN;
+ return rc;
}
int sp = 0;