atoi atof源码

 

atoi atof源码

默认分类    2009-07-29 18:53   阅读16   评论0  
字号:    

// Test2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <cassert>
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <vld.h>

using namespace std;

double Strtod(char *str, char **endstr)
{
    double num1 = 0.0;
    double num2 = 0.0;
    double point = 0.1;
    int sign = 1;

    int len = strlen(str) + 1;
    *endstr = new char[len];
    memset(*endstr, 0, len);

    if (*str == '-')
    {
        sign = -1;
        ++str;
    }

    if (!isdigit(*str))
    {
        strcpy(*endstr, str);
        return 0.0;
    }
    while (*str && isdigit(*str))
    {
        if (*str == '.')
        {
            ++str;
            while (*str && isdigit(*str))
            {
                num2 += point * ((*str) - '0');
                point *= 0.1;
                ++str;
            }
            strcpy(*endstr, str);
            break;
        }
        else
        {
            num1 = 10 * num1 + *str - '0';
            str++;
            if (!*str || !isdigit(*str))
            {
                strcpy(*endstr, str);
                break;
            }
        }
    }
   
    return (num1 + num2) * sign;
}

int Atoi(const char *pstr)
{
    int sign = 1;
    int num = 0;

    while (*pstr == ' ' || *pstr == '/t')
    {
        pstr++;
    }

    if (*pstr == '-')
    {
        sign = -1;
        pstr++;
    }

    while (*pstr)
    {
        if (*pstr >= '0' && *pstr <= '9')
        {
            num = 10 * num + *pstr - '0';
        }
        else
        {
            return num * sign;
        }
        pstr++;
    }
    return (num * sign);
}

double Atof(const char *pstr)
{
    double sign = 1.0;
    double num1 = 0.0;
    double num2 = 0.0;
    double point = 0.1;

    while (*pstr == ' ' || *pstr == '/t')
    {
        pstr++;
    }

    if (*pstr == '-')
    {
        sign = -1;
        pstr++;
    }

    while (*pstr)
    {
        if (*pstr == '.')
        {
            pstr++;
            while (*pstr >= '0' && *pstr <= '9')
            {
                num1 += point * (*pstr - '0');
                point *= 0.1;
                pstr++;
            }
        }
        else if (*pstr >= '0' && *pstr <= '9')
        {
            num2 = num2 * 10 + *pstr -'0';
        }
        else
        {
            return (num1 + num2) * (sign);
        }
        pstr++;
    }
    return (num1 + num2) * (sign);
}

int main( void )
{
    char str[] = "    -1234565kljh";
    int num = Atoi(str);
    cout<< num << endl;

    char pstr[] = "    112adfaf3  43224.569877aa";
    double n = Atof(pstr);
    printf("%6.8f/n", n);

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值