Cocos2d-X atoi() 函数的具体实现
来源:互联网 发布:mac air教程视频 编辑:程序博客网 时间:2024/04/28 11:41
- /* 8、请编写能直接实现 int atoi(const char * pstr)函数功能的代码 */
- /************************************************************************/
- //考虑全局变量返回结果是否有效,和大数问题
- bool isToIntValid =true;
- int strToInt(const char *str)
- {
- long long num = 0;
- int mark = (*str== '-' ? -1: 1);
- long long upperBound = numeric_limits<int>::max();;
- if(mark == -1)
- ++upperBound;
- const char* temp = (*str == '+' || *str == '-') ? str + 1: str;
- for( ;*temp >= '0' && *temp <= '9'; ++temp)
- {
- num = num * 10 + *temp - '0';
- if(num > upperBound)
- {
- //越界,atoi中对于越界直接取最值
- isToIntValid = false;
- num = upperBound;
- break;
- }
- }
- if(*temp !='\0' || *str == '0')
- isToIntValid = false;
- return static_cast<int>(mark * num);
- }
- void testOfstrToInt()
- {
- assert(atoi("+1234") == strToInt("+1234"));
- assert(atoi("-1234") == strToInt("-1234"));
- assert(atoi("+aaa234") == strToInt("+aaa234"));
- assert(atoi("aaa1234") == strToInt("aaa1234"));
- assert(atoi("-1234a") == strToInt("-1234a"));
- assert(atoi("1234") == strToInt("1234"));
- assert(atoi("12a34") == strToInt("12a34"));
- assert(atoi("aaaa") == strToInt("aaaa"));
- assert(atoi("0123") == strToInt("0123"));
- //大数
- assert(atoi("123456789012345123456") == strToInt("123456789012345123456"));
- assert(atoi("-123456789012345123456") == strToInt("-123456789012345123456"));
- }
0 0
- Cocos2d-X atoi() 函数的具体实现
- Cocos2d-X 简单画图的具体实现
- 字符串转换成整数(atoi)函数的具体实现
- Cocos2d-X 背景层循环运动效果的具体实现
- atoi函数的实现
- atoi()函数的实现
- atoi()函数的实现
- atoi函数的实现
- atoi函数的实现
- atoi函数的实现
- atoi函数的实现
- atoi函数的实现
- atoi函数的实现
- atoi()函数的实现
- atoi()函数的实现
- atoi()函数的实现
- atoi函数的实现
- atoi()函数的实现
- 第十周项目一——程序填充之式子的计算
- enq: RO - fast object reuse
- 测试控制符号
- Linux crontab 命令格式与详细例子
- MyReport报表引擎2.6.5.0新功能
- Cocos2d-X atoi() 函数的具体实现
- oracle父子结构表递归查询sql语句
- cocos2dx像素碰撞(转国外文章)
- iOS工程结构分享
- CR - block range reuse ckpt
- 第10周项目1项目填充一的拓展(多次输入计算)
- xcode6中如何添加pch文件
- 推荐!手把手教你使用Git
- 构建快捷开发IDE:VisualSVN+Sublime+Visual Studio 2013+QuickEasyFTPServer