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;
}
不使用库函数将字符串转化为数字 实现
最新推荐文章于 2020-06-11 10:37:27 发布
本文介绍了一个实用的C语言函数intstr2int,该函数能够将字符串形式的数字转换为整型数值。通过逐个解析字符串中的字符,并考虑符号位的影响,此函数实现了从字符串到整数的有效转换。

892

被折叠的 条评论
为什么被折叠?



