请问如何把"123"变成123

qhgary 2004-01-26 11:48:35
不用atoi,不用其他第三方库,不用('1'-'0')*100+('2'-'0')*10+('3'-'0')的方法,要求尽可能高效,用C语言实现
...全文
173 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
viking2001 2004-02-02
  • 打赏
  • 举报
回复
这样算不算?

#include<iostream.h>
void main()
{
char str[]="123";
int i=0;
int val=0;
int tmp;
while(str[i]!='\0')
{
val=val<<1;
tmp=val<<2;
val+=tmp;
tmp=str[i] & 0xf;
val+=tmp;
i++;
}
cout << val;
}
qhgary 2004-02-02
  • 打赏
  • 举报
回复
还有呢?
fierygnu 2004-01-30
  • 打赏
  • 举报
回复
sscanf不就行了。。。
shornmao 2004-01-30
  • 打赏
  • 举报
回复
-'0'后*10,这就是最高效的方法了,不过为了兼容其他字符集,例如非ASC字符集,在可移植的库版本中,可能采用查表法,将'0'-'9'和0-9建立一个对照表,用查表法,而不是-'0',但是乘10是不可避免的。
从效率上来说,-'0'比查表要快一点,因为-'0'这个操作实在是太简单了。
qhgary 2004-01-30
  • 打赏
  • 举报
回复
这些说穿了不还是-'0'然后*10么,那跟我说的有什么区别,郁闷。。。
LVOLCANO 2004-01-30
  • 打赏
  • 举报
回复
study
sevencat 2004-01-29
  • 打赏
  • 举报
回复
vc 2003的实现

#ifndef _UNICODE
#define _tchartodigit(c) ((c) >= '0' && (c) <= '9' ? (c) - '0' : -1)
#else /* _UNICODE */
int _wchartodigit(wchar_t);
#define _tchartodigit(c) _wchartodigit((wchar_t)(c))
#endif /* _UNICODE */

/***
*long atol(char *nptr) - Convert string to long
*
*Purpose:
* Converts ASCII string pointed to by nptr to binary.
* Overflow is not detected.
*
*Entry:
* nptr = ptr to string to convert
*
*Exit:
* return long int value of the string
*
*Exceptions:
* None - overflow is not detected.
*
*******************************************************************************/

long __cdecl _tstol(
const _TCHAR *nptr
)
{
int c; /* current char */
long total; /* current total */
int sign; /* if '-', then negative, otherwise positive */
#if defined (_MT) && !defined (_UNICODE)
pthreadlocinfo ptloci = _getptd()->ptlocinfo;

if ( ptloci != __ptlocinfo )
ptloci = __updatetlocinfo();

/* skip whitespace */
while ( __isspace_mt(ptloci, (int)(_TUCHAR)*nptr) )
#else /* defined (_MT) && !defined (_UNICODE) */
while ( _istspace((int)(_TUCHAR)*nptr) )
#endif /* defined (_MT) && !defined (_UNICODE) */
++nptr;

c = (int)(_TUCHAR)*nptr++;
sign = c; /* save sign indication */
if (c == _T('-') || c == _T('+'))
c = (int)(_TUCHAR)*nptr++; /* skip sign */

total = 0;

while ( (c = _tchartodigit(c)) != -1 ) {
total = 10 * total + c; /* accumulate digit */
c = (_TUCHAR)*nptr++; /* get next char */
}

if (sign == '-')
return -total;
else
return total; /* return result, negated if necessary */
}


/***
*int atoi(char *nptr) - Convert string to long
*
*Purpose:
* Converts ASCII string pointed to by nptr to binary.
* Overflow is not detected. Because of this, we can just use
* atol().
*
*Entry:
* nptr = ptr to string to convert
*
*Exit:
* return int value of the string
*
*Exceptions:
* None - overflow is not detected.
*
*******************************************************************************/

int __cdecl _tstoi(
const _TCHAR *nptr
)
{
return (int)_tstol(nptr);
}

#ifndef _NO_INT64

__int64 __cdecl _tstoi64(
const _TCHAR *nptr
)
{
int c; /* current char */
__int64 total; /* current total */
int sign; /* if '-', then negative, otherwise positive */
#if defined (_MT) && !defined (_UNICODE)
pthreadlocinfo ptloci = _getptd()->ptlocinfo;

if ( ptloci != __ptlocinfo )
ptloci = __updatetlocinfo();

/* skip whitespace */
while ( __isspace_mt(ptloci, (int)(_TUCHAR)*nptr) )
#else /* defined (_MT) && !defined (_UNICODE) */
while ( _istspace((int)(_TUCHAR)*nptr) )
#endif /* defined (_MT) && !defined (_UNICODE) */
++nptr;

c = (int)(_TUCHAR)*nptr++;
sign = c; /* save sign indication */
if (c == _T('-') || c == _T('+'))
c = (int)(_TUCHAR)*nptr++; /* skip sign */

total = 0;

while ( (c = _tchartodigit(c)) != -1 ) {
total = 10 * total + c; /* accumulate digit */
c = (_TUCHAR)*nptr++; /* get next char */
}

if (sign == _T('-'))
return -total;
else
return total; /* return result, negated if necessary */
}
bluesen 2004-01-29
  • 打赏
  • 举报
回复
以下是C++Builder实现,源代码如下:

/*---------------------------------------------------------------------------
* filename - atol.c
*
* function(s)
* atol - converts a string to a long
* atoi - converts a string to an int
* _wtol - converts a wide-character string to a long
* _wtoi - converts a wide-character string to an int
*--------------------------------------------------------------------------*/

/*
* C/C++ Run Time Library - Version 11.0
*
* Copyright (c) 1987, 2002 by Borland Software Corporation
* All Rights Reserved.
*
*/

/* $Revision: 9.4.2.1 $ */

#include <stdlib.h>
#include <ctype.h>
#include <tchar.h>

#undef atoi /* macro in stdlib */

/*--------------------------------------------------------------------------*

Name atol, _wtol - converts a string to an integer

Usage long atol(const char *nptr);
long _wtol(const wchar_t *nptr);

Prototype in stdlib.h

Description Convert a string to a long integer. The syntax of
the string must be:

long ::= [isspace]* [sign] digit [digit]*

Only decimal integers are acceptable.

Error handling is poor. The function will protect
itself (crash-proof) but there is no defined method
to return an error indication to the caller. The
result is undefined if the input string is invalid.

Return value converted long value of the input string. If the string
cannot be converted to a long, the return value is 0.

*---------------------------------------------------------------------------*/

long _RTLENTRY _EXPFUNC _ttol(const _TCHAR *strP)
{
_TCHAR c;
int is_neg;
long result;

result = 0; /* default result is 0 */

while (_istspace((c = *strP++))) /* skip any whitespace characters */
;

if (c == _TEXT('+') || c == _TEXT('-')) /* remember if negative sign seen */
{
is_neg = c == _TEXT('-');
c = *strP++; /* skip sign, get next char */
}
else
is_neg = 0;

while (c >= _TEXT('0') && c <= _TEXT('9')) /* accumulate digits, ignore overflow */
{
result = result * 10 + c - _TEXT('0');
c = *strP++;
}

return (is_neg ? -result : result); /* negate result if '-' seen */
}

/*--------------------------------------------------------------------------*

Name atoi, _wtoi - converts a string to an integer

Usage int atoi(char *nptr);
int atoi(wchar_t *nptr);

Prototype in stdlib.h

Description Convert ASCII string to word integer.

The only difference between this and the atol
function is whether the result is truncated.

Return value converted long value of the input string. If the string
cannot be converted to an int, the return value is 0.

*---------------------------------------------------------------------------*/

int _RTLENTRY _EXPFUNC _ttoi(const _TCHAR *strP)
{
return (int) _ttol (strP);
}
qhgary 2004-01-29
  • 打赏
  • 举报
回复
有人知道atoi本身是怎么实现的么?
不管是不是位与,总还是要经过*10左移。。。这样的步骤,本身"123"应该把123的信息包含进去的,有人知道效率更高的么?这是我面试的一道题目,就是这个题目害得我。。。
我当时就是说的-'0'然后*10这样求,但是看上去他并不满意。。。。
hackeryang 2004-01-28
  • 打赏
  • 举报
回复
"123"to 123
就是字符型到整形的转换
int covert(char *s)
{
int sum,temp;temp=0;sum=0;
for(;s;){
temp=in(*s):
sum=sum*10+temp:
}
return sum}
int in(char s){
for(i=0;i<10;i++)
if(s=Int(i))return i;//Int[]={0,1,2,3,5,6,7,8,9}
}
由于本人水平有限,c语言的工地不好只有思路,希望大家看的清楚,不清楚的问我给我留言
freewing 2004-01-27
  • 打赏
  • 举报
回复
用位与吧,'1'& 0FH,这样会快一点,不过!有什么意义吗???????????????
laogaifan 2004-01-27
  • 打赏
  • 举报
回复
还是不懂,
有区别吗?
'1'-'0'和做按位与运算,
两种方法有效率差别吗?
极速小王子 2004-01-27
  • 打赏
  • 举报
回复
怎么位与啊?介绍一下啊!
qhgary 2004-01-27
  • 打赏
  • 举报
回复
位与是一种方法,还有么? 我当时怎么没有想到呢。。。。ft
qhgary 2004-01-27
  • 打赏
  • 举报
回复
不知道是不是我表达不清楚,已经说了不用atoi,用C语言实现阿,而且不能用-‘0’的方法,好像上面说的都在我说得之列阿,我现在不是想写一个比atoi高效的什么东东,只是
想了解atoi里面怎么弄得,而且我确信不是-'0'
laogaifan 2004-01-27
  • 打赏
  • 举报
回复
。。。。。
会有其他方法吗?、
用汇编?
liquanle 2004-01-27
  • 打赏
  • 举报
回复
atoi
  • 打赏
  • 举报
回复
关注...
  • 打赏
  • 举报
回复
晕...sorry...搂住好像不要这个方法哈...

白编了...
  • 打赏
  • 举报
回复
小弟刚编了一个,不止是否符合搂住的意思,望指教

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

void main()
{
char *s = "123";
int len = strlen(s);
int number = 0;

int i = 0;
for (; len !=0; --len, ++i)
{
number += pow(10, len - 1) * (s[i] - '0');
}

printf("number = %d\n", number);
system("PAUSE");
}
加载更多回复(4)

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧