atoi实现

来源:互联网 发布:重庆少儿编程 编辑:程序博客网 时间:2024/06/06 20:24

对于关键字_Bool

我们都知道在c中c89是没有bool类型的,c99引用了一个_Bool关键字代表布尔类型。其中当给该关键字赋值非0时,该关键字值为1,给该关键字赋值为0时,该关键字值为0。这也符合c语言之前对于逻辑判断的特性,对于0为假,非0值代表真。

bool宏

在c99后引用了_Bool关键字后,在stdbool.h头文件中定义了三个宏代表bool类型,第一个宏bool,第二个宏True,第三个宏false为了向c++兼容。

atoi代码

#include<stdio.h>#include <stdlib.h>enum status{    Valid = 0,    InValid,    ERange,};int my_errno = Valid;int my_atoi(const char* p){    if (p == NULL)    {        my_errno = InValid;        return 0;    }    while (isspace(*p)) ++p;//处理空格    _Bool minus = 0;    unsigned nums = 0;    if (*p == '+'||*p=='-'||*p>='0'||*p<='9')    {        if (*p == '+')            ++p;        else if (*p == '-')        {            ++p;            minus = 1;        }        else {            while ('0' <= *p && *p <= '9')            {                nums = nums * 10 + (*p - '0');                ++p;                if (minus == 0 && nums>0x7fffffff || minus == 1 && nums<0x80000000)                    goto Rang;            }        }        nums = minus ? nums : -1 * nums;    }    else{        goto Inval;    }    return nums;Inval:    my_errno = InValid;    return 0;Rang:    my_errno = ERange;    return 0;}
原创粉丝点击