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

来源:互联网 发布:输入汉语拼音软件 编辑:程序博客网 时间:2024/05/05 20:55

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

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

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

/* -------------------------------------------------------------------------//文件名:StringToFloat.h//创建者:magictong//创建时间:2011-9-6 14:14:25//功能描述:////$Id: $// -----------------------------------------------------------------------*/#ifndef __STRINGTOFLOAT_H__#define __STRINGTOFLOAT_H__// -------------------------------------------------------------------------float StrToFloatW(wchar_t* pstrfloat);float StrToFloatA(char* pstrfloat);#if defined(UNICODE) || defined(_UNICODE)#define StrToFloat  StrToFloatW#else#define StrToFloat  StrToFloatA#endif // !UNICODE// -------------------------------------------------------------------------// $Log: $#endif /* __STRINGTOFLOAT_H__ */


/* -------------------------------------------------------------------------//文件名:StringToFloat.cpp//创建者:magictong//创建时间:2011-9-6 14:14:02//功能描述:////$Id: $// -----------------------------------------------------------------------*/#include "stdafx.h"#include "StringToFloat.h"// -------------------------------------------------------------------------#pragma warning(disable:4244)// -------------------------------------------------------------------------// 函数: StrToFloatA// 功能: 将一个字符串转换为浮点数// 返回值: float // 参数: char* pstrfloat// 附注: // -------------------------------------------------------------------------float StrToFloatA(char* pstrfloat){// checkif (!pstrfloat){return 0.0;}bool bNegative = false;bool bDec = false;char* pSor = 0;char chByte = '0';float fInteger = 0.0;float fDecimal = 0.0;float fDecPower = 0.1f;// 进行首位判断,判断是否是负数if (pstrfloat[0] == '-'){bNegative = true;pSor = pstrfloat + 1;}else{bNegative = false;pSor = pstrfloat;}while (*pSor != '\0'){chByte = *pSor;if (bDec){// 小数if (chByte >= '0' && chByte <= '9'){fDecimal += (chByte - '0') * fDecPower;fDecPower = fDecPower * 0.1;}else{return (bNegative? -(fInteger +  fDecimal): fInteger + fDecimal);}}else{// 整数if (chByte >= '0' && chByte <= '9'){fInteger = fInteger * 10.0 + chByte - '0';}else if (chByte == '.'){bDec = true;}else{return (bNegative? -fInteger : fInteger);}}pSor++;}return (bNegative? -(fInteger +  fDecimal): fInteger + fDecimal);}// -------------------------------------------------------------------------// 函数: StrToFloatW// 功能: 将一个字符串转换为浮点数// 返回值: float // 参数: char* pstrfloat// 附注: // -------------------------------------------------------------------------float StrToFloatW(wchar_t* pstrfloat){// checkif (!pstrfloat){return 0.0;}bool bNegative = false;bool bDec = false;wchar_t* pSor = 0;wchar_t chByte = L'0';float fInteger = 0.0;float fDecimal = 0.0;float fDecPower = 0.1f;// 进行首位判断,判断是否是负数if (pstrfloat[0] == L'-'){bNegative = true;pSor = pstrfloat + 1;}else{bNegative = false;pSor = pstrfloat;}while (*pSor != L'\0'){chByte = *pSor;if (bDec){// 小数if (chByte >= L'0' && chByte <= L'9'){fDecimal += (chByte - L'0') * fDecPower;fDecPower = fDecPower * 0.1;}else{return (bNegative? -(fInteger +  fDecimal): fInteger + fDecimal);}}else{// 整数if (chByte >= L'0' && chByte <= L'9'){fInteger = fInteger * 10.0 + chByte - L'0';}else if (chByte == L'.'){bDec = true;}else{return (bNegative? -fInteger : fInteger);}}pSor++;}return (bNegative? -(fInteger +  fDecimal): fInteger + fDecimal);}// -------------------------------------------------------------------------// $Log: $

        测试用例:

// StringToFloatShell.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "StringToFloat.h"int _tmain(int argc, _TCHAR* argv[]){float aaaa = 0;aaaa = StrToFloat(L"12.34");aaaa = StrToFloat(L"a23");aaaa = StrToFloat(L"1234");aaaa = StrToFloat(L"12.34");aaaa = StrToFloat(L"12.34.56");aaaa = StrToFloat(L".34");aaaa = StrToFloat(L"34a");aaaa = StrToFloat(L"34a.456");aaaa = StrToFloat(L"-34");aaaa = StrToFloat(L"-56.34");aaaa = StrToFloat(L"-3.45.67");aaaa = StrToFloat(L"-.45.6a");aaaa = StrToFloat(L"-.");aaaa = StrToFloat(L"-0");return 0;}


        大家可以再找找BUG。

        [END]


原创粉丝点击