浮点数字符串转换成浮点数实现(2)

本文介绍了如何实现一个浮点数字符串到浮点数的转换,包括处理前导和尾随空格,以及支持E/e形式的科学计数法。虽然运行时库已有类似函数,但此实现旨在练习和增强理解。
摘要由CSDN通过智能技术生成

        其实几年前实现过一个面试题版本的,参考:浮点数字符串转换成浮点数实现

        最近因为某些原因又拿了出来,做了一些简单的修改,支持了前端空格处理,溢出检测等等,当然精度处理这个难度有点大,没有特殊处理。另外带E(e)的浮点数字符串也进行了处理。需要特别说明的时候,运行时库里面是有一个类似的转换函数的,写一写只是练练手而已。

        头文件

/* -------------------------------------------------------------------------
//  文件名      :  KString2Float.h
//  创建者      :  magic
//  创建时间    :  2015/7/28 17:36:43
//  功能描述    :  
//
//  $Id: $
// -----------------------------------------------------------------------*/
#ifndef __KSTRING2FLOAT_H__
#define __KSTRING2FLOAT_H__

#include <float.h>
#include <windows.h>
// -------------------------------------------------------------------------
namespace NSUtil
{

// -------------------------------------------------------------------------
// 函数     : atofloat
// 功能     : 将一个字符串转换为浮点数
// 返回值   : float 
// 参数     : const char* s
// 附注     : 
// -------------------------------------------------------------------------
float atofloat(const char* s);

// -------------------------------------------------------------------------
// 函数     : wtofloat
// 功能     : 将一个宽字符串转换为浮点数
// 返回值   : float 
// 参数     : const wchar_t* s
// 附注     : 
// -------------------------------------------------------------------------
float wtofloat(const wchar_t* s);

// -------------------------------------------------------------------------
// 函数     : strtofloatT
// 功能     : 将一个字符串转换为浮点数
// 返回值   : float 
// 参数     : const tchar* s
// 附注     : 
// -------------------------------------------------------------------------
template<typename tchar>
float strtofloatT(const tchar* s)
{
    // check
    if (!s)
        return 0.0f;

    float fResult = 0.0f; // 存储结果
    tchar c = 0;

    // 整数部分
    bool bNegative = false;
    float fInteger = 0.0f;

    // 小数部分
    bool bDec = false;
    float fDecimal = 0.0f;
    float fDecPower = 0.1f;

    // 指数部分
    bool bMeetE = false;
    bool bEInFirst = true; // e不能在最前面
    bool bNegativeExponent = false;
    unsigned int nIntegerExponent = 0;
    float fExponentData = 1.0f;

    c = *s++;
    /* skip whitespace */
    while (0x20 == c)
        c = *s++;

    // 进行首位判断,判断是否是负数
    if (0x2d == c)
    {
        bNegative = true;
        c = *s++;
    }
    else if (0x2b == c)
    {
        bNegative = false;
        c = *s++;
    }

    while (0 != c)
    {
        if (bMeetE)
        {
            // 指数部分
            if (c >= 0x30 && c <= 0x39)
            {
                nIntegerExponent = nIntegerExponent * 10 + c - 0x30;
            }
            else
            {
                break;
            }
        }
        else
        {
            if (0x65 == c || 0x45 == c)
            {
                if (bEInFirst)
                    break;

                // 确定指数的符号
                c = *s;
                if (0 == c)
                    break;
                if (0x2d == c)
                {
                    bNegativeExponent = true;
                    c = *s++;
                }
                else if (0x2b == c)
                {
                    bNegativeExponent = false;
                    c = *s++;
                }

                bMeetE = true;
            }
            else if (bDec)
            {
                bEInFirst = false;
                // 小数部分
                if (c >= 0x30 && c <
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值