现在的位置: 首页 > 综合 > 正文

atoi实现_字符串中含有字母_处理的不好哎

2013年08月09日 ⁄ 综合 ⁄ 共 746字 ⁄ 字号 评论关闭
#include <iostream>
using namespace std;

int isspace(int x)
{
    if(x==' '||x=='/t'||x=='/n'||x=='/f'||x=='/b'||x=='/r')
	{
		return 1;
	}
    else 
	{
		return 0;
	}
}

int isdigit(int x)
{
    if(x<='9'&&x>='0')         
	{
		return 1; 
	}
    else 
	{
		return 0;
	}
}

int atoi(const char *nptr)
{
        int c;              /* current char */
        int total;         /* current total */
        int sign;           /* if '-', then negative, otherwise positive */
        /* skip whitespace */
        while ( isspace((int)(unsigned char)*nptr) )
		{
			++nptr;
		}
        c = (int)(unsigned char)*nptr++;
        sign = c;           /* save sign indication */
        if (c == '-' || c == '+')
		{
			c = (int)(unsigned char)*nptr++;    /* skip sign */
		}
        total = 0;
        while (isdigit(c)) 
		{
            total = 10 * total + (c - '0');     /* accumulate digit */
            c = (int)(unsigned char)*nptr++;    /* get next char */
        }
        if (sign == '-')
		{
            return -total;
		}
        else
		{
			return total;   /* return result, negated if necessary */
		}

}
 
void main()
{
	const char *str = "12df3";
	cout << atoi(str) << endl;
}

抱歉!评论已关闭.