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
    评论
下面是一个较为详细的版本的atoi函数源码示例: ```c #include <stdio.h> #include <stdlib.h> #include <ctype.h> int atoi(const char *str) { int num = 0; int sign = 1; int i = 0; // 处理空白字符 while (isspace(str[i])) i++; // 处理正负号 if (str[i] == '-' || str[i] == '+') { sign = (str[i] == '-') ? -1 : 1; i++; } // 转换数字 while (isdigit(str[i])) { if (num > INT_MAX / 10 || (num == INT_MAX / 10 && (str[i] - '0') > INT_MAX % 10)) { // 处理溢出情况 if (sign == 1) return INT_MAX; else return INT_MIN; } num = num * 10 + (str[i] - '0'); i++; } return num * sign; } int main() { char str[] = "12345"; // 要转换的字符串 int num = atoi(str); // 使用自定义的atoi函数将字符串转换为整数 printf("转换后的整数为: %d\n", num); return 0; } ``` 这个版本的atoi函数与之前的简化版相比,增加了以下功能: - 使用`isspace`函数处理空白字符,而不仅仅是处理空格和制表符。 - 增加了溢出检查,以避免将超出整数范围的字符串转换为整数。如果溢出,则根据符号位返回INT_MAX或INT_MIN。 在上述示例中,我们首先包含了`stdlib.h`、`stdio.h`和`ctype.h`头文件。然后,我们定义了一个自定义的atoi函数,它接受一个指向const char类型的字符串,并返回相应的整数。 最后,我们在main函数中使用自定义的atoi函数将字符串转换为整数,并打印出转换后的结果。 请注意,这只是一个较为详细的版本的atoi函数示例。实际的atoi函数可能会更复杂,考虑更多的边界条件和错误处理情况。此外,该实现还未考虑负号后面跟随非数字字符的情况,如果你需要更严格的实现,请根据具体需求进行修改和扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值