atoi自实现

来源:互联网 发布:spark大数据 编辑:程序博客网 时间:2024/05/17 14:24

int myatoi(const char *pszStr)
{
 if (NULL == pszStr)
 {
  return 0;
 }

 int nTemp = 0, nResult = 0;
 int i = 0;
 BOOL bMinus = FALSE;

 if ('-' == pszStr[0])
 {
  bMinus = TRUE;
  i = 1;
 }

 while (pszStr[i] != '/0')
 {
  if (pszStr[i] >= '0' && pszStr[i] <= '9')
  {
   nResult = nResult * 10;
   nTemp = pszStr[i] - '0';
   nResult = nResult + nTemp;
  }
  else
  {
   break;
  }
  i++;
 }

 if (bMinus)
 {
  nResult = nResult * -1;
 }

 return nResult;
}

原创粉丝点击