atoi实现

1

int   atoi(const   char   *s)
{
      char   *p   =   s;
      char   c;
      int   i   =   0;
      while(c=*p++)
      {
            if(c>='0'   &&   c<='9')
            {
                  i   =   i*10   +   (c-'0');
            }
            else
                  return   -1;                     //Invalid   string
      }
      return   i;
}

2

#include   <iostream>
#include   <string>
#include   <algorithm>
using   namespace   std;

int   MyAtoi(const   string   &s)
{
string   str(s);
str.erase(remove(str.begin(),   str.end(),
                              '   '),
    str.end());

int   PositiveFlag   =   1;
if   (!str.empty())
{
if   (str.at(0)   ==   '-')  
{
PositiveFlag   =   -1;
str.erase(0,   1);
}

}

str.erase(str.find_first_not_of("0123456789"));

int   res   =   0;
for(string::iterator   pos   =   str.begin();   pos   !=   str.end();   ++pos)
{
res   =   res   *   10   +   static_cast<int>(*pos   -   0x30);
}

return   res   *   PositiveFlag;
}

int   main()
{
char   *s   =   "   a   3-9885   pigs";            
cout   <<   MyAtoi(s)   <<   endl;
return   0;
}

3

#include "stdafx.h"
#include <iostream>
using namespace std;

int my_atoi(char * pzNum)
{
 if (pzNum == NULL)
  return 0;

 int nLen = strlen(pzNum);
 if (nLen <= 0)
  return 0;

 int nIndex = 0; 
 bool bFlag = true;
 if (!isdigit(pzNum[0]))
 {
  if (pzNum[0] == '-')
   bFlag = false;
  else if (pzNum[0] == '+')
   bFlag = true;
  else
   return 0;
  
  nIndex ++;
 }

 int nRt = 0;
 for (; nIndex < nLen; nIndex++)
 {
  if (!isdigit(pzNum[nIndex]))
   break;
  nRt = nRt * 10 + (pzNum[nIndex]-'0');
 }
 if (!bFlag)
  nRt = -nRt;

 return nRt;
}

int _tmain(int argc, _TCHAR* argv[])
{
 char* pzTest = NULL;
 char* pzTest1 = "1234";
 char* pzTest2 = "-1234";
 char* pzTest22 = "+1234";
 char* pzTest3 = "12 34";
 char* pzTest4 = "a1234";
 char* pzTest5 = "123a4";
 char* pzTest6 = "123-4";
 //cout<<pzTest<<" atoi:"<<atoi(pzTest)<<endl;//!!!!!!this call will lead to died;
 cout<<pzTest1<<" atoi:"<<atoi(pzTest1)<<endl;
 cout<<pzTest2<<" atoi:"<<atoi(pzTest2)<<endl;
 cout<<pzTest22<<" atoi:"<<atoi(pzTest22)<<endl;
 cout<<pzTest3<<" atoi:"<<atoi(pzTest3)<<endl;
 cout<<pzTest4<<" atoi:"<<atoi(pzTest4)<<endl;
 cout<<pzTest5<<" atoi:"<<atoi(pzTest5)<<endl;
 cout<<pzTest6<<" atoi:"<<atoi(pzTest6)<<endl;

 cout<<" ---------my atoi -----"<<endl;
 cout<<"pzTest"<<" my_atoi:"<<my_atoi(pzTest)<<endl;
 cout<<pzTest1<<" my_atoi:"<<my_atoi(pzTest1)<<endl;
 cout<<pzTest2<<" my_atoi:"<<my_atoi(pzTest2)<<endl;
 cout<<pzTest22<<" my_atoi:"<<my_atoi(pzTest22)<<endl;
 cout<<pzTest3<<" my_atoi:"<<my_atoi(pzTest3)<<endl;
 cout<<pzTest4<<" my_atoi:"<<my_atoi(pzTest4)<<endl;
 cout<<pzTest5<<" my_atoi:"<<my_atoi(pzTest5)<<endl;
 cout<<pzTest6<<" my_atoi:"<<my_atoi(pzTest6)<<endl;
 return 0;
}

result:
1234 atoi:1234
-1234 atoi:-1234
+1234 atoi:1234
12 34 atoi:12
a1234 atoi:0
123a4 atoi:123
123-4 atoi:123
 ---------my atoi --
pzTest my_atoi:0
1234 my_atoi:1234
-1234 my_atoi:-1234
+1234 my_atoi:1234
12 34 my_atoi:12
a1234 my_atoi:0
123a4 my_atoi:123
123-4 my_atoi:123

/*---------------------------------------------------------------------------
  *   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);
}

#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   */
}


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值