__atoi64无法正常工作 [英] __atoi64 not working correctly

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

问题描述

向大家致意,


阅读C''99添加的int64_t类型,我决定为我实现64位版本的atoi
a自己的图书馆但是,对于

提供的测试编号,它没有做它应该做的事情。

(我使用GCC / MingW32作为编译器用C'' 99启用。哦,那里

在这段代码中没有溢出检查!)代码如下:


#include< ctype.h>


#ifdef __C99__

#include< stdint.h>


int64_t

__atoi64(const char * numstr)

{

int64_t result = 0; / *存储结果数* /

寄存器int ch; / *当前字符* /

int sign =''+''; / *数字的符号* /


/ *尝试删除它并传递(const char *)NULL。 ;)* /

if(NULL == numstr)

返回0;


/ *跳过前导空格* /

while(isspace(* numstr))

++ numstr;


/ *得到标志。含。 '''''因为它有效! * /

sign = * numstr;

if(''+'= = sign ||'' - ''== sign)

++ numstr;


/ *每次将数字左移一个地方

*并坚持使用新数字。

* /

while((ch = * numstr)&& isdigit(ch))

{

result * = 10;

结果+ = ch - ''0'';

++ numstr;

}


/ *带符号返回。 * /

返回(('' - ''==签名)? - 结果:结果);

}

#endif / *不是__C99__ * /


#ifdef测试

#include< stdio.h>

int64_t __atoi64(const char * numstr );


int

main(int argc,char ** argv)

{

const char * numstr =" 18726761288" ;;


/ *具体实施:

* LONG_LONG_MAX在limits.h = 9223372036854775807LL

* typedef long long int64_t;

* /


#ifdef __C99__

printf("%s:% lld \ n",numstr,__atoi64(numstr));

#endif / * not __C99__ * /


返回0;

}

#endif / *不是测试* /


我想知道为什么这不起作用。任何帮助将不胜感激。

休息时间。 :)


问候,

Jonathan。


-

"我正在学习编程因为那时我可以编写

程序来更快地完成我的作业。 - Andy Anfilofieff

Greetings everyone,

Reading about the int64_t types added by C''99, I decided to implement
a 64-bit version of atoi for my own library. However, for
the test number provided, it isn''t doing what it is supposed to do.
(I used GCC/MingW32 as the compiler with C''99 enabled. Oh, and there
is no overflow-checking in this code!) The code follows:

#include <ctype.h>

#ifdef __C99__
#include <stdint.h>

int64_t
__atoi64 (const char *numstr)
{
int64_t result = 0; /* stores the resulting number */
register int ch; /* current character */
int sign = ''+''; /* sign of the number */

/* Try removing this and pass (const char*)NULL. ;) */
if (NULL == numstr)
return 0;

/* skip leading whitespace */
while (isspace(*numstr))
++numstr;

/* get the sign. incl. ''+'' because it is valid! */
sign = *numstr;
if (''+'' == sign || ''-'' == sign)
++numstr;

/* shift digits to left by one place each time
* and stick the new digit in.
*/
while ( (ch = *numstr) && isdigit(ch) )
{
result *= 10;
result += ch - ''0'';
++numstr;
}

/* return with sign. */
return ((''-'' == sign) ? -result : result);
}
#endif /* not __C99__ */

#ifdef TEST
#include <stdio.h>
int64_t __atoi64 (const char *numstr);

int
main (int argc, char **argv)
{
const char *numstr = "18726761288";

/* Implementation-specific:
* LONG_LONG_MAX as in limits.h = 9223372036854775807LL
* typedef long long int64_t;
*/

#ifdef __C99__
printf ("%s: %lld\n", numstr, __atoi64 (numstr));
#endif /* not __C99__ */

return 0;
}
#endif /* not TEST */

I wonder why this isn''t working. Any help will be appreciated.
Nap time. :)

Regards,
Jonathan.

--
"I''m learning to program because then I can write
programs to do my homework faster." - Andy Anfilofieff

推荐答案

Jonathan Burd写道:
Jonathan Burd wrote:
问候大家,

阅读有关由C''99添加的int64_t类型,我决定为我自己的库实现一个64位版本的atoi。但是,对于提供的测试编号,它没有做它应该做的事情。
(我使用GCC / MingW32作为编译器,启用了C''99。哦,那里
在此代码中没有溢出检查!)代码如下:

#include< ctype.h>

#ifdef __C99__
#include < stdint.h>

int64_t
__atoi64(const char * numstr)
{
int64_t result = 0; / *存储结果数* /
寄存器int ch; / *当前字符* /
int sign =''+''; / *数字的符号* /

/ *尝试删除它并传递(const char *)NULL。 ;)* /
如果(NULL == numstr)
返回0;

/ *跳过前导空格* /
while(isspace(* numstr))
++ numstr;

/ *得到标志。含。 '''''因为它有效! * /
sign = * numstr;
if(''+'= = sign ||'' - ''== sign)
++ numstr;

/ *每次将数字左移一个地方
*并将新数字保持在。
* /
while((ch = * numstr)&& isdigit(ch ))
{
结果* = 10;
结果+ = ch - ''0'';
++ numstr;
}

/ *带符号返回。 * /
返回(('' - ''==签名)? - 结果:结果);
}
#endif / * not __C99__ * /

#ifdef TEST
#include< stdio.h>
int64_t __atoi64(const char * numstr);

int
main(int argc,char ** argv )
{
const char * numstr =" 18726761288" ;;

/ *具体实施:
* LONG_LONG_MAX在limits.h = 9223372036854775807LL * typedef long long int64_t;
* /

#ifdef __C99__
printf("%s:%lld \ n",numstr,__atoi64(numstr));
#endif / * not __C99__ * /

返回0;
}
#endif / *不测试* /

我想知道为什么这不起作用。任何帮助将不胜感激。
休息时间。 :)

问候,
Jonathan。
Greetings everyone,

Reading about the int64_t types added by C''99, I decided to implement
a 64-bit version of atoi for my own library. However, for
the test number provided, it isn''t doing what it is supposed to do.
(I used GCC/MingW32 as the compiler with C''99 enabled. Oh, and there
is no overflow-checking in this code!) The code follows:

#include <ctype.h>

#ifdef __C99__
#include <stdint.h>

int64_t
__atoi64 (const char *numstr)
{
int64_t result = 0; /* stores the resulting number */
register int ch; /* current character */
int sign = ''+''; /* sign of the number */

/* Try removing this and pass (const char*)NULL. ;) */
if (NULL == numstr)
return 0;

/* skip leading whitespace */
while (isspace(*numstr))
++numstr;

/* get the sign. incl. ''+'' because it is valid! */
sign = *numstr;
if (''+'' == sign || ''-'' == sign)
++numstr;

/* shift digits to left by one place each time
* and stick the new digit in.
*/
while ( (ch = *numstr) && isdigit(ch) )
{
result *= 10;
result += ch - ''0'';
++numstr;
}

/* return with sign. */
return ((''-'' == sign) ? -result : result);
}
#endif /* not __C99__ */

#ifdef TEST
#include <stdio.h>
int64_t __atoi64 (const char *numstr);

int
main (int argc, char **argv)
{
const char *numstr = "18726761288";

/* Implementation-specific:
* LONG_LONG_MAX as in limits.h = 9223372036854775807LL
* typedef long long int64_t;
*/

#ifdef __C99__
printf ("%s: %lld\n", numstr, __atoi64 (numstr));
#endif /* not __C99__ */

return 0;
}
#endif /* not TEST */

I wonder why this isn''t working. Any help will be appreciated.
Nap time. :)

Regards,
Jonathan.




您的代码与lcc-win32完美配合。

我得到:

18726761288:18726761288

lccwin32: http://www.cs.virginia.edu/~lcc-win32


jacob navia写道:
jacob navia wrote:
Jonathan Burd写道:
< snip>
你的代码与lcc-win32完美配合。
我得到:
18726761288:18726761288

lccwin32: http://www.cs.virginia .edu / ~lcc-win32
Jonathan Burd wrote: <snip>
Your code works perfectly with lcc-win32.
I get:
18726761288: 18726761288

lccwin32: http://www.cs.virginia.edu/~lcc-win32




英特尔的编译器和GCC给我这个:


18726761288:1546892104

问候,

Jonathan。


-

" ;我正在学习编程,因为那样我就可以编写

程序来更快地完成我的作业。 - Andy Anfilofieff



Intel''s compiler and GCC give me this:

18726761288: 1546892104

Regards,
Jonathan.

--
"I''m learning to program because then I can write
programs to do my homework faster." - Andy Anfilofieff


Jonathan Burd写道:
Jonathan Burd wrote:
jacob navia写道:
jacob navia wrote:
Jonathan Burd写道:
Jonathan Burd wrote:



< snip>



<snip>


您的代码与lcc-win32完美配合。
我得到:
18726761288:18726761288
lccwin32: http: //www.cs.virginia.edu/~lcc-win32



英特尔的编译器和GCC给我这个:

18726761288 :1546892104

问候,Jonathan。


Intel''s compiler and GCC give me this:

18726761288: 1546892104

Regards,
Jonathan.




这很清楚。如果你制作(int)18726761288LL你

获得1546892104.


你的代码对我来说似乎是对的。打印机的编译器或它们的实现是错误的。

请注意,英特尔编译器需要一些额外的开关才能进入C99。模式。请参阅文档。


如果您在Windows下使用gcc,它似乎无法工作

具有一些C99功能。


如果所有其他方法都失败了,请下载lcc-win32 ...它正在运行,至少

这个代码的含义。


雅各布



This is very clear. If you make (int)18726761288LL you
obtain 1546892104.

Your code seems correct to me. The compilers or their
implementation of printf are buggy.
Note that the Intel compiler needs some extra switch to
get into "C99" mode. See the documentation.

If you are using gcc under windows it doesn''t seem to work
with some C99 features.

If all else fails, download lcc-win32... It is working, at least
what this code is concerned.

jacob


这篇关于__atoi64无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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