现在的位置: 首页 > 综合 > 正文

浮点数字符串转换成浮点数实现

2018年04月20日 ⁄ 综合 ⁄ 共 5880字 ⁄ 字号 评论关闭

        之前面试的时候,常给面试者出的一个面试题目是,给定一个字符串,输出该字符串表示的浮点数的值,要求如下:

        写一个转换函数,该函数的输入是一个表示浮点数的字符串,把该字符串转换成浮点数并输出。条件:请考虑各种情况,并且代码中的循环尽量少,不能调用API或者crt库中的函数。例如:输入字符串"345.7",则输出浮点数345.7。接口可以为:float StrToFloatA(TCHAR* pstrfloat);

        没想到完全做对这个题目的人居然不多(上机笔试,函数写好之后丢到测试机里面跑一下看是否完全正确),因此自己在空闲时间实现了一下,确实还是有一点难度的。代码如下:

  1. /* ------------------------------------------------------------------------- 
  2. //  文件名     :   StringToFloat.h 
  3. //  创建者     :   magictong 
  4. //  创建时间    :   2011-9-6 14:14:25 
  5. //  功能描述    :    
  6. // 
  7. //  $Id: $ 
  8. // -----------------------------------------------------------------------*/  
  9. #ifndef __STRINGTOFLOAT_H__  
  10. #define __STRINGTOFLOAT_H__  
  11.   
  12. // -------------------------------------------------------------------------  
  13. float StrToFloatW(wchar_t* pstrfloat);  
  14. float StrToFloatA(char* pstrfloat);  
  15.   
  16. #if defined(UNICODE) || defined(_UNICODE)  
  17.     #define StrToFloat  StrToFloatW  
  18. #else  
  19.     #define StrToFloat  StrToFloatA  
  20. #endif // !UNICODE  
  21. // -------------------------------------------------------------------------  
  22. // $Log: $  
  23.   
  24. #endif /* __STRINGTOFLOAT_H__ */  


  1. /* ------------------------------------------------------------------------- 
  2. //  文件名     :   StringToFloat.cpp 
  3. //  创建者     :   magictong 
  4. //  创建时间    :   2011-9-6 14:14:02 
  5. //  功能描述    :    
  6. // 
  7. //  $Id: $ 
  8. // -----------------------------------------------------------------------*/  
  9.   
  10. #include "stdafx.h"  
  11. #include "StringToFloat.h"  
  12.   
  13. // -------------------------------------------------------------------------  
  14.   
  15. #pragma warning(disable:4244)  
  16. // -------------------------------------------------------------------------  
  17. // 函数       : StrToFloatA  
  18. // 功能       : 将一个字符串转换为浮点数  
  19. // 返回值  : float   
  20. // 参数       : char* pstrfloat  
  21. // 附注       :   
  22. // -------------------------------------------------------------------------  
  23. float StrToFloatA(char* pstrfloat)  
  24. {  
  25.     // check  
  26.     if (!pstrfloat)  
  27.     {  
  28.         return 0.0;  
  29.     }  
  30.   
  31.     bool bNegative = false;  
  32.     bool bDec = false;  
  33.   
  34.     char* pSor = 0;  
  35.     char chByte = '0';  
  36.     float fInteger = 0.0;  
  37.     float fDecimal = 0.0;  
  38.     float fDecPower = 0.1f;  
  39.   
  40.     // 进行首位判断,判断是否是负数  
  41.     if (pstrfloat[0] == '-')  
  42.     {  
  43.         bNegative = true;  
  44.         pSor = pstrfloat + 1;  
  45.     }  
  46.     else  
  47.     {  
  48.         bNegative = false;  
  49.         pSor = pstrfloat;  
  50.     }  
  51.   
  52.     while (*pSor != '\0')  
  53.     {  
  54.         chByte = *pSor;  
  55.   
  56.         if (bDec)  
  57.         {  
  58.             // 小数  
  59.             if (chByte >= '0' && chByte <= '9')  
  60.             {  
  61.                 fDecimal += (chByte - '0') * fDecPower;  
  62.                 fDecPower = fDecPower * 0.1;  
  63.             }  
  64.             else  
  65.             {  
  66.                 return (bNegative? -(fInteger +  fDecimal): fInteger + fDecimal);  
  67.             }  
  68.         }  
  69.         else  
  70.         {  
  71.             // 整数  
  72.             if (chByte >= '0' && chByte <= '9')  
  73.             {  
  74.                 fInteger = fInteger * 10.0 + chByte - '0';  
  75.             }  
  76.             else if (chByte == '.')  
  77.             {  
  78.                 bDec = true;  
  79.             }  
  80.             else  
  81.             {  
  82.                 return (bNegative? -fInteger : fInteger);  
  83.             }  
  84.         }  
  85.   
  86.         pSor++;  
  87.     }  
  88.   
  89.     return (bNegative? -(fInteger +  fDecimal): fInteger + fDecimal);  
  90. }  
  91.   
  92. // -------------------------------------------------------------------------  
  93. // 函数       : StrToFloatW  
  94. // 功能       : 将一个字符串转换为浮点数  
  95. // 返回值  : float   
  96. // 参数       : char* pstrfloat  
  97. // 附注       :   
  98. // -------------------------------------------------------------------------  
  99. float StrToFloatW(wchar_t* pstrfloat)  
  100. {  
  101.     // check  
  102.     if (!pstrfloat)  
  103.     {  
  104.         return 0.0;  
  105.     }  
  106.   
  107.     bool bNegative = false;  
  108.     bool bDec = false;  
  109.   
  110.     wchar_t* pSor = 0;  
  111.     wchar_t chByte = L'0';  
  112.     float fInteger = 0.0;  
  113.     float fDecimal = 0.0;  
  114.     float fDecPower = 0.1f;  
  115.   
  116.     // 进行首位判断,判断是否是负数  
  117.     if (pstrfloat[0] == L'-')  
  118.     {  
  119.         bNegative = true;  
  120.         pSor = pstrfloat + 1;  
  121.     }  
  122.     else  
  123.     {  
  124.         bNegative = false;  
  125.         pSor = pstrfloat;  
  126.     }  
  127.   
  128.     while (*pSor != L'\0')  
  129.     {  
  130.         chByte = *pSor;  
  131.   
  132.         if (bDec)  
  133.         {  
  134.             // 小数  
  135.             if (chByte >= L'0' && chByte <= L'9')  
  136.             {  
  137.                 fDecimal += (chByte - L'0') * fDecPower;  
  138.                 fDecPower = fDecPower * 0.1;  
  139.             }  
  140.             else  
  141.             {  
  142.                 return (bNegative? -(fInteger +  fDecimal): fInteger + fDecimal);  
  143.             }  
  144.         }  
  145.         else  
  146.         {  
  147.             // 整数  
  148.             if (chByte >= L'0' && chByte <= L'9')  
  149.             {  
  150.                 fInteger = fInteger * 10.0 + chByte - L'0';  
  151.             }  
  152.             else if (chByte == L'.')  
  153.             {  
  154.                 bDec = true;  
  155.             }  
  156.             else  
  157.             {  
  158.                 return (bNegative? -fInteger : fInteger);  
  159.             }  
  160.         }  
  161.   
  162.         pSor++;  
  163.     }  
  164.   
  165.     return (bNegative? -(fInteger +  fDecimal): fInteger + fDecimal);  
  166. }  
  167. // -------------------------------------------------------------------------  
  168. // $Log: $  


        测试用例:

  1. // StringToFloatShell.cpp : Defines the entry point for the console application.  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include "StringToFloat.h"  
  6.   
  7.   
  8. int _tmain(int argc, _TCHAR* argv[])  
  9. {     
  10.     float aaaa = 0;  
  11.     aaaa = StrToFloat(L"12.34");  
  12.     aaaa = StrToFloat(L"a23");  
  13.     aaaa = StrToFloat(L"1234");  
  14.     aaaa = StrToFloat(L"12.34");  
  15.     aaaa = StrToFloat(L"12.34.56");  
  16.     aaaa = StrToFloat(L".34");  
  17.     aaaa = StrToFloat(L"34a");  
  18.     aaaa = StrToFloat(L"34a.456");  
  19.     aaaa = StrToFloat(L"-34");  
  20.     aaaa = StrToFloat(L"-56.34");  
  21.     aaaa = StrToFloat(L"-3.45.67");  
  22.     aaaa = StrToFloat(L"-.45.6a");  
  23.     aaaa = StrToFloat(L"-.");  
  24.     aaaa = StrToFloat(L"-0");  
  25.     return 0;  
  26. }  

        大家可以再找找BUG。

        原文地址:http://blog.csdn.net/magictong/article/details/6900410

抱歉!评论已关闭.