最完美的自定义atoi

        最完美的atoi,与内置版本行为完全一致,如发现BUG,请邮件至tinycoco@126.com

        转换规则: 跳过原串前面的所有空字符,从第一个非空字符开始转换,第一个非空字符为’-‘表示负数,’+’忽略,在转换过程中遇到非数字就立即终止转换。

/* 函数实现 */
int my_atoi(const char *str)
{
	assert(str);
	
	int i = 0;/* index */
	int total = 0;/* result */
	int tmp_total = 0;/* temp result */
	int is_neg = 0;// 默认为正数
	const int minest = -2147483648;//负数溢出时统一设为最小值
	const int maxest = 2147483647;//正数溢出时统一设为最大值
	
	/*while (str[i] == ' '
		|| str[i] == '\t'
		|| str[i] == '\r'
		|| str[i] == '\n'
		|| str[i] == '\v'
		|| str[i] == '\f')// 跳过字符串开头的所有空字符
		++i;*/
	/* 1、跳过开头所有空字符 */
	while (isspace(str[i]))// 跳过字符串开头的所有空字符(等价于上面的长判断)
		++i;
		
	/* 2、处理符号位 */
	if ('-' == str[i])// 处理负数
	{
		is_neg = 1;
		++i;
	}
	else if ('+' == str[i])// 跳过多余的+号
		++i;
	
	/* 3、连续转换 */
	while (str[i] >= '0' && str[i] <= '9')
	{
		tmp_total = total;
		total = total*10 + (str[i++]-'0');
		
		/* 4、溢出处理 */
		if (total < tmp_total)/* overflow */
		{
			return (1==is_neg)?(minest):(maxest);
		}
	}
	
	/* 5、结果返回 */
	return (1==is_neg)?(-total):(total);
}

重在测试:

/* 测试函数 */
void test_my_atoi(void)
{
	printf("Begin test my_atoi...\n");
	//printf("atoi(123) = %d\n", atoi("123"));
	//printf("my_atoi(123) = %d\n", my_atoi("123"));
	assert(atoi("123") == my_atoi("123"));//测试方法
	
	//printf("atoi(  123) = %d\n", atoi("  123"));
	//printf("my_atoi(  123) = %d\n", my_atoi("  123"));
	assert(atoi("  123") == my_atoi("  123"));
	
	//printf("atoi(	123) = %d\n", atoi("	123"));
	//printf("my_atoi(	123) = %d\n", my_atoi("	123"));
	assert(atoi("	123") == my_atoi("	123"));
	
	//printf("atoi(a123) = %d\n", atoi("a123"));
	//printf("my_atoi(a123) = %d\n", my_atoi("a123"));
	assert(atoi("a123") == my_atoi("a123"));
	
	//printf("atoi(  12a3) = %d\n", atoi("  12a3"));
	//printf("my_atoi(  12a3) = %d\n", my_atoi("  12a3"));
	assert(atoi("  12a3") == my_atoi("  12a3"));
	
	//printf("atoi(  123bb) = %d\n", atoi("  123bb"));
	//printf("my_atoi(  123bb) = %d\n", my_atoi("  123bb"));
	assert(atoi("  123bb") == my_atoi("  123bb"));
	
	//printf("atoi( ,-12) = %d\n", atoi(" ,-12"));
	//printf("my_atoi( ,-12) = %d\n", my_atoi(" ,-12"));
	assert(atoi(" ,-12") == my_atoi(" ,-12"));
	
	//printf("atoi( \t-,12) = %d\n", atoi(" \t-,12"));
	//printf("my_atoi( \t-,12) = %d\n", my_atoi(" \t-,12"));
	assert(atoi(" \t-,12") == my_atoi(" \t-,12"));
	
	//printf("atoi(\"\r\t\v -3\") = %d\n", atoi("\r\t\v -3"));
	//printf("my_atoi(\"\r\t\v -3\") = %d\n", my_atoi("\r\t\v -3"));
	assert(atoi("\r\t\v -3") == my_atoi("\r\t\v -3"));

	//printf("atoi(+2147483646) = %d\n", atoi("+2147483646"));
	//printf("my_atoi(+2147483646) = %d\n", my_atoi("+2147483646"));
	assert(atoi("+2147483646") == my_atoi("+2147483646"));
	
	//printf("atoi(+ 2147483646) = %d\n", atoi("+ 2147483646"));
	//printf("my_atoi(+ 2147483646) = %d\n", my_atoi("+ 2147483646"));
	assert(atoi("+ 2147483646") == my_atoi("+ 2147483646"));
	
	//printf("atoi(2147483646) = %d\n", atoi("2147483646"));
	//printf("my_atoi(2147483646) = %d\n", my_atoi("2147483646"));
	assert(atoi("2147483646") == my_atoi("2147483646"));
	
	//printf("atoi(2147483647) = %d\n", atoi("2147483647"));
	//printf("my_atoi(2147483647) = %d\n", my_atoi("2147483647"));
	assert(atoi("2147483647") == my_atoi("2147483647"));
	
	//printf("atoi(2147483648) = %d\n", atoi("2147483648"));
	//printf("my_atoi(2147483648) = %d\n", my_atoi("2147483648"));
	assert(atoi("2147483648") == my_atoi("2147483648"));
	
	//printf("atoi(-2147483647) = %d\n", atoi("-2147483647"));
	//printf("my_atoi(-2147483647) = %d\n", my_atoi("-2147483647"));
	assert(atoi("-2147483647") == my_atoi("-2147483647"));
	
	//printf("atoi(-2147483648) = %d\n", atoi("-2147483648"));
	//printf("my_atoi(-2147483648) = %d\n", my_atoi("-2147483648"));
	assert(atoi("-2147483648") == my_atoi("-2147483648"));
	
	//printf("atoi(-2147483649) = %d\n", atoi("-2147483649"));
	//printf("my_atoi(-2147483649) = %d\n", my_atoi("-2147483649"));
	assert(atoi("-2147483649") == my_atoi("-2147483649"));
	printf("End test my_atoi, successfully!\n");
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值