strToInt
来源:互联网 发布:java中格式化时间毫秒 编辑:程序博客网 时间:2024/04/27 05:35
测试:
struct TestInput{ const char* str; int expectedRetValue; int expectedErrStatus;} ;TestInput inputs[] ={ {"123", 123, kValid}, {"+123", 123, kValid}, {"-123", -123, kValid}, {" 123", 123, kValid}, {"123abc", 123, kValid}, {"123.", 123, kValid}, {NULL, 0, kInvalid}, {"", 0, kInvalid}, {"+", 0, kInvalid}, {"-", 0, kInvalid}, {".123", 0, kInvalid}, {"abc", 0, kInvalid}};#include <cstdio>#define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))void testStrToInt(){ int failed = 0; unsigned int i = 0; for (i=0; i<ARRAY_SIZE(inputs); ++i) { int ret = 0; if ((inputs[i].expectedRetValue != (ret=strToInt(inputs[i].str))) || (inputs[i].expectedErrStatus != g_error_status)) { ++failed; printf("\ncase %d failed\n" "input %s\n" "expected:ret=%d,err=%d\n" "actual:ret=%d,err=%d\n", i, inputs[i].str, inputs[i].expectedRetValue, inputs[i].expectedErrStatus, ret, g_error_status); } } printf("\ntest done! total %d cases, %d ok, %d failed\n", i, i-failed, failed);}
实现:
#define NULL 0typedef enum{ kValid, kInvalid} ErrorStatus;ErrorStatus g_error_status = kInvalid;static bool isDigit(char c){ return !((c-'0') < 0 || (c-'0') >9);}static bool isWhiteSpace(char c){ return ((c == '\t') || (c == ' '));}static bool overflowForIntType(bool minus, long long num){ return ( (minus && num < (signed int)0x80000000) || (!minus && num > 0x7fffffffL));}static const char* ignoreWhitespace(const char* str){ while(*str && isWhiteSpace(*str)) { str++; } return str;}/*should pass below test cases:|----------------------------|input|return|g_error_status|----------------------------|123|123|kValid|----------------------------|+123|123|kValid|----------------------------|-123|-123|kValid|----------------------------|[blank]123|123|kValid|----------------------------|[tab]123|123|kValid|----------------------------|123abc|123|kValid|----------------------------|123.|123|kValid|----------------------------|[null]|0|kInvalid|----------------------------|""|0|kInvalid|----------------------------|+|0|kInvalid|----------------------------|-|0|kInvalid|----------------------------|.123|0|kInvalid|----------------------------|abc|0|kInvalid|----------------------------*/int strToInt(const char* str){ g_error_status = kInvalid; if ( (NULL == str) || (*str == '\0')) { return 0; } const char* cur = ignoreWhitespace(str); bool minus = false; if (*cur == '-') { minus = true; ++cur; } else if (*cur == '+') { ++cur; } long long result = 0; bool isNum = false; while((*cur != '\0')) { if (!isDigit(*cur)) { break; } else { isNum = true; result = result*10 + (*cur - '0') ; } cur++; } if (!isNum) { return 0; } if (minus) { result = -1*result; } if (overflowForIntType(minus, result)) { return 0; } g_error_status = kValid; return (int)result;}
- strToInt
- strtoint
- StrToInt()
- StrToInt实现
- strToInt 与IntToStr
- 实现 StrToInt函数
- 自己实现StrToInt函数
- StrToInt 和 IntToStr
- StrToInt && StrToHex && IntToString && 编辑框格式
- strToInt和intToStr的自我定义
- C++ Builder中StrToInt的使用
- 每天一道算法题1(StrToInt)
- brew中如何实现strtoint类似的功能
- 靠谱的程序员太少了
- colinux和windows主机共享上网
- Flex 4双向数据绑定
- Motorola vs Nokia
- javascript判断浏览器类型与版本
- strToInt
- uva 568 - Just the Facts
- 指针强制转换问题
- ubuntu下codeblocks 设置qt开发环境
- hdu 2036 (求多边形面积)
- struts2.2的路径匹配(struts2与servlet的耦合)
- 初尝NokiaQtSDK
- 角色脱节与失衡条件下,仅靠一己之责,事态发展必定走向无力回天
- TOMCAT类似iis主机头配置方法