您的位置:首页 > 其它

atoi 自己实现 leecode

2014-07-24 00:47 411 查看
//测试数据 0k4 2147483648 -1 1 " 10522545459"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <limits.h>
class Solution {
public:
int atoi(const char *str) {
long long total = 0;
while (isspace(*str))
str++;
int c = (int)(unsigned char)*str++;
int sign = 1;
if (c == '-' || c == '+'){
sign = c;
c = (int)(unsigned char)*str++;
}
while (c == '0'){
c = (int)(unsigned char)*str++;
}
if (!isdigit(c)) return total;
while (isdigit(c)){
total = total*10 + (c - '0');
c = (int)(unsigned char)*str++;
}
if (sign == '-')
return (-total < INT_MIN) ? INT_MIN : -total;
return (total > INT_MAX) ? INT_MAX : total;
}
};
int main(){

char *a = " 10522545459";
Solution d;
int c = d.atoi(a);
printf("%d",c);
return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: