gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 04:25:39 27 4
gpt4 key购买 nike

K&R 前。 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);
}

一如既往的感谢。

最佳答案

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

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

关于c - atof 练习(K&R 第 4.2 节,练习 4.2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41867199/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com