#include <cctype>
int my_atoi(const char* p)
{
assert(p != NULL);
bool neg_flag = false;// 符号标记
int res = 0;// 结果
if(p[0] == '+' || p[0] == '-')
neg_flag = (*p++ == '-');
while(isdigit(*p))
res = res*10 + (*p++ - '0');
return neg_flag ? -res : res;
}
本文介绍了一个用C语言实现的atoi函数,该函数能够将字符串转换为整数,并正确处理正负号。通过使用assert宏确保输入指针不为空,利用isdigit函数检查字符是否为数字,从而完成字符串到整数的有效转换。
553

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



