不准访问
- 积分
- 100
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2012-8-22
- 最后登录
- 1970-1-1
|
- /* str2int
- Convert string to integer.
- input: const char*
- output: int
- Notice:
- 1. Input string check;
- 2. -0 & +0 is not allowed
- 3. Integer overflow: invalid
- By Jason K.Ying 20130926
- */
- #include <iostream>
- using namespace std;
- class CException{
- public:
- CException(const char* str):m_str(str){}
- string getErrorInfo(){ return m_str; }
- private:
- string m_str;
- };
- int myatoi(const char* q){
- if( NULL == q )
- throw CException("Invalid Input style: NULL pointor.");
- int nFlg = 1;
- if( '-' == *q )
- nFlg = -1;
- if( '-' == *q || '+' == *q ){
- q++;
- if( '0' == *q )
- throw CException("Invalid Input style: -0 or +0.");
- }
- int Res = 0;
- int Tmp = 0;
- while( '\0' != *q ){
- if( '0'>*q || '9'<*q )
- throw CException("Invalid Input style: invalid charactor.");
- Tmp = Res*10+nFlg*(*q-'0');
- if( Res!=0 && (Tmp&0x80000000) != (Res&0x80000000) ) // Res != 0 is necessary for minus number
- throw CException("Digit Overflow.");
- Res = Tmp;
- q++;
- }
- return Res;
- }
- int main(){
- // Test Cases
- char p[10][12] = {
- "2147483648",
- "-2147483649",
- "1","-1",
- "0","-0","+0",
- "12a45","a2345",
- ""
- };
- long len[10] = {0};
- cout<<"test result:"<<endl;
- for(int i=0;i<10;++i){
- len[i] = myatoi(p[i]);
- cout<<len[i]<<endl;
- }
- return 0;
- }
复制代码 一亩三分地严打"顶""好贴""收藏了"之类的垃圾回复帖!被警告三次,系统会自动封杀ID!
想支持楼主,请点击帖子下方的"好苗""分享""收藏"键,酌情给楼主加大米(系统不扣你自己的分)。
积分不够看不了帖子,请参考论坛导航里的"帮助","新手提纲"里有攒积分指南 |
|