1. 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.
  1. double atof(char* num)
  2. {
  3. if (!num || !*num)
  4. return ;
  5. double integerPart = ;
  6. double fractionPart = ;
  7. int divisorForFraction = ;
  8. int sign = ;
  9. bool inFraction = false;
  10. /*Take care of +/- sign*/
  11. if (*num == '-')
  12. {
  13. ++num;
  14. sign = -;
  15. }
  16. else if (*num == '+')
  17. {
  18. ++num;
  19. }
  20. while (*num != '\0')
  21. {
  22. if (*num >= '' && *num <= '')
  23. {
  24. if (inFraction)
  25. {
  26. /*See how are we converting a character to integer*/
  27. fractionPart = fractionPart* + (*num - '');
  28. divisorForFraction *= ;
  29. }
  30. else
  31. {
  32. integerPart = integerPart* + (*num - '');
  33. }
  34. }
  35. else if (*num == '.')
  36. {
  37. if (inFraction)
  38. return sign * (integerPart + fractionPart/divisorForFraction);
  39. else
  40. inFraction = true;
  41. }
  42. else
  43. {
  44. return sign * (integerPart + fractionPart/divisorForFraction);
  45. }
  46. ++num;
  47. }
  48. return sign * (integerPart + fractionPart/divisorForFraction);
  49. }

atof的更多相关文章

  1. android 5.0以下版本使用atof报错解决

    经过测试,如果手机系统在5.0之下,项目project.properties的target若在5.0以上(android-20), NDK 使用atof就会报错: cannot locate symb ...

  2. C语言atof()函数:将字符串转换为double(双精度浮点数)

    头文件:#include <stdlib.h> 函数 atof() 用于将字符串转换为双精度浮点数(double),其原型为:double atof (const char* str); ...

  3. Linux下c++中的atoi、atol、atoll、atof函数调用实例

    本文中调用的四个函数如下: atoi函数:将字符串转化为int类型变量 atol函数:将字符串转化为long类型变量 atoll函数:将字符串转化为long long类型变量 atof函数:将字符串转 ...

  4. 面试题目-atof与ftoa

    /////////////////////////////////////////////////////////////////////////////// // // FileName : ato ...

  5. C函数的实现(strcpy,atoi,atof,itoa,reverse)

    在笔试面试中经常会遇到让你实现C语言中的一些函数比如strcpy,atoi等 1. atoi 把字符串s转换成数字 int Atoi( char *s ) { int num = 0, i = 0; ...

  6. atoi函数和atof函数

    1.函数名:atoi 功能:是把字符串转换成整型数的一个函数,应用在计算机程序和办公软件中 名字来源:alphanumeric to integer 用法:int atoi(const char *n ...

  7. cannot locate symbol "atof" referenced by错误分析

    ndk从r8升级到r10后, 使用eclipse编译出来的so库报错了,加载库的时候报错cannot locate symbol "atof" referenced by 原因:A ...

  8. 模拟实现C库的atoi、atof和itoa

    1.C函数atoi atoi (表示 alphanumeric to integer)是把字符串转换成整型数的一个函数.广泛的应用在计算机程序和办公软件中.atoi( ) 函数会扫描参数 nptr字符 ...

  9. 字符串常用-----atof()函数,atoi()函数

    头文件:#include <stdlib.h>函数 atof() 用于将字符串转换为双精度浮点数(double),其原型为:double atof (const char* str);at ...

随机推荐

  1. Java 程序员必须掌握的 Linux 命令

    作为一个Java开发人员,有些常用的Linux命令必须掌握.即时平时开发过程中不使用Linux(Unix)或者mac系统,也需要熟练掌握Linux命令.因为很多服务器上都是Linux系统.所以,要和服 ...

  2. [置顶] 【Git入门之八】分支管理

    原创作品,转载请标明:http://blog.csdn.net/jackystudio/article/details/12309385 1.分支又是神马? 我为什么说又是... 分支就是一个我们能通 ...

  3. RHEL6配置IP

    修改配置文件:/etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0BOOTPROTO=noneBROADCAST=192.168.10.255HW ...

  4. powershell利用winform批量执行tsql语句

    #加载.net的winform模块 [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") $app= ...

  5. C primer plus 读书笔记第五章

    本章的标题是运算符,表达式和语句.主要研究如何处理数据. 示例代码展示了一个使用简单的while循环的代码,难度不大. 下面简单介绍本章的基本概念. 1.基本运算符. 基本运算符有:赋值运算符(C语言 ...

  6. 【C++深入探索】Copy-and-swap idiom详解和实现安全自我赋值

    分类: C/C++2012-08-30 21:40 2017人阅读 评论(2) 收藏 举报 任何管理某资源的类比如智能指针需要遵循一个规则(The Rule of Three): 如果你需要显式地声明 ...

  7. javascript內容向上不間斷滾動

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. js判断访问者是否来自移动端代码

    <script type="text/javascript"> function is_mobile() { var regex_match = /(nokia|iph ...

  9. js进制转换

    var n = 17; var n2 = n.toString(2); var n8 = "0" + n.toString(8); var n16 = "0x" ...

  10. Shell中的${},##和%%的使用

    假设我们定义了一个变量为: file=/dir1/dir2/dir3/my.file.txt 可以用${ }分别替换得到不同的值: ${file#*/}:删掉第一个/ 及其左边的字符串:dir1/di ...