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
    评论
好的,以下是一个简单的计算器软件的C语言代码,实现了上述要求: ```c #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> #define MAX_LEN 1000 int priority(char op); // 定义运算符优先级函数 int hex_to_dec(char c); // 定义16进制数转10进制函数 char dec_to_hex(int n); // 定义10进制数转16进制函数 void reverse(char s[]); // 定义字符串反转函数 void big_add(char num1[], char num2[], char result[]); // 定义大数相加函数 void big_sub(char num1[], char num2[], char result[]); // 定义大数相减函数 int eval(char exp[]); // 定义表达式求值函数 int main() { char op, num1[MAX_LEN], num2[MAX_LEN], result[MAX_LEN], exp[MAX_LEN]; int num, base; printf("选择要进行的操作:\n"); printf("1. 大数相加\n"); printf("2. 大数相减\n"); printf("3. 进制转换\n"); printf("4. 判断括号是否匹配并求值\n"); scanf("%d", &num); switch (num) { case 1: printf("请输入两个大数:\n"); scanf("%s %s", num1, num2); big_add(num1, num2, result); printf("结果为:%s\n", result); break; case 2: printf("请输入两个大数:\n"); scanf("%s %s", num1, num2); big_sub(num1, num2, result); printf("结果为:%s\n", result); break; case 3: printf("请输入要转换的数字和进制:\n"); scanf("%s %d", num1, &base); if (base == 10) { printf("2进制:%s\n", itoa(atoi(num1), num2, 2)); printf("8进制:%s\n", itoa(atoi(num1), num2, 8)); printf("16进制:%s\n", itoa(atoi(num1), num2, 16)); } else if (base == 2 || base == 8 || base == 16) { int dec = 0; for (int i = 0; num1[i]; i++) { dec = dec * base + hex_to_dec(num1[i]); } printf("10进制:%d\n", dec); } else { printf("不支持该进制!\n"); } break; case 4: printf("请输入要判断的表达式:\n"); scanf("%s", exp); int res = eval(exp); if (res == -1) { printf("表达式中括号不匹配!\n"); } else { printf("结果为:%d\n", res); } break; default: printf("无效的选择!\n"); break; } return 0; } int priority(char op) { switch (op) { case '+': case '-': return 1; case '*': case '/': return 2; case '^': return 3; default: return 0; } } int hex_to_dec(char c) { if (isdigit(c)) { return c - '0'; } else { return c - 'A' + 10; } } char dec_to_hex(int n) { if (n < 10) { return n + '0'; } else { return n - 10 + 'A'; } } void reverse(char s[]) { for (int i = 0, j = strlen(s) - 1; i < j; i++, j--) { char tmp = s[i]; s[i] = s[j]; s[j] = tmp; } } void big_add(char num1[], char num2[], char result[]) { int carry = 0, i = 0, j = 0; while (num1[i] || num2[j] || carry) { int sum = carry; if (num1[i]) { sum += num1[i] - '0'; i++; } if (num2[j]) { sum += num2[j] - '0'; j++; } carry = sum / 10; result[strlen(result)] = sum % 10 + '0'; } reverse(result); } void big_sub(char num1[], char num2[], char result[]) { int borrow = 0, i = strlen(num1) - 1, j = strlen(num2) - 1; while (i >= 0 || j >= 0) { int diff = borrow; if (i >= 0) { diff += num1[i] - '0'; i--; } if (j >= 0) { diff -= num2[j] - '0'; j--; } if (diff < 0) { diff += 10; borrow = -1; } else { borrow = 0; } result[strlen(result)] = diff + '0'; } while (result[0] == '0' && strlen(result) > 1) { strcpy(result, result + 1); } } int eval(char exp[]) { char stack[MAX_LEN]; int top = -1, res = 0; for (int i = 0; exp[i]; i++) { if (isdigit(exp[i])) { int num = 0; while (isdigit(exp[i])) { num = num * 10 + exp[i] - '0'; i++; } i--; stack[++top] = num; } else if (exp[i] == '(') { stack[++top] = '('; } else if (exp[i] == ')') { while (stack[top] != '(') { int num2 = stack[top--]; char op = stack[top--]; int num1 = stack[top--]; if (op == '+') { stack[++top] = num1 + num2; } else if (op == '-') { stack[++top] = num1 - num2; } else if (op == '*') { stack[++top] = num1 * num2; } else if (op == '/') { stack[++top] = num1 / num2; } else if (op == '^') { int res = 1; for (int i = 0; i < num2; i++) { res *= num1; } stack[++top] = res; } } top--; } else if (exp[i] == '+' || exp[i] == '-' || exp[i] == '*' || exp[i] == '/' || exp[i] == '^') { while (top >= 0 && priority(stack[top]) >= priority(exp[i])) { int num2 = stack[top--]; char op = stack[top--]; int num1 = stack[top--]; if (op == '+') { stack[++top] = num1 + num2; } else if (op == '-') { stack[++top] = num1 - num2; } else if (op == '*') { stack[++top] = num1 * num2; } else if (op == '/') { stack[++top] = num1 / num2; } else if (op == '^') { int res = 1; for (int i = 0; i < num2; i++) { res *= num1; } stack[++top] = res; } } stack[++top] = exp[i]; } } while (top >= 0) { int num2 = stack[top--]; char op = stack[top--]; int num1 = stack[top--]; if (op == '+') { res = num1 + num2; } else if (op == '-') { res = num1 - num2; } else if (op == '*') { res = num1 * num2; } else if (op == '/') { res = num1 / num2; } else if (op == '^') { res = 1; for (int i = 0; i < num2; i++) { res *= num1; } } stack[++top] = res; } return top == 0 ? stack[top] : -1; } ``` 代码中实现了大数相加、大数相减、进制转换和表达式求值等功能。其中,大数相加和大数相减函数使用了字符串表示数字,进制转换函数支持2、8、10、16进制之间的相互转换,表达式求值函数支持加、减、乘、除、幂和括号运算。 需要注意的是,代码中的表达式求值函数只支持整数运算,不支持浮点数运算。另外,在进行大数相减操作时,需要保证被减数大于减数,否则函数可能会出现错误。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值