atof exercise(K& R第4.2节,练习4.2)

时间:2017-01-26 05:02:57

标签: c arrays

K& R ex。 4.2要求您修改一个给定的(非标准)atof函数,该函数缺少指数处理机制来处理指数(如123e6或456e-7)。我添加了一个最小的更改来处理正确输入,无空间,单位数的指数。为了检查它是否正常工作,我将示例输入数据和printf函数添加到main。返回值一直都是关闭的(有些是零,没有符号或小数,没有明显的关系)。有人可以帮我改进吗?代码:

#include <ctype.h>

double antof(char[]);  /* name changed to protect the innocent
                        and avoid references to stdlib functions */

int main()
{
    char putin1[] = "12345";
    char putin2[] = "987.65";
    char putin3[] = "  -2468";
    char putin4[] = "12e2";
    char putin5[] = "  -34E-3";
    printf ("%s \t %s \t %s \t %s \t %s \n\n", putin1, putin2, putin3, putin4, putin5);

    double converted1 = antof(putin1);
    double converted2 = antof(putin2);
    double converted3 = antof(putin3);
    double converted4 = antof(putin4);
    double converted5 = antof(putin5);
    printf ("%d \t %d \t %d \t %d \t %d", converted1, converted2, converted3, converted4, converted5);

    return 0;
}

/* atof: convert string s to double */
double antof(char s[])

{
    double val, power;
    int i, sign;

    for (i = 0; isspace(s[i]); i++) /* skip white space */
        ;
    sign = (s[i] == '-') ? -1 : 1;

    if (s[i] == '+' || s[i] == '-')
        i++;

    for (val = 0.0; isdigit(s[i]); i++)
        val = 10.0 * val + (s[i] - '0');

    if (s[i] == '.')
        i++;

    for (power = 1.0; isdigit(s[i]); i++) {   /*tracks right side of decimal, keeps adding to val */
        val = 10.0 * val + (s[i] - '0');      /* but keeps multiplying power by 10 to keep track of decimal place */
        power *= 10;
    }

    /* added from here to handle scientific notation */
    int exponenty;
    int exponentysign;

    if (s[i] == "e" || s[i] == "E")
        i++;

    if (s[i] == '-')
        exponentysign == -1;
        i++;

    exponenty = (s[i] - '0');
        /* full functionality would require storing data like val and power
        but here I assume single digit exponent as given in the example */

    if (exponentysign == -1)
        exponenty = (1 / exponenty);

    return (sign * val / power) * (10^exponenty);
}

一如既往地谢谢。

1 个答案:

答案 0 :(得分:0)

修改后的功能:antof

double antof(char s[])
{
    double val = 0.0, power = 1;
    int i, sign;

    /* skip white space */
    for (i = 0; isspace(s[i]); i++);

    sign = (s[i] == '-') ? -1 : 1;
    if (s[i] == '+' || s[i] == '-')  i++;

    for (val = 0.0; isdigit(s[i]); i++)
        val = 10.0 * val + (s[i] - '0');

    if (s[i] == '.')
    {
        i++;

        for (power = 10.0; isdigit(s[i]); i++) {  /*tracks right side of decimal, keeps adding to val */
            val = 10.0 * val + (s[i] - '0');      /* but keeps multiplying power by 10 to keep track of decimal place */
            power *= 10;
        }
    }

    /* added from here to handle scientific notation */
    double exponenty = 0;
    int exponentysign = 1;

    if (s[i] == 'e' || s[i] == 'E')
    {
        i++;

        if (s[i] == '-')
        {
            exponentysign = -1;
            i++;
        }

        exponenty = (s[i] - '0');
        /* full functionality would require storing data like val and power
        but here I assume single digit exponent as given in the example */

        exponenty *= exponentysign;
    }

    return (sign * val / power) * pow(10.0, exponenty);
}

此外,您还要注意^执行bitwise xor而非power。您已使用pow中的math.h(或者,如果您不想使用pow,则可以执行重复的乘法或除法)。