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 ...
随机推荐
- @Query注解的用法(Spring Data JPA)
参考文章:http://www.tuicool.com/articles/jQJBNv 1. 一个使用@Query注解的简单例子 @Query(value = "select name,au ...
- lvs keepalived 安装配置详解【转】
lvs keepalived 安装配置详解 张映 发表于 2012-06-20 分类目录: 服务器相关 前段时间看了一篇文章,lvs做负载均衡根F5差不多,说实话不怎么相信,因为F5没玩过,也无法比较 ...
- OpenStack Neutron DVR L2 Agent的初步解析 (一)
声明: 本博客欢迎转载,但请保留原作者信息! 作者:林凯 团队:华为杭州OpenStack团队 OpenStack Juno版本号已正式公布,这是这个开源云平台的10个版本号,在Juno版的Neutr ...
- ios AFNetworking 有用篇
在寻常开发中,af是个非常好用的东西.非常喜欢.可是网上的af找了好多都不太全面,不有用.所以我今天做了一个demo.有上传下载的. 比較有用.希望大家可以用到. 去我github下载demo git ...
- GSON 简介 示例
Gson简介 目前解析json最常用的三种工具:org.json(Java常用的解析),fastjson(阿里巴巴出的),Gson(Google出的),解析速度最快的是Gson. Gson的全名为Go ...
- MVC的Model层中的一些便签
由于自己重新接触MVC,所以把Model层里的一些标签给记录下来,方便自己的使用. 这些是自己目前试用过的一些,在以后的工作中我会接着补充进去新的内容
- spring-data-redis问题总结
如何判断一个字符串是否是合法的long类型? 参考文档如下: http://www.oschina.net/question/59889_45179 spring-data-redis http:// ...
- C#获取磁盘列表与信息
方法1:使用Environment //获取当前计算机逻辑磁盘名称列表 String[] drives = Environment.GetLogicalDrives(); Console.WriteL ...
- SQL 查询字段为值不为空
方法一sql="select * from table where id<>null " or sql="select ...
- Swift - 34 - 闭包的基础语法
//: Playground - noun: a place where people can play import UIKit // 初始化一个整数数组 var arr = [1, 3, 5, 7 ...