分享

aoti函数的实现

 海漩涡 2015-03-16

// char*itoa(intvalue,char*string,intradix);   linux库中无此函数
// int value 被转换的整数,char *string 转换后储存的字符数组,
// int radix 转换进制数,如2,8,10,16 进制等


//atoi.cpp
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int my1atoi(const char *str)
{
    int value = 0;
    int symble = 1;

    if( NULL == str)
    {
        printf("NULL == str\n");
        return -1;
    }

    while(0 != isspace(*str))
    {
        str++;
    }

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

    while('0'<=*str && *str<='9')
    {
        value = value*10 + (*str - '0');
        str++;
    }
    
    return value*symble;
}

int main(int argc, char * argv [ ])
{
    int n=0,m;
    //n = myatoi("-A");
    const char a[]="  -11-00";
    const char b[]="-200";

    char c[20]={0};
    char d[20]={0};
    
    n = atoi(a);
    m = atoi(b);
    printf("%d %d\n",n,m);

    n = my1atoi(a);
    m = my1atoi(b);
    printf("%d %d\n",n,m);
   
    return 0;
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多