Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C atof练习(K&R第4.2节,练习4.2)_C_Arrays - Fatal编程技术网

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

C atof练习(K&R第4.2节,练习4.2),c,arrays,C,Arrays,K&R ex.4.2要求修改给定(非标准)atof函数,该函数缺少处理指数的指数处理机制(如123e6或456e-7)。我添加了一个最小的更改来处理正确输入的、无空格的、单位指数。为了检查它是否工作,我在main中添加了示例输入数据和printf函数。返回值完全不同(有些是零,没有符号或小数,没有明显的关系)。有人能帮我改进一下吗?代码: #include <ctype.h> double antof(char[]); /* name changed to protect the

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

#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);
}
#包括
双反(字符[]);/*改名保护无辜
并避免引用stdlib函数*/
int main()
{
字符putin1[]=“12345”;
字符putin2[]=“987.65”;
字符输入3[]=“-2468”;
字符putin4[]=“12e2”;
字符输入5[]=“-34E-3”;
printf(“%s\t%s\t%s\t%s\t%s\n\n”,putin1,putin2,putin3,putin4,putin5);
双转换1=antof(putin1);
双转换2=自动转换(putin2);
双转换3=自动转换(putin3);
双转换4=antof(putin4);
双转换5=自动转换(putin5);
printf(“%d\t%d\t%d\t%d\t%d\t%d”,converted1,converted2,converted3,converted4,converted5);
返回0;
}
/*atof:将字符串s转换为双精度*/
双antof(字符s[]
{
双val,功率;
int i,符号;
对于(i=0;isspace(s[i]);i++/*跳过空白*/
;
符号=(s[i]='-')?-1:1;
如果(s[i]='+'| | s[i]=='-')
i++;
对于(val=0.0;isdigit(s[i]);i++)
val=10.0*val+(s[i]-“0”);
如果(s[i]=='。)
i++;
对于(power=1.0;isdigit(s[i]);i++{/*跟踪十进制的右侧,不断添加到val*/
val=10.0*val+(s[i]-“0”);/*但不断将幂乘以10以跟踪小数点*/
功率*=10;
}
/*从这里添加来处理科学符号*/
整数指数;
指数积分;
如果(s[i]=“e”| s[i]=“e”)
i++;
如果(s[i]=='-')
指数符号==-1;
i++;
指数=(s[i]-“0”);
/*完整的功能需要存储val和power等数据
但这里我假设一位数的指数,如示例中所示*/
如果(指数符号==-1)
指数=(1/指数);
返回(符号*值/幂)*(10^指数);
}

一如既往地感谢。

修改后的函数:
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);
}

另外,您必须注意,
^
执行
按位异或
,而不是
。您必须使用
math.h
中的
pow
(或者如果您不想使用
pow
,您必须执行重复的乘法或除法)。

修改后的函数:
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);
}

另外,您必须注意,
^
执行
按位异或
,而不是
。您必须使用
math.h
中的
pow
(或者如果您不想使用
pow
,您必须执行重复的乘法或除法)。

我们不是代码审查。如果您的代码正确且有效,您可以在那里尝试。如果您有问题,请阅读并遵循建议。还要正确设置代码的格式。@Olaf的OP说代码工作不正常,这在代码审查中是离题的。我们不是代码审查。如果您的代码正确且有效,您可以在那里尝试。如果您有问题,请阅读并遵循建议。另外,请正确设置代码格式。@Olaf操作人员说代码工作不正常,这可能与代码检查无关。感谢您的帮助,特别是我已经忘记的关于^的更正是按位进行的。如果您认为我的答案有帮助,请接受为答案。谢谢您的帮助,特别是关于^的更正,我已经忘记了。如果你觉得我的回答有帮助,请接受我的回答。