From 590c098937e0cfcce4531f86807b030ee345f990 Mon Sep 17 00:00:00 2001 From: sinanmohd Date: Sat, 13 May 2023 22:35:26 +0530 Subject: 7.5: v2 using more of sacnf --- 7.5/op.c | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) (limited to '7.5/op.c') diff --git a/7.5/op.c b/7.5/op.c index e7640e6..426baad 100644 --- a/7.5/op.c +++ b/7.5/op.c @@ -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; } -- cgit v1.2.3