实现atoi [英] implement atoi

查看:123
本文介绍了实现atoi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道如何有效地实现这个功能?

解决方案

-----开始PGP签名留言--- -

哈希:SHA1


puzzlecracker写道:

有没有人知道如何有效地实现这个功能?




是的。


如果您知道如何有效地使用Google网上论坛,您会发现



comp.lang.c.这已经讨论过死亡(以及代码示例丰富)

实现atoi是一个小小的工作( ),这样C的任何学生都应该没有问题。


- -

Lew Pitcher

IT企业数据系统顾问,

企业技术解决方案,道明银行金融集团


(表达的意见是我自己的,而不是我的雇主'')
----- BEGIN PGP SIGNATURE -----

版本: GnuPG v1.2.4(MingW32)

iD8DBQFB7 / fdagVFX4UWr64RApWZAKCgmok2s6Xrrfqky / 04U6lxuHk / YwCgqdzX

igkeTZ8f3NJpYnBtvRMRpQs =

= Zh9L

----- END PGP SIGNATURE -----




" puzzlecracker" < IR ********* @ gmail.com>在消息中写道

news:11 ********************** @ c13g2000cwb.googlegr oups.com ...

有没有人知道如何有效地实现这个功能?




这是我对atoi的快速实现。请记住,这有
没有错误检查,因为我完全控制了这个

程序的输入 - 它对我有用。如果您需要错误检查,那么您需要

来添加它。此外,这是一个#define以获得最佳速度,它可能不会给出最好的文件大小,但是我的程序正在读取数千个,如果不是数百万的

来自ascii的数字,那么这是最好的方式(我也有一个等价的

strtod)。


Allan


#define ATOI(X ,结果,leng)\

做{\

char * lptr = X; \

结果= 0; \

leng = 0; \\ /

while(1)\

{\

if((* lptr> =''0'')& ;&(* lptr< =''9''))\

{\

结果* = 10; \

结果+ = * lptr - ''0''; \

lptr ++; \

leng ++; \

} \

其他\

{\

休息; \

} \

} \

}而(0)


< blockquote>

" puzzlecracker" < IR ********* @ gmail.com>在留言中写道

新闻:11 ********************** @ c13g2000cwb.googlegr oups.com ......

Re:实现atoi
有没有人知道如何有效地实现这个功能?




是的,我这样做。你呢?


-Mike


does anyone know how to implement this function efficiently?

解决方案

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

puzzlecracker wrote:

does anyone know how to implement this function efficiently?



Yes.

And if you knew how to use Google Groups effectively, you would have found that
this has been discussed to death (along with code examples galore) here in
comp.lang.c.

It is a minor effort to implement atoi(), such that any student of C should be
able to do it with no problem.

- --
Lew Pitcher
IT Consultant, Enterprise Data Systems,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed are my own, not my employers'')
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFB7/fdagVFX4UWr64RApWZAKCgmok2s6Xrrfqky/04U6lxuHk/YwCgqdzX
igkeTZ8f3NJpYnBtvRMRpQs=
=Zh9L
-----END PGP SIGNATURE-----



"puzzlecracker" <ir*********@gmail.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...

does anyone know how to implement this function efficiently?



Here is my fast implementation of atoi. Please bear in mind that this has
no error checking, and since I have total control of the inputs to this
program - it works for me. If you need error checking then you would need
to add it. Also, this is a #define for optimal speed, it might not give
best filesize but my program was reading thousands, if not millions of
numbers from ascii so this was the best way (I also have an equivalent
strtod).

Allan

#define ATOI(X, result, leng) \
do{ \
char *lptr = X; \
result = 0; \
leng = 0; \
while (1) \
{ \
if ((*lptr >= ''0'') && (*lptr <= ''9'')) \
{ \
result *= 10; \
result += *lptr - ''0''; \
lptr++; \
leng++; \
} \
else \
{ \
break; \
} \
} \
}while(0)



"puzzlecracker" <ir*********@gmail.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...

Re: implement atoi does anyone know how to implement this function efficiently?



Yes, I do. Do you?

-Mike


这篇关于实现atoi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆