免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 6376 | 回复: 17
打印 上一主题 下一主题

[C] 如何实现一个简单高效的atoi [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-04-03 12:33 |只看该作者 |倒序浏览
如何实现一个简单高效的atoi
完全不能用到标准库中的东西,环境*nux,大牛些,帮忙看看吧!

论坛徽章:
0
2 [报告]
发表于 2010-04-03 12:49 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
3 [报告]
发表于 2010-04-03 12:55 |只看该作者
本帖最后由 peidright 于 2010-04-03 12:56 编辑

去看 libc里面的实现不就行了。我写的话。会这样? bug肯定有的,将就下

int atoi(const char *str)
{
    int sum,i;
    char *s;
    s = str;
    sum = 0;
    while(*s++){
        if( (i = *s - '0')>=0 && *s <='9')
        {
            sum = sum * 10 + i;
            continue;
        }
        return -1;
    }
    return sum;
}

论坛徽章:
0
4 [报告]
发表于 2010-04-03 13:14 |只看该作者
我的实现,未必高效,但能用。
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <limits.h>
  4. int myatoi(const char*str);

  5. int main()
  6. {
  7.         unsigned int i = 0,j=0;
  8.         char *s="123423432423424234";
  9.         i = atoi(s);
  10.         j = myatoi(s);
  11.         printf("i is :%d,%d\n",i,j);
  12.         return 0;
  13. }

  14. int myatoi(const char*str) {
  15.         if(NULL == str || '\0' == *str) {
  16.                 return 0;
  17.         }
  18.         int ret=0,left_bit=sizeof(int)*8-1;
  19.         unsigned int tmp=0;
  20.         while(*str>='0' && *str <='9') {
  21.                 tmp = (*str-'0') + ret*10;
  22.                 if(tmp>>left_bit==1) {
  23.                         return INT_MAX;
  24.                 }
  25.                 ret = tmp;
  26.                 str++;
  27.         }
  28.         return ret;
  29. }
复制代码

论坛徽章:
0
5 [报告]
发表于 2010-04-03 17:46 |只看该作者
难道标准库里的不够高效,还是想看看怎么写才高效?

论坛徽章:
0
6 [报告]
发表于 2010-04-03 17:51 |只看该作者
{:3_187:},邀请论坛的c水门都写份吧。。。。。。要不容易水! 不准看书,不准看库哦!

论坛徽章:
0
7 [报告]
发表于 2010-04-03 18:02 |只看该作者
去看 libc里面的实现不就行了。我写的话。会这样? bug肯定有的,将就下

int atoi(const char *str)
{
...
peidright 发表于 2010-04-03 12:55



   good idea !but , it's hard for me to read glibc

论坛徽章:
0
8 [报告]
发表于 2010-04-03 18:03 |只看该作者
难道标准库里的不够高效,还是想看看怎么写才高效?
bluewaterray 发表于 2010-04-03 17:46



    in kernel can be used?

论坛徽章:
0
9 [报告]
发表于 2010-04-03 22:04 |只看该作者
本帖最后由 mmgg00 于 2010-04-03 22:10 编辑

回复 1# sh19871122

  1. #line 1 "atoi.rl"
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdio.h>


  5. #line 8 "atoi.c"
  6. static const int atoi_start = 1;
  7. static const int atoi_first_final = 4;
  8. static const int atoi_error = 0;

  9. static const int atoi_en_main = 1;

  10. #line 8 "atoi.rl"


  11. long long myatoi( char *str )
  12. {
  13.         char *p = str, *pe = str + strlen( str );
  14.         int cs;
  15.         long long val = 0;
  16.         int neg = 0;

  17.        
  18. #line 26 "atoi.c"
  19.         {
  20.         cs = atoi_start;
  21.         }

  22. #line 31 "atoi.c"
  23.         {
  24.         if ( p == pe )
  25.                 goto _test_eof;
  26.         switch ( cs )
  27.         {
  28. case 1:
  29.         switch( (*p) ) {
  30.                 case 43: goto st2;
  31.                 case 45: goto tr2;
  32.         }
  33.         if ( 48 <= (*p) && (*p) <= 57 )
  34.                 goto tr3;
  35.         goto st0;
  36. st0:
  37. cs = 0;
  38.         goto _out;
  39. tr2:
  40. #line 18 "atoi.rl"
  41.         {
  42.                         neg = 1;
  43.                 }
  44.         goto st2;
  45. st2:
  46.         if ( ++p == pe )
  47.                 goto _test_eof2;
  48. case 2:
  49. #line 58 "atoi.c"
  50.         if ( 48 <= (*p) && (*p) <= 57 )
  51.                 goto tr3;
  52.         goto st0;
  53. tr3:
  54. #line 22 "atoi.rl"
  55.         {
  56.                         val = val * 10 + ((*p) - '0');
  57.                 }
  58.         goto st3;
  59. st3:
  60.         if ( ++p == pe )
  61.                 goto _test_eof3;
  62. case 3:
  63. #line 72 "atoi.c"
  64.         if ( (*p) == 10 )
  65.                 goto st4;
  66.         if ( 48 <= (*p) && (*p) <= 57 )
  67.                 goto tr3;
  68.         goto st0;
  69. st4:
  70.         if ( ++p == pe )
  71.                 goto _test_eof4;
  72. case 4:
  73.         goto st0;
  74.         }
  75.         _test_eof2: cs = 2; goto _test_eof;
  76.         _test_eof3: cs = 3; goto _test_eof;
  77.         _test_eof4: cs = 4; goto _test_eof;

  78.         _test_eof: {}
  79.         _out: {}
  80.         }
  81. #line 33 "atoi.rl"


  82.         if ( neg )
  83.                 val = -1 * val;

  84.         if ( cs < atoi_first_final )
  85.                 fprintf( stderr, "atoi: there was an error\n" );

  86.         return val;
  87. };


  88. #define BUFSIZE 1024

  89. int main()
  90. {
  91.         char buf[BUFSIZE];
  92.         while ( fgets( buf, sizeof(buf), stdin ) != 0 ) {
  93.                 long long value = myatoi( buf );
  94.                 printf( "%lld\n", value );
  95.         }
  96.         return 0;
  97. }
复制代码

论坛徽章:
0
10 [报告]
发表于 2010-04-04 00:09 |只看该作者
回复  sh19871122
mmgg00 发表于 2010-04-03 22:04

不得不说,代码看起来很复杂,细细分析,没这么复杂,用了很多#line和跳转,不得不说我不习惯
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP