diff options
| author | Sadeep Madurange <smadurange@users.noreply.github.com> | 2021-12-16 17:44:00 +0800 |
|---|---|---|
| committer | Sadeep Madurange <smadurange@users.noreply.github.com> | 2021-12-16 17:44:00 +0800 |
| commit | 19ef1c01b28a720217978c02b363d2bccfc99563 (patch) | |
| tree | 21647dacecc994e432832505002c9936308c5050 /4/10.c | |
| parent | 5d759f2deab8f7837b74d800525729624c5b7e80 (diff) | |
| download | k&r-exercises-19ef1c01b28a720217978c02b363d2bccfc99563.tar.gz | |
4.10
Diffstat (limited to '4/10.c')
| -rw-r--r-- | 4/10.c | 46 |
1 files changed, 20 insertions, 26 deletions
@@ -12,6 +12,8 @@ #define FUN '1' #define VAR '2' +int var[52]; + int getop(char[]); void push(double); double peek(); @@ -93,7 +95,7 @@ int main(int argc, char *argv[]) { return 0; } -int ptr = 0; +int idx = 0; int line[MAXLINE]; void mgetline() { @@ -103,38 +105,30 @@ void mgetline() { line[i] = c; } line[i] = 0; - ptr = 0; + idx = 0; } int getop(char s[]) { int i, c; - if (line[ptr] == 0) - mgetline(); - - // get next token - for (i = 0; i < MAXOP - 1 && (c = line[ptr]) != ' ' && c != 0; i++, ptr++) - s[i] = c; - s[i] = 0; - - // opertor, single digit or variable - if (i == 1) { - if (isdigit(s[0])) - return NUM; - else if (isalpha(s[0])) - return VAR; - else - return s[0]; - } - - if (i > 1 && (isdigit(s[0]) || s[0] == '-' && isdigit(s[1]))) + while ((s[0] = c = line[idx++]) == ' ' || c == '\t') + ; + s[1] = 0; + + // special characters and operators + if (c == 0 || c == '+' || c == '*' || c == '/' || c == '%' || c == '=' || + (c == '-' && isdigit(line[idx + 1])) || c == EOF) + return c; + + i = 0; + + if (isdigit(c) || c == '-' || (c == '.' && isdigit(line[idx + 1]))) { + while (isdigit((s[++i] = c = line[idx++])) || (c == '.' && isdigit(line[idx + 1]))) + ; return NUM; + } - // last printed variable - if (strcmp("LOUT", s) == 0) - return VAR; - - return FUN; + } int sp = 0; |
