C++atoi与atof

#include <iostream>
using namespace std;
static int sflags = 0;
//atof的函数实现。
bool Isnum(char ch)
{
    return (ch - '0') >= 0 || (ch - '0') <= 9;
}
float Getnum(char *s,int flags)
{
    float count = 0;
    int k=0;
    while (*s != '\0')
    {
        if (Isnum(*s) && *s!='.')
        count = count * 10 + *s - '0';
        if ((Isnum(*s) == 0 && *s != '.') || (Isnum(*s) == 0 && *s == '.' && k == 1))
        {
            return 0;
        }   
        if (*s == '.')
        {
            k++;
        }
        if (k > 0)
            k++;
        if (k > 7)
            return count / 1000000;//多算一位。
        s++;
    }
    for (int i = 0; i < k-2; i++)
    {
        count /= 10;
    }
    return count;
}

float my_atof(char *s)
{
    float count = 0;
    int flags = 0;//记录小数点位数。
    if (s == NULL)
    {
        sflags = 1;
        return 0;
    }
    while (*s == ' ')s++;
    if (*s == '\0')return 0;
    if (*s == '+')
    {
        count = Getnum(s + 1,flags);
        return count;
    }
    else if (*s == '-')
    {
        count = Getnum(s + 1, flags);
        return 0 - count;
    }
    else
    {
        count = Getnum(s, flags);
        return count;
    }
    return 0;
}

int main()
{
    cout << my_atof("-1.123456789") << endl;
    cout << atof("-1.123456789") << endl;
}


//atoi的实现。
#include <iostream>
using namespace std;
static int flags = 0;

int Isnum(char ch)
{
    return (ch - '0') >= 0 || (ch - '0') <= 9;
}
int Getnum(char *s)
{
    long long count = 0;
    while (*s != '\0')
    {
        if (Isnum(*s))
        {
            count = count * 10 + *s - '0';
            if (count >= 0x7fffffff)return 0x7fffffff;
        }
        if (*s == '.')return count;
        if (Isnum(*s) == 0)
            return  -1;
        s++;
    }
    return count;
}

int my_atoi(char *s)
{
    int count = 0;
    if (s == NULL)
    {
        flags = 1;
        return 0;
    }
    if (*s == '\0')return 0;
    while (*s == ' ')s++;
    if (*s == '+')
    {
        count = Getnum(s + 1);
        if (count == -1)return 0;
        else if (count == 0x7ffffffff)
        {
            return 0x7fffffff;
        }
        else
        {
            return count;
        }
    }
    else if (*s == '-')
    {
        count = Getnum(s + 1);
        if (count == -1)return 0;
        else if (count == 0x7fffffff)
        {
            return 0x7fffffff+1;
        }
        else
        {
            return 0-count;
        }
    }
    else
    {
        count = Getnum(s);
        if (count == -1)return 0;
        return count;
    }
}
int main()
{
    char *s = new char[20];
    cin >> s;
    cout << my_atoi(s) << endl;
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值