📣 黑五价提前开抢|VIP一年通行证5折,蓝莓、Offer多多同步特惠
123
返回列表 发新帖
楼主: Imbalism
跳转到指定楼层
上一主题 下一主题
收起左侧

Facebook :实现atoi函数

🔗
jason_k_ying 2013-9-26 11:50:23 | 只看该作者
全局:
wwwyhx 发表于 2011-10-11 08:25
25分钟写的, 还调试了....

good
建议限制"+0"、"-0"及其后字符串属于无效
回复

使用道具 举报

🔗
jason_k_ying 2013-9-26 11:50:53 | 只看该作者
全局:
  1. /* str2int
  2.    Convert string to integer.
  3.    input: const char*
  4.    output: int

  5.    Notice:
  6.    1. Input string check;
  7.    2. -0 & +0 is not allowed
  8.    3. Integer overflow: invalid

  9.     By Jason K.Ying 20130926
  10. */

  11. #include <iostream>
  12. using namespace std;

  13. class CException{
  14. public:
  15.     CException(const char* str):m_str(str){}
  16.     string getErrorInfo(){ return m_str; }
  17. private:
  18.     string m_str;
  19. };

  20. int myatoi(const char* q){

  21.     if( NULL == q )
  22.         throw CException("Invalid Input style: NULL pointor.");

  23.     int nFlg = 1;

  24.     if( '-' == *q )
  25.         nFlg = -1;

  26.     if( '-' == *q || '+' == *q ){
  27.         q++;
  28.         if( '0' == *q )
  29.             throw CException("Invalid Input style: -0 or +0.");
  30.     }

  31.     int Res = 0;
  32.     int Tmp = 0;
  33.     while( '\0' != *q ){
  34.         if( '0'>*q || '9'<*q )
  35.             throw CException("Invalid Input style: invalid charactor.");

  36.         Tmp = Res*10+nFlg*(*q-'0');
  37.         if( Res!=0 && (Tmp&0x80000000) != (Res&0x80000000) ) // Res != 0 is necessary for minus number
  38.             throw CException("Digit Overflow.");

  39.         Res = Tmp;
  40.         q++;
  41.     }

  42.     return Res;
  43. }

  44. int main(){

  45.     // Test Cases
  46.     char p[10][12] = {
  47.                         "2147483648",
  48.                         "-2147483649",
  49.                         "1","-1",
  50.                         "0","-0","+0",
  51.                         "12a45","a2345",
  52.                         ""
  53.                      };

  54.     long len[10] = {0};
  55.     cout<<"test result:"<<endl;
  56.     for(int i=0;i<10;++i){
  57.         len[i] = myatoi(p[i]);
  58.         cout<<len[i]<<endl;
  59.     }

  60.     return 0;
  61. }
复制代码
回复

使用道具 举报

🔗
jason_k_ying 2013-9-26 11:51:44 | 只看该作者
全局:
  1. /* str2int
  2.    Convert string to integer.
  3.    input: const char*
  4.    output: int

  5.    Notice:
  6.    1. Input string check;
  7.    2. -0 & +0 is not allowed
  8.    3. Integer overflow: invalid

  9.     By Jason K.Ying 20130926
  10. */

  11. #include <iostream>
  12. using namespace std;

  13. class CException{
  14. public:
  15.     CException(const char* str):m_str(str){}
  16.     string getErrorInfo(){ return m_str; }
  17. private:
  18.     string m_str;
  19. };

  20. int myatoi(const char* q){

  21.     if( NULL == q )
  22.         throw CException("Invalid Input style: NULL pointor.");

  23.     int nFlg = 1;

  24.     if( '-' == *q )
  25.         nFlg = -1;

  26.     if( '-' == *q || '+' == *q ){
  27.         q++;
  28.         if( '0' == *q )
  29.             throw CException("Invalid Input style: -0 or +0.");
  30.     }

  31.     int Res = 0;
  32.     int Tmp = 0;
  33.     while( '\0' != *q ){
  34.         if( '0'>*q || '9'<*q )
  35.             throw CException("Invalid Input style: invalid charactor.");

  36.         Tmp = Res*10+nFlg*(*q-'0');
  37.         if( Res!=0 && (Tmp&0x80000000) != (Res&0x80000000) ) // Res != 0 is necessary for minus number
  38.             throw CException("Digit Overflow.");

  39.         Res = Tmp;
  40.         q++;
  41.     }

  42.     return Res;
  43. }

  44. int main(){

  45.     // Test Cases
  46.     char p[10][12] = {
  47.                         "2147483648",
  48.                         "-2147483649",
  49.                         "1","-1",
  50.                         "0","-0","+0",
  51.                         "12a45","a2345",
  52.                         ""
  53.                      };

  54.     long len[10] = {0};
  55.     cout<<"test result:"<<endl;
  56.     for(int i=0;i<10;++i){
  57.         len[i] = myatoi(p[i]);
  58.         cout<<len[i]<<endl;
  59.     }

  60.     return 0;
  61. }
复制代码
一亩三分地严打"顶""好贴""收藏了"之类的垃圾回复帖!被警告三次,系统会自动封杀ID!

想支持楼主,请点击帖子下方的"好苗""分享""收藏"键,酌情给楼主加大米(系统不扣你自己的分)。
积分不够看不了帖子,请参考论坛导航里的"帮助","新手提纲"里有攒积分指南
回复

使用道具 举报

🔗
sxnfnj 2013-10-10 11:17:37 | 只看该作者
全局:
各位高手十分厉害,受教!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表