atoi 自己实现 leecode

来源:互联网 发布:搬瓦工 centos 7 和 6 编辑:程序博客网 时间:2024/05/20 08:23
//测试数据 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;}

0 0
原创粉丝点击