字符串转换成整数 c++

字符串转换成整数 c++

注:主函数为测试代码片段;核心代码参考StrToInt函数的定义。

需要考虑的细节:1)“+”,“-”; 2)非法输入:空指针;3)特殊字符;4)溢出。

#include <iostream>

int StrToInt(const char* str);

int main()
{

	std::cout << "\"\"" << "\t\t\t" << StrToInt("") << std::endl;
	std::cout << "\"1\"\t\t\t" << StrToInt("1") << std::endl;
	std::cout << "\"+1\"\t\t\t" << StrToInt("+1") << std::endl;
	std::cout << "\"-1\"\t\t\t" << StrToInt("-1") << std::endl;
	std::cout << "\"123\"\t\t\t" << StrToInt("123") << std::endl;
	std::cout << "\"-123\"\t\t\t" << StrToInt("-123") << std::endl;
	std::cout << "\"010\"\t\t\t" << StrToInt("010") << std::endl;
	std::cout << "\"+00131204\"\t\t" << StrToInt("+00131204") << std::endl;
	std::cout << "\"-01324000\"\t\t" << StrToInt("-01324000") << std::endl;
	std::cout << "\"2147483647\"\t\t" << StrToInt("2147483647") << std::endl;
	std::cout << "\"-2147483647\"\t\t" << StrToInt("-2147483647") << std::endl;
	std::cout << "\"-2147483648\"\t\t" << StrToInt("-2147483648") << std::endl;
	std::cout << "\"2147483648\"\t\t" << StrToInt("2147483648") << std::endl;
	std::cout << "\"-2147483649\"\t\t" << StrToInt("-2147483649") << std::endl;
	std::cout << "\"abc\"" << "\t\t\t" << StrToInt("abc") << std::endl;
	std::cout << "\"-abc\"" << "\t\t\t" << StrToInt("-abc") << std::endl;
	std::cout << "\"1a\"" << "\t\t\t" << StrToInt("1a") << std::endl;
	std::cout << "\"23a8f\"" << "\t\t\t" << StrToInt("23a8f") << std::endl;
	std::cout << "\"-3924x8fc\"" << "\t\t" << StrToInt("-3924x8fc") << std::endl;
	std::cout << "\"   321\"" << "\t\t\t" << StrToInt("   321") << std::endl;
	std::cout << "\"   -321\"" << "\t\t\t" << StrToInt("   -321") << std::endl;
	std::cout << "\"123   456\"" << "\t\t" << StrToInt("123   456") << std::endl;
	std::cout << "\"123   \"" << "\t\t\t" << StrToInt("123   ") << std::endl;
	std::cout << "\"   - 321\"" << "\t\t" << StrToInt("   - 321") << std::endl;
	std::cout << "\"   +4488\"" << "\t\t" << StrToInt("   +4488") << std::endl;
	std::cout << "\"   + 413\"" << "\t\t" << StrToInt("   + 413") << std::endl;
	std::cout << "\" ++c\"" << "\t\t\t" << StrToInt(" ++c") << std::endl;
	std::cout << "\"++1\"" << "\t\t\t" << StrToInt("++1") << std::endl;
	std::cout << "\"--2\"" << "\t\t\t" << StrToInt("--2") << std::endl;
	std::cout << "\" -2\"" << "\t\t\t" << StrToInt(" -2") << std::endl;
	
	return 0;
}

int StrToInt(const char* str)
{
	int result = 0;
	char signal = '+';
	int cur;
	// 判断是否为空指针
	if ( !str )
		return 0;
		
	// 忽略空白
	while ( *str == ' ' || *str == '\t' )
		str++;

	// 判断正负
	if ( *str == '+' )
		str++;
	else if(*str == '-' )
	{
		signal = '-';
		str++;
	}

	while ( *str != '\0' )
	{
		if ( *str >= '0' && *str <= '9' )
		{
			cur = *str - '0';
			// 判断是否溢出
			if ((signal == '+') && ((result > INT_MAX / 10) || ((result == INT_MAX/10)&&(cur >= INT_MAX % 10))))
				return INT_MAX;
			if ((signal == '-') && ((result < INT_MIN / 10) || ((-result == INT_MIN / 10) && (-cur <= INT_MIN % 10))) )
				return INT_MIN;
			result = result * 10 + cur;
			str++;
		}
		else if ( *str == ' ' || *str == '\t' )
			str++;
		else
			return (signal == '+') ? result : -result;
	}
	return (signal == '+') ? result : -result;
}

参考:
字符串转换为整数
C++中枚举enum详解
C++中static的作用
C++的前置声明

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值