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; }
相关文章推荐
- 自己实现atoi和atof
- atoi 函数自己写代码实现
- 自己实现atoi系列
- 自己实现atoi函数功能,另加一个str2num有用的小程序
- 自己编写代码实现atoi和itoa函数的功能
- C++ Studio (二) ----- atoi()函数的实现 (自己编写功能)
- 自己实现atoi系列
- 自己实现的atof()和atoi()代码
- 自己实现atoi
- 自己实现atoi系列(atoi函数)
- 自己实现的atof()和atoi()代码 (转)
- 自己实现atoi
- 自己编写代码实现atoi和itoa函数的功能
- 实现自己的ASP.NET宿主系统
- Windows 2000下如何在自己的程序中实现关机!
- 实现自己的O/R Mapping组件[二]-问题关注:昂贵的字段
- 在WebWork中实现自己的Result Type
- 在Vovida的基础上实现自己的SIP协议栈(一)
- 实现自己的ASP.NET宿主系统
- 实现自己的配置读取器