atof
So given a string like "2.23" your function should return double 2.23. This might seem easy in the first place but this is a highly ambiguous question. Also it has some interesting test cases. So overall a good discussion can revolve around this question. We are not going to support here scientific notation like 1.45e10 etc. We will also not support hex and octal strings just for the sake of simplicity. But these are good assumption to state upfront. Let's write code for this.
double atof(char* num)
{
if (!num || !*num)
return ;
double integerPart = ;
double fractionPart = ;
int divisorForFraction = ;
int sign = ;
bool inFraction = false;
/*Take care of +/- sign*/
if (*num == '-')
{
++num;
sign = -;
}
else if (*num == '+')
{
++num;
}
while (*num != '\0')
{
if (*num >= '' && *num <= '')
{
if (inFraction)
{
/*See how are we converting a character to integer*/
fractionPart = fractionPart* + (*num - '');
divisorForFraction *= ;
}
else
{
integerPart = integerPart* + (*num - '');
}
}
else if (*num == '.')
{
if (inFraction)
return sign * (integerPart + fractionPart/divisorForFraction);
else
inFraction = true;
}
else
{
return sign * (integerPart + fractionPart/divisorForFraction);
}
++num;
}
return sign * (integerPart + fractionPart/divisorForFraction);
}
atof的更多相关文章
- android 5.0以下版本使用atof报错解决
经过测试,如果手机系统在5.0之下,项目project.properties的target若在5.0以上(android-20), NDK 使用atof就会报错: cannot locate symb ...
- C语言atof()函数:将字符串转换为double(双精度浮点数)
头文件:#include <stdlib.h> 函数 atof() 用于将字符串转换为双精度浮点数(double),其原型为:double atof (const char* str); ...
- Linux下c++中的atoi、atol、atoll、atof函数调用实例
本文中调用的四个函数如下: atoi函数:将字符串转化为int类型变量 atol函数:将字符串转化为long类型变量 atoll函数:将字符串转化为long long类型变量 atof函数:将字符串转 ...
- 面试题目-atof与ftoa
/////////////////////////////////////////////////////////////////////////////// // // FileName : ato ...
- C函数的实现(strcpy,atoi,atof,itoa,reverse)
在笔试面试中经常会遇到让你实现C语言中的一些函数比如strcpy,atoi等 1. atoi 把字符串s转换成数字 int Atoi( char *s ) { int num = 0, i = 0; ...
- atoi函数和atof函数
1.函数名:atoi 功能:是把字符串转换成整型数的一个函数,应用在计算机程序和办公软件中 名字来源:alphanumeric to integer 用法:int atoi(const char *n ...
- cannot locate symbol "atof" referenced by错误分析
ndk从r8升级到r10后, 使用eclipse编译出来的so库报错了,加载库的时候报错cannot locate symbol "atof" referenced by 原因:A ...
- 模拟实现C库的atoi、atof和itoa
1.C函数atoi atoi (表示 alphanumeric to integer)是把字符串转换成整型数的一个函数.广泛的应用在计算机程序和办公软件中.atoi( ) 函数会扫描参数 nptr字符 ...
- 字符串常用-----atof()函数,atoi()函数
头文件:#include <stdlib.h>函数 atof() 用于将字符串转换为双精度浮点数(double),其原型为:double atof (const char* str);at ...
随机推荐
- 通过xslt把xml转换成html
将内容与内容的表现分离,软件界自从成为一个行业以来一直在追求的目标. xml+xslt是典型的数据与表现分离的设计方式.当然,你可以直接转换成HTML,但是如果你要进行整体变化的时候,XML+XSLT ...
- Linq to Sql语法及实例大全
LINQ to SQL语句(1)之Where Where操作 适用场景:实现过滤,查询等功能. 说明:与SQL命令中的Where作用相似,都是起到范围限定也就是过滤作用的 ,而判断条件就是它后面所接的 ...
- Java中可变长参数的方法
原文转自:http://www.cnblogs.com/lanxuezaipiao/p/3190673.html 在Java5 中提供了变长参数(varargs),也就是在方法定义中可以使用个数不确定 ...
- 站内信DB设计实现
两年前,万仓一黍在博客园发了两篇关于站内信的设计实现博文,<群发"站内信"的实现>.<群发"站内信"的实现(续)>,其中阐述了他关于站内 ...
- 关于Android Canvas.drawText方法中的坐标参数的正确解释
canvas.drawText("www.jcodecraeer.com", x, y, paint); x和y参数是指定字符串中心的坐标吗?还是左上角的坐标?这个问题的直观印象 ...
- [Redux] Avoiding Array Mutations with concat(), slice(), and ...spread
For Redux, you cannot use mutable methods like push, splice. Need to use immutable methods such as c ...
- sql为了实现转换的行列
全名 学科 成绩 牛芬 语文 81 牛芬 数学 88 牛芬 英语 84 张三 语文 90 张三 数学 98 张三 英语 90 (表一) 现有一个表如(表一) 姓名 语文 数学 英语 牛芬 81 88 ...
- 编译lua版本问题
Compile++ thumb : game_shared <= main.cppjni/hellocpp/main.cpp: In function 'void Java_org_cocos ...
- PHP 将Base64图片保存到 Sae storage
<?php $file_dir='tu/'.date("Y/m/d").'/'; $fileName=create_guid(); $storage = new SaeSto ...
- 关于js跨域
get方式: 称为jsonp,就是js的跨域通信方式,因为知道有些标签可以跨域获取内容,例如img,script,link...,jsonp就是把动态创建一个script标签,然后配置src属性,后台 ...