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

自己写atoi

2018年01月17日 ⁄ 综合 ⁄ 共 383字 ⁄ 字号 评论关闭
#include <iostream>
using namespace std;

const int maxv = (1 << 31) - 1;
const int minv = (1 << 31) ;

int strToInt(const char *str)
{
	if(!str)
		exit(-1); 
	char *p = const_cast<char*>(str);
	int re = 0;
	bool nagetive = false;
	if(*p == '-' || '+')
	{
		if(*p == '-')
			nagetive = true;
		p++;	
	}
		
	while(*p)
	{
		re *= 10;
		re += (*p++ - '0');
	}
	return nagetive ? (0 - re) : re;
}

int main(void)
{
//	cout << maxv << endl << minv << endl;
	char a[] = "-123456";
	cout << strToInt(a) << endl;
	
	return 0;
}

抱歉!评论已关闭.