典型c庫函數的實現

來源:互聯網
上載者:User

標籤:style   blog   color   ar   div   amp   log   on   

StrToInt:字串轉int輸出

 1 enum Status  2 { 3 kValid = 0, 4 kInvalid = 1, 5 }; 6  7 int StrToInt(const char* str) 8 { 9     g_nStatus = kInvalid;10     long long num = 0;11 12     if(str!=NULL && *str != ‘\0‘)13     {14         bool minus = false;15         while(*str =="")16             str++;17         if(*str ==‘+‘)18         {19             str++;20         }21         else if(*str==‘-‘)22         {23             str++;24             minus = true;25         }26 27         if(*str !=‘\0‘)28         {29             num = StrToIntCore(str, minus);30        }31     }32     return (int)num;33 }34 35 int StrToIntCore(const char *digit, bool minus)36 {37     long long num = 0;38     while(*digit !=‘0‘)39     {40         if(*digit >=‘0‘ && *digit<=‘9‘)41         {42             int flag = minus ? -1:1;43             num =num*10 + flag*(*digit -‘0‘);44 45             //判斷整數是否發生上溢/下溢46             if((!minus && num > 0x7FFFFFFF) //最大正數:2^32-147                 || (minus && num <(signed int)0x80000000))//最小負數:2^3248             {49                 num = 0;50                 break;51             }52             digit++;53         }54         else55         {56             num = 0;57             break;58         }59     }60     if(*digit==‘\0‘)61     {62         g_nStatus = kValid;63     }64 65     return num;66 }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.