/*
实现atoi函数
*/
#include <stdio.h>
#define MAX (2147483647)//越界最大值
#define MIN (-2147483647)//越界最小值
int myatoi(const char *str)
{
int get_num = 0;
int base = 1;
if(str == NULL)
return 0;
if( ((str[0] <= '9') && (str[0] >= '0')) || \
(str[0] == '+') || \
(str[0] == '-') \
)
{
if((str[0] == '-') || (str[0] == '+'))
{
if((str[0] == '-'))
{
base = -1;
}
str++;
}
}
while((*str <= '9') && (*str >= '0'))
{
if(get_num >= MAX/10)
{
return base == -1 ? MIN : MAX;
}
get_num = get_num * 10 + (*str - '0');
++str;
}
return base * get_num;
}
int main(int argc, char **argv)
{
int n = myatoi(argv[1]);
printf("%d\n", n);
return 0;
}