C语言实现 atoi功能包括 (1)判断溢出输出最大值最小值(2)智能匹配字符 (3)16进制和8进制转化 (4)正负数

#define  _CRT_SECURE_NO_WARNINGS 
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <assert.h>
#include <limits.h>

//使用查看当前结果是否为负数的方法可以有效的判断是否溢出 缺点:无法判断所有情况可能出错比如17777777777 当数字加到的时候它距离溢出只需再加个7就溢出了,但是17777777777返回一个正数。
int isOverflow(int tag, int *sum)
{
	if (*sum < 0)
	{
		if (tag == 0) { *sum = INT_MAX;}
		else if (tag == 1) { *sum = INT_MIN;}
		return 1;
	}
	else
		return 0;
}

//将结果值置为INT_MAX或者INT_MIN
void ChangetoMaxOrMin(int tag, int **sum)
{
	if (tag == 0)
		**sum = INT_MAX;
	else if (tag == 1)
		**sum = INT_MIN;
}

//判断十进制数字,如果是同级整数。可能出现意外。ex:4294967297 本应该返回INT_MAX,但是返回了1 这个函数就是为了解决这种情况。
int is_D_Overflow_MAX_Bit(char *aIntstr, int tag, int *sum)
{
	//2147483647是INT的最大值
	if (*aIntstr > '2')
	{
		ChangetoMaxOrMin(tag, &sum);//大于0小于0的处理
		return 0;
	}
	else
	{
		aIntstr++;
	}
	if (*aIntstr > '1')
	{
		ChangetoMaxOrMin(tag, &sum);//大于0小于0的处理
		return 0;
	}
	else
	{
		aIntstr++;
	}
	if (*aIntstr > '4')
	{
		ChangetoMaxOrMin(tag, &sum);//大于0小于0的处理
		return 0;
	}
	else
	{
		aIntstr++;
	}
	if (*aIntstr > '7')
	{
		ChangetoMaxOrMin(tag, &sum);//大于0小于0的处理
		return 0;
	}
	else
	{
		aIntstr++;
	}
	if (*aIntstr > '4')
	{
		ChangetoMaxOrMin(tag, &sum);//大于0小于0的处理
		return 0;
	}
	else
	{
		aIntstr++;
	}
	if (*aIntstr > '8')
	{
		ChangetoMaxOrMin(tag, &sum);//大于0小于0的处理
		return 0;
	}
	else
	{
		aIntstr++;
	}
	if (*aIntstr > '3')
	{
		ChangetoMaxOrMin(tag, &sum);//大于0小于0的处理
		return 0;
	}
	else
	{
		aIntstr++;
	}
	if (*aIntstr > '6')
	{
		ChangetoMaxOrMin(tag, &sum);//大于0小于0的处理
		return 0;
	}
	else
	{
		aIntstr++;
	}
	if (*aIntstr > '4')
	{
		ChangetoMaxOrMin(tag, &sum);//大于0小于0的处理
		return 0;
	}
	else
	{
		aIntstr++;
	}
	if (*aIntstr > '7')
	{
		ChangetoMaxOrMin(tag, &sum);//大于0小于0的处理
		return 0;
	}
	else
	{
		aIntstr++;
	}
	return 1;
}


//防止循环轮回为正的情况:但是只能防止比最大值高一个位数的数量级的轮回:::嗯还有待改进:::已经改进(通过判断10进制位数为INT_MAX位数可能轮回为1,用减法并判断不了是否越界)
int isOverBit(int tag,int *sum,int UsefulLen,int StandardMaxLen,char *aIntstr)
{
	//只有十进制有这个问题-轮回为正。
	if (UsefulLen == StandardMaxLen && UsefulLen == 10)//此种情况判断大于2147483647 且位数位10位的字符是否溢出 ex:4294967296 返回0
	{
		if(!is_D_Overflow_MAX_Bit(aIntstr, tag, sum)) return 0;
	}
	//如果字符串的有效长度大于指定长度直接返回最大最小的INT值
	if (UsefulLen > StandardMaxLen)
	{
		if (tag == 0)
			*sum = INT_MAX;
		else if (tag == 1)
			*sum = INT_MIN;
	}
	return 0;
}

//智能匹配 
int SmartMatch(char **aIntstr,int UserfulLen)
{
		if (**aIntstr == 'O' || **aIntstr == 'o')
		{
			printf("the %dth character you input is 'O' or 'o', Do you mean '0' in that position?\n", UserfulLen + 1);
			**aIntstr = '0';//直接将当前可能打错字符修改正确字符---用于判断是否溢出时候方便
			return 1;
		}
		else if (**aIntstr == 'I' || **aIntstr == 'i')
		{
			printf("the %dth character you input is 'I' or 'i', Do you mean '1' in that position?\n", UserfulLen + 1);
			**aIntstr = '1';//
			return 1;
		}
		else if(**aIntstr == 'q')
		{
			printf("the %dth character you input is 'q', Do you mean '1' in that position?\n", UserfulLen + 1);
			**aIntstr = '9';//
			return 1;
		}
		else if (**aIntstr == 'b')
		{
			printf("the %dth character you input is 'b', Do you mean '6' in that position?\n", UserfulLen + 1);
			**aIntstr = '6';//
			return 1;
		}
		else if (**aIntstr == 'l')
		{
			printf("the %dth character you input is 'l', Do you mean '1' in that position?\n", UserfulLen + 1);
			**aIntstr = '1';//
			return 1;
		}
	return 0;
}

int my_dtoi(char *aIntstr,int tag)
{
	int rst = 0;
	int UserfulLen = 0;
	char *p_IsOver = aIntstr;
	while (*aIntstr)
	{
		SmartMatch(&aIntstr,UserfulLen);
			//continue;
		if (isdigit(*aIntstr)) 
		{ 
			//rst = (rst * 10 + *aIntstr++ - '0'); //使用了long long int 更大的数值范围来判断是否超过INT_MAX INT_MIN
			rst = (rst * 10 + *aIntstr++ - '0');
		}
		else break;
		if (isOverflow(tag, &rst))break;
		UserfulLen++;
	}
	//是否超过位数
	isOverBit(tag, &rst, UserfulLen, 10, p_IsOver);
	return rst;
}

int my_otoi(char *aIntstr,int tag)
{
	int rst = 0;
	int UserfulLen = 0;
	char *p_IsOver = aIntstr;
	while (*aIntstr)
	{
		SmartMatch(&aIntstr, UserfulLen);
			//continue;
		if (*aIntstr <= '7'&&*aIntstr >= 8)rst = (rst * 8 + (*aIntstr++ - '0'));
		else break;
		if (isOverflow(tag, &rst))break;
		UserfulLen++;
	}
	isOverBit(tag, &rst, UserfulLen, 11,p_IsOver);
	return rst;
}

int my_htoi(char *aIntstr,int tag)
{
	int rst = 0;
	int UserfulLen = 0;
	char *p_IsOver = aIntstr;
	while (*aIntstr)
	{
		SmartMatch(&aIntstr, UserfulLen);
			//continue;
		if (isxdigit(*aIntstr))
		{
			if (isalpha(*aIntstr))
			{
				char ch = toupper(*aIntstr++);
				rst = (rst * 16 + (ch - 'A' + 10));
			}
			else
			{
				rst = (rst * 16 + (*aIntstr++ - '0'));
			}
		}
		else break;
		if (isOverflow(tag, &rst))break;
		UserfulLen++;
	}
	isOverBit(tag, &rst, UserfulLen, 8, p_IsOver);
	return rst;
}

int my_atoi(char *aIntstr)
{
	assert(aIntstr != NULL);
	int rst = 0;
	int ispositivenum = 0;//是否为正数-不加符号默认为正

	if (*aIntstr == '+') { aIntstr += 1; }
	else if (*aIntstr == '-') { aIntstr += 1; ispositivenum = 1; }

	//十六进制或者八进制
	if (*aIntstr == '0')
	{
		if (*(aIntstr + 1) == 'x') rst = my_htoi(aIntstr + 2, ispositivenum);
		else if (isdigit(*(aIntstr + 1))) rst = my_otoi(aIntstr + 1, ispositivenum);
	}

	//正常十进制
	else rst = my_dtoi(aIntstr, ispositivenum);


	if (!ispositivenum) return rst;
	else return -rst;
}

void main()
{
	char aIntstr[30] = { 0 };
	do {
		scanf("%s", aIntstr);
		int rst = my_atoi(aIntstr);
		printf("%d\n", rst);

	} while (strcmp(aIntstr, "#") != 0);
	printf("hello...\n");
	system("pause");
	return ;
}

测试结果:奇数行为输入,偶数行为输出。

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值