ps里的复制快捷键:设计函数 int atoi(char *s)。(高手帮帮小弟)

来源:百度文库 编辑:神马品牌网 时间:2024/04/30 18:14:24
请高手指点,是联想的一道笔试题
还有:2.int i=(j=4,k=8,l=16,m=32); printf(“%d”, i); 输出是多少?
3.论述含参数的宏与函数的优缺点。
急啊~~谢谢啦
如果第2,3题能够详细点就好了.....谢谢
譬如第2题是如何运算的.
还有,3楼和4楼的答案有出入.究竟是谁的正确啊,我是初学者,请达人们指教指教啊~~~

1。微软的VC++中的实现
/***
*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);
}
2。输出32,逗号运算符从左到右执行,返回最右边的一个数。
3。宏的缺点是不进行类型检查,有点是可以左某些特殊的工作,比如字符串连接的,宏在预处理阶段展开。 C++的语言设计者本人强烈建议使用inline函数来代替宏。

1. atoi是把字符串转化为整形数,比如“980”变成980
2. 输出4
3. 含参数的宏缺点主要是可读性差,容易出错,优点是在预处理阶段完成,不占用编译时间

不知道啊