diff options
author | sinanmohd <sinan@firemail.cc> | 2023-05-13 22:35:26 +0530 |
---|---|---|
committer | sinanmohd <sinan@firemail.cc> | 2023-05-13 22:35:26 +0530 |
commit | 590c098937e0cfcce4531f86807b030ee345f990 (patch) | |
tree | 91d196912c30e4fd6d1f71aed49a05a44812f075 /7.5 | |
parent | 563cea897121b33cc5e98dc6ed3a36eb39d52766 (diff) |
7.5: v2 using more of sacnf
Diffstat (limited to '7.5')
-rw-r--r-- | 7.5/main.c | 2 | ||||
-rw-r--r-- | 7.5/op.c | 33 |
2 files changed, 14 insertions, 21 deletions
@@ -11,7 +11,7 @@ int main(void) int opd2; int c; - while ((c = getop(op, MAXOP))) { + while ((c = getop(op, MAXOP)) != EOF) { switch (c) { case NUM: push(atof(op)); @@ -4,29 +4,22 @@ char getop(char *op, size_t len) { - size_t i; + int rs; + double f; - *op = '\0'; // if scanf fails - do { - scanf("%c", op); - } while (isblank(*op)); + while ((rs = scanf("%c", op)) != EOF) + if (*op != ' ' && *op != '\t') + break; - if (!isdigit(*op)) - return *op; - - i = 1; - do { - scanf("%c", op + i); - } while (isdigit(op[i]) && ++i < len); + op[1] = '\0'; + if (rs == EOF) + return EOF; - if (op[i] == '.') { - i++; - do { - scanf("%c", op + i); - } while (isdigit(op[i]) && ++i < len); - } + if (!isdigit(*op) && *op != '.') + return *op; - ungetc(op[i], stdin); - op[i] = '\0'; + ungetc(*op, stdin); + scanf("%lf", &f); + snprintf(op, len, "%lf", f); return NUM; } |