多的不说直接淦代码:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
int My_atoi(const char* str) {
assert(str);//判断指针
int flag = 1;//符号位默认为正数
int ret = 0;//返回值初始化
char* p = (char*)str;
while (*p != '\0') {//不到'\0'一直循环
while (*p == ' ') {//空格跳过
p++;
}
if (*p == '-') {//判断符号
p++;
flag = -1;
}
while (*p >= '0' && *p <= '9') {//判断是否为0-9的数字
int n = *p - '0';//把字符转换为数字
ret = ret * 10 + n;//每次*10 比如"123"->先判断1,0*10+1=1;然后是2->1*10+2=12;然后是3->12*10+3=123
p++;
}
}
return flag * ret;//返回符号乘以总值
}
int main() {
char* str = "12345";
printf("%d\n", My_atoi(str));
return 0;
}