函数实现-aoti-atol
1. 函數原型
??? int atoi ( const char * str );
??? long int atol ( const char * str );
2. 函數工作
??? atoi是將字符串轉化為整型,atol是將字符串轉化為長整型,這兩個函數的實現十分相似。工作步驟基本如下:
?? ·跳過若干空格、制表符等
?? ·如果有正號或者負號,進行識別
?? ·將數字整合到結果中,直到字符串遍歷完畢或者遇到數字以外的其他字符為止。
??? C庫中的atoi會盡可能的把更多的數字整合到結果中,不能發現數值溢出的問題。而且對于“整數+其他字符”這樣的字符串識別出其中的“整數”,并不考慮后面的字符串中有沒有非法字符。比如"1234abc",atoi的結果是1234,而不會認為"1234abc"本身是非法的。
??? 本文給出的aoti實現會考慮到如下幾個方面:
??? 如果沒有一個數字被整合到字符串中,拋出異常“沒有數字存在”。
??? 如果數值溢出,拋出異常,“數值溢出”。
????對于"1234abc"這樣的數字,保留原有atoi的處理形式,也識別出1234。
3. 函數設計
????為了更好的展示思路,這里我給出一個基于狀態機的aoti實現。???
#include<iostream>using?namespace?std;
int?my_atoi(const?char?*str)?{
??if(str?==?NULL)
????throw?"str?==?NULL!";
??enum?STATE?{STATE_SKIP_SPACE,?STATE_RECOGNIZE_SIGN,?STATE_RECOGNIZE_NUM,
????STATE_RECOGNIZE_LAST_NUM,?STATE_RECOGNIZE_TAIL_CHAR};
??STATE?state?=?STATE_SKIP_SPACE;
??int?max?=?INT_MAX;
??int?min?=?INT_MIN;
??int?bound?=?max?/?10;
??int?sign?=?1;
??unsigned?int?result?=?0;
??bool?find_num?=?false;
??while(true)?{
????switch(state)?{
??????case?STATE_SKIP_SPACE: // 跳過盡可能多的空格
????????if(*str=='?'||*str=='\t'||*str=='\n'||*str=='\f'||*str=='\b'||*str=='\r')
??????????str++;
????????else?
??????????state?=?STATE_RECOGNIZE_SIGN;
????????break;
??????case?STATE_RECOGNIZE_SIGN: // 識別可能的正負符號
????????if(*str?==?'-')?{
??????????str++;
??????????sign?=?-1;??????????
????????}
????????else?if(*str?==?'+')?{
??????????str++;
????????}
????????state?=?STATE_RECOGNIZE_NUM;
????????break;
??????case?STATE_RECOGNIZE_NUM:?//?最多識別max/10的位數的數字?
????????if(*str?<'0'?||?*str>'9'?||?*str=='\0')?//?當前字符不是數字,或者已經結束?
??????????if(find_num?==?false)?//?一個數字都還沒遇到過?
????????????throw??"no?num?found!";
??????????else??//?已經遇到過數字了?
????????????return?result?*?sign;
????????else?{?//?當前字符是數字?
??????????if(find_num?==?false)
????????????find_num?=?true;??
??????????result?*=?10;
??????????result?+=?(*str-'0');
??????????bound?/=?10;
??????????if(bound?==0)?{
????????????state?=?STATE_RECOGNIZE_LAST_NUM;
??????????}
??????????str++;
????????}
????????break;
??????case?STATE_RECOGNIZE_LAST_NUM: //?識別最后一個數字
????????if(*str?<'0'?||?*str>'9'?||?*str=='\0')?//?當前字符不是數字,或者已經結束?
??????????return?result?*?sign;?//?能到這一步,肯定是已經遇到了很多數字?
????????else?{?//?當前字符是數字
??????????if((result?>?max/10)?||?(sign==1?&&?(*str-'0')>(max%10))?||?(sign==-1?&&?(*str-'0')>abs(min%10)))?{
????????????throw?"over?num?limit?!";
??????????}
??????????else?{
????????????result?*=?10;
????????????result?+=?(*str?-?'0');
????????????state?=?STATE_RECOGNIZE_TAIL_CHAR;
????????????str++;
??????????}
????????}
????????break;
??????case?STATE_RECOGNIZE_TAIL_CHAR: //?不能再識別數字了
????????if(*str?<'0'?||?*str>'9'?||?*str=='\0')?//?當前字符不是數字,或者已經結束
??????????return?result?*?sign;
????????else?//?當前字符是數字,此時必然溢出?
??????????throw?"over?num?limit?!";
????????break;
??????default:
????????break;
????}
??}
??return?0;
}
int?main()?{
??char*?a?=?"????????+123213123abc";
??char*?max?=?"?????2147483647abc";
??char*?min?=?"?????-2147483648abc";
??char*?max_more_1?=?"?????2999999997abc";
??char*?max_more_2?=?"?????2147483648abc";
??char*?min_more_1?=?"?????-2147483649abc";
??cout?<<?a?<<?":??"?<<?my_atoi(a)?<<?endl;
??cout?<<?max?<<?":??"?<<?my_atoi(max)?<<?endl;
??cout?<<?min?<<?":??"?<<?my_atoi(min)?<<?endl;?
??try?{
????cout?<<?max_more_1?<<?":??";
????my_atoi(max_more_1);
??}
??catch?(const?char*?info){
????cout?<<?info?<<?endl;
??}
??try?{
????cout?<<?max_more_2?<<?":??";
????my_atoi(max_more_2);
??}
??catch?(const?char*?info){
????cout?<<?info?<<?endl;
??}
??try?{
????cout?<<?min_more_1?<<?":??";
????my_atoi(min_more_1);
??}
??catch?(const?char*?info){
????cout?<<?info?<<?endl;
??}
??char?wait;
??cin?>>?wait;
??return?0;
}
??? long int 的轉化與int的轉化差不多。
4. 參考資料
????C++ Reference atoi??? http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/?
????C/C++常見面試題(30)----atoi函數??? http://blog.csdn.net/lzueclipse/archive/2011/05/26/6446945.aspx
??? 程序員面試題精選---“itoa函數”和“atoi函數??? http://blog.csdn.net/zhangxinrun/archive/2010/12/01/6048695.aspx
??? atoi的實現,一道對數值邊界考察的題目??? http://hi.baidu.com/xuelianglv/blog/item/cda76b2c52fbdded8a1399fe.html
??? C庫函數atoi的實現和一些討論_c/c++_電腦編程網??? http://blog.csdn.net/udknight/archive/2007/10/22/1836799.aspx???
轉載于:https://www.cnblogs.com/pangxiaodong/archive/2011/06/17/2083471.html
總結
以上是生活随笔為你收集整理的函数实现-aoti-atol的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: sql 替换text字段中的指定字符
- 下一篇: 酒店预定系统—需求规格说明书