#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
using namespace std;
long long atoi_core(const char *str, int sign);
int my_atoi(const char *str) {
long long result = 0;
if (str != NULL && *str != '\0') {
while (*str == ' ') str++;
int sign = (*str == '-') ? -1 : 1;
if (*str == '+' || *str == '-') str++;
if (*str != '\0') {
result = atoi_core(str, sign);
}
else {
fprintf(stderr, "str novalid");
}
}
else {
fprintf(stderr, "str is NULL || empty");
}
return (int)result;
}
long long atoi_core(const char *str, int sign) {
long long result = 0;
while (*str != '\0') {
if (*str >= '0' && *str <= '9') {
result = result * 10 + (*str - '0');
if ((sign == 1 && result > 0x7fffffff) || (sign == -1 && result > 0x80000000)) {
fprintf(stderr, "str atoi is larger int");
return 0;
}
str++;
}
else {
fprintf(stderr, "str exist illegal character");
return 0;
}
}
return result * sign;
}
int main() {
return 0;
}
atoi 的实现
最新推荐文章于 2019-01-12 16:42:06 发布