From e6de3d11933b4351a3c8046110c45f4cb801b977 Mon Sep 17 00:00:00 2001 From: Tibor Bizjak Date: Mon, 17 Jul 2023 12:44:19 +0200 Subject: [PATCH] fixed parsef, exponent can now follow integer part --- .../04-calc/04-calc.c | 38 +++++++++---------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/k&r/04-funcs-and-prog-struct/04-calc/04-calc.c b/k&r/04-funcs-and-prog-struct/04-calc/04-calc.c index 1472e8e..c58666c 100644 --- a/k&r/04-funcs-and-prog-struct/04-calc/04-calc.c +++ b/k&r/04-funcs-and-prog-struct/04-calc/04-calc.c @@ -188,27 +188,25 @@ char * parsef(char s[], double *x) int sign = SIGN(s); ATOI(s, *x); -#define X(sep) *s != sep && - if (DEC_SEP 1) { - *x *= sign; - return s; + int exp = 0; + /* fractional part */ +#define X(sep) *s == sep || + if (DEC_SEP 0) { + s++; + ATOI(s, *x, exp--); + *x *= pow(10, exp); + } + *x *= sign; +#undef X + /* exponent part */ +#define X(sep) *s == sep || + if (EXP_SEP 0) { + s++; + sign = SIGN(s); + exp = 0; + ATOI(s, exp); + *x *= pow(10, sign * exp); } #undef X - s++; - /* fractional part */ - int exp = 0; - ATOI(s, *x, exp--); - *x *= sign * pow(10, exp); - -#define X(sep) *s != sep && - if (EXP_SEP 1) - return s; -#undef X - s++; - /* exponent */ - sign = SIGN(s); - exp = 0; - ATOI(s, exp); - *x *= pow(10, sign * exp); return s; }