首页 > 编程知识 正文

c语言 atof,atof函数用法

时间:2023-05-04 04:52:38 阅读:239776 作者:1867

// 模拟实现库函数的atof函数

#include

#include

#include

#include

double my_atof(char const *p)

{

double ret = 0;

int flag = 1;

int count = 0;

assert(p != NULL);

while (isspace(*p))

{

p++;

}

while (*p)

{

if (count)

{

count = count * 10;

}

if (*p == '+')

p++;

else if (*p == '-')

{

p++;

flag = -1;

}

else if (*p == '.')

{

count++;

p++;

}

else if (*p >= '0'&& *p <= '9')

{

ret = ret * 10 + (*p - '0');

p++;

}

else

return 0;

}

return ret*flag / count;

}

int main()

{

printf("%fn", my_atof(" +23.45"));

printf("%fn", my_atof(" -2.345"));

printf("%fn", my_atof("+234.5"));

printf("%fn", my_atof("-23.45"));

printf("%fn", my_atof("2.345"));

printf("%fn", my_atof("234.5"));

printf("%fn", my_atof(" ."));

printf("%fn", my_atof("12.3ab"));

return 0;

}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。