C++ atoi函数的实现

#include
#include
using namespace std;
#define INT_MAX ((int)0x7FFFFFFF)
#define INT_MIN ((int)0x80000000)
int Myatoi(const char *str)
{
	int flag=1;
	char *p=const_cast (str);
	assert(str);
	long long int ret=0;
	while(*p==' ')
		p++;
	if(*p=='\0')
		return 0;
	if(*p=='+')
		p++;
	else if(*p=='-')
	{
		flag=-1;
		p++;
	}
	while(*p>='0'&&*p<='9')
	{
		ret=ret*10+flag*(*p-'0');
		if(ret>=INT_MAX)
			return INT_MAX;
		else if(ret<=INT_MIN)
			return INT_MIN;
		p++; 
	}
	return (int)ret;
}
int main()
{
//	int ret=Myatoi("21474836470666");
	int ret=Myatoi("-2147483647234");
	cout<

 

你可能感兴趣的:(C++ atoi函数的实现)