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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-26 22:31:09

C++atoi与atof的相关文章

_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明

原文:http://www.cnblogs.com/lidabo/archive/2012/07/10/2584706.html _itoa 功能:把一整数转换为字符串 用法:char * _itoa(int value, char *string, int radix); 详细解释: _itoa是英文integer to array(将int整型数转化为一个字符串,并将值保存在数组string中)的缩写.其中value为要转化的整数, radix是基数的意思,即先将value转化为radix进

模拟实现库函数的atoi、atof和itoa

1.函数atoi atoi (表示 alphanumeric to integer)是把字符串转换成整型数的一个函数.广泛的应用在计算机程序和办公软件中.atoi( ) 函数会扫描参数 nptr字符串,跳过前面的空白字符(例如空格,tab缩进等). 原型:int atoi(const char *nptr),nptr:要进行转换的字符串: 功能:把字符串转换成整型数: 返回值:函数返回一个 int 值,此值由将输入字符作为数字解析而生成. 如果该输入无法转换为该类型的值,则atoi的返回值为 0

字符数字转换 atoi 与 strtol

原文:http://www.cnblogs.com/JefferyZhou/archive/2010/07/01/1769555.html 在很多时候我们都很清楚 atoX 系列函数: atoi , atol , atof新来的一系列函数:  strtol,  strtoul, strtod 通常有如下的关系:1. 对应关系其中: atoi   (把字符串转到整形)    --对应--   strtol  (把字符串转到长整形) atol   (把字符串转到长整形)    --对应--   st

数字转字符串及字符串转数字

头文件:#include<cstdio> 1.字符串数字之间的转换 (1)string --> char *   string str("OK");   char * p = str.c_str(); (2)char * -->string   char *p = "OK";   string str(p); (3)char * -->CString    char *p ="OK";   CString m_Str

C++中数字与字符串之间的转换

1.字符串数字之间的转换 (1)string --> char *   string str("OK");   char * p = str.c_str(); (2)char * -->string   char *p = "OK";   string str(p); (3)char * -->CString    char *p ="OK";   CString m_Str(p);   //或者   CString m_Str

CString和string

CString和string(一) 概述 string和CString均是字符串模板类,string为标准模板类(STL)定义的字符串类,已经纳入C++标准之中: CString(typedef CStringT > CString)为Visual C++中最常用的字符串类,继承自CSimpleStringT类,主要应用在MFC和ATL编程中,主要数据类型有char(应用于ANSI),wchar_t(unicode),TCHAR(ANSI与unicode均可): char*为C编程中最常用的字符

C++中数字和字符串的转换

1.字符串数字之间的转换 (1)string --> char *   string str("OK");   char * p = str.c_str(); (2)char * -->string   char *p = "OK";   string str(p); (3)char * -->CString    char *p ="OK";   CString m_Str(p);   //或者   CString m_Str

PAT A+B格式

A + B格式(20) 时间限制 400毫秒 内存限制 65536 kB 代码长度限制 16000 B. 判断程序 标准 作者 陈,岳 计算a + b并以标准格式输出总和 - 即数字必须用逗号分隔成三个组(除非有少于四个数字). 输入 每个输入文件包含一个测试用例.每个案例包含一对整数a和b,其中-1000000 <= a,b <= 1000000.数字由空格分隔. 输出 对于每个测试用例,您应该在一行中输出a和b的和.总和必须以标准格式写入. 样品输入 -1000000 9 示例输出 -99

boost库学习之 lexical_cast

在C中字符串转换为数值,可以使用atoi().atof().atol()等,数值转换为字符串可以使用itoa().sprintf()等,但itoa与编译器有关,并不是标准函数,而sprintf可能会不安全. 使用lexical_cast可以很容易地在数值与字符串之间转换,只需在模板参数中指定转换的目标类型即可.如 int x = lexical_cast<int>("100"); long y = lexical_cast<long>("10000&q