atof函数实现代码,原理
来源:互联网 发布:sql union会去重吗 编辑:程序博客网 时间:2024/06/06 23:16
/*
atof函数实现字符串向double数据的转换,
*/
#include <stdio.h>
double myatof(const char *str);
int main()
{
//char str[20] = "sd sdf32.47e+2df";
char str[20] = "sd sdf32.47e2df";
{
//char str[20] = "sd sdf32.47e+2df";
char str[20] = "sd sdf32.47e2df";
double result = 0.0;
printf("the str is %s\n",str);
result = myatof(str);
printf("the result is %lf\n",result);
return 0;
}
}
double myatof(const char *str)
{
char flag = 0; //表示正数
double res = 0.0;
double d = 10.0;
int e = 0;
while(*str != '\0')
{
if( !(*str >= '0' && *str <= '9')) //找到字符串中的第一个数字
{
str++;
continue;
}
if(*(str-1) == '-')
{
flag = 1; //表示是一个负数
}
while(*str >= '0' && *str <= '9')
{
res = res *10.0 + (*str - '0');
str++;
}
if(*str == '.')
{
str++;
}
while(*str >= '0' && *str <= '9')
{
res = res + (*str - '0')/d;
d = d*10;
str++;
}
if(*str == 'e' || *str == 'E')
{
str++;
if(*str == '+')
{
str++;
while(*str >= '0' && *str <= '9')
{
e = e*10 + (*str - '0');
str++;
}
while(e>0)
{
res = res*10;
e--;
}
}
if(*str == '-')
{
str++;
while(*str >= '0' && *str <= '9')
{
e = e*10 + (*str - '0');
str++;
}
while(e>0)
{
res = res/10;
e--;
}
}
if(*str >= '0' && *str <= '9')
{
while(*str >= '0' && *str <= '9')
{
e = e*10 + (*str - '0');
str++;
}
while(e>0)
{
res = res*10;
e--;
}
}
}
{
char flag = 0; //表示正数
double res = 0.0;
double d = 10.0;
int e = 0;
while(*str != '\0')
{
if( !(*str >= '0' && *str <= '9')) //找到字符串中的第一个数字
{
str++;
continue;
}
if(*(str-1) == '-')
{
flag = 1; //表示是一个负数
}
while(*str >= '0' && *str <= '9')
{
res = res *10.0 + (*str - '0');
str++;
}
if(*str == '.')
{
str++;
}
while(*str >= '0' && *str <= '9')
{
res = res + (*str - '0')/d;
d = d*10;
str++;
}
if(*str == 'e' || *str == 'E')
{
str++;
if(*str == '+')
{
str++;
while(*str >= '0' && *str <= '9')
{
e = e*10 + (*str - '0');
str++;
}
while(e>0)
{
res = res*10;
e--;
}
}
if(*str == '-')
{
str++;
while(*str >= '0' && *str <= '9')
{
e = e*10 + (*str - '0');
str++;
}
while(e>0)
{
res = res/10;
e--;
}
}
if(*str >= '0' && *str <= '9')
{
while(*str >= '0' && *str <= '9')
{
e = e*10 + (*str - '0');
str++;
}
while(e>0)
{
res = res*10;
e--;
}
}
}
return res*(flag?-1:1);
}
}
}
}
阅读全文
0 0
- atof函数实现代码,原理
- atof()函数实现
- atof & aoti 函数实现
- atof函数的实现
- java 实现atof函数
- atof函数的实现
- atoi函数和atof函数实现
- 字符串函数---atof()函数详解及实现
- 自己实现的atof()和atoi()代码
- C语言实现atoi和atof函数
- 编程实现C函数 atoi atof
- 【C语言】编写函数实现库函数atof
- 尝试实现 atoi和atof函数
- C 标准库 atof 函数的实现
- 自己实现的atio()和atof()函数
- atof函数
- 字符串函数---atof()函数详解及实现(完整版)
- C函数的实现(strcpy,atoi,atof,itoa,reverse)
- POJ 2777 Count Color(线段树区间更新)
- 字符类元素个数统计
- Nginx+WSGI生产环境static文件解决
- 选择Python2.x还是应该选择Python3.x
- Mybatis总结《四》实体类和数据库中的字段不对应时的处理
- atof函数实现代码,原理
- C语言:用循环累加
- HDU 4825 Xor Sum (01字典树模板题)
- linux虚拟机网络配置
- [IOS APP]蒋勋说西洋美学史
- 数据分析(入门篇)-第一章-高效处理千万数据-Part2(Microsoft Query工具)
- 大数据环境搭建(测试)
- Angular学习资料
- java去掉空格