1.目的
编码实现函数atoi(),把一个字符串转化为一个整型数值
例如(“12345”转化成12345)
2.避坑
避免一下几种坑没有考虑到
1.字符串有没有正负号问题;
2.字符串里夹杂着各种字母或是异常字符的情况;
3.考虑字符串转化成int 整型时会不会越界.
3.两个解法
代码1
#include <stdio.h>
#include <Windows.h>
#include <assert.h>
#include <stdlib.h>
int status = 0;//全局变量,用来判断错误的情况,没有问题status=0;越界status=1;异常字符status=2.
int my_atoi(const char *str){
assert(str);
int flag = 1;//记录正负号
long long result = 0;
int i = 0;
while (isspace(str[i])){
//isspace()函数,时判断是不是空格
i++;
}
if (str[i] ==