您的位置:首页 > 其它

不使用库函数将字符串转化为数字 实现

2012-05-12 19:03 393 查看
int str2int(const char *str)
{
	int temp = 0;
	const char *pstr = str;//ptr保存
	if (*str=='-'||*str=='+')
	{
		str++;
	}
	while (*str!=0)
	{
		if ((*str < '0') || (*str > '9'))//如果当前字符不是数字,则退出循环
		{
			break;
		}
		temp = temp*10 + (*str - '0');//如果当前字符数数字则计算数值
		str++;
	}
	if (*pstr =='-')//如果字符串是以"-"开头,则转换成其相反数
	{
		temp = -temp;
	}
	return temp;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: