首页 > 代码库 > 大数加减法总结

大数加减法总结

大数加减法总结(包括整数或者负数):

1、先解决不带符号的数的加减法

2、根据加数或者减数的符号位判断该选择加法还是减法计算,并且赋予结果对应的符号

需要注意的是:不带符号的减法产生的结果可能高位为‘0’,要进行处理。

string bigNumberMinusWithoutSign(const char* num1, const char *num2){
	string res = "";
	
	bool flag1 = false; // 正负标志位
	int flag = 0;//退位标志

	const char* p1, *p2;
	p1 = num1, p2 = num2;
	int len1 = strlen (p1);
	int len2 = strlen (p2);
	int index1 = len1 - 1;
	int index2 = len2 - 1;


	if(len1 < len2 ||len1==len2&&strcmp (num1, num2) < 0){
		flag1 = true;
		p1 = num2;
		p2 = num1;
		index1 = len2 - 1;
		index2 = len1 - 1;
	}
	// 处理减法
	while(index1 >= 0 && index2 >= 0){

		if(p1[index1]+flag > p2[index2]){
			res = char(p1[index1]+flag-p2[index2]+'0') + res;
			flag = 0;
		}
		else if(p1[index1]+flag == p2[index2]){
			res = char('0') + res;
			flag = 0;
		}
		else{
			res = char(p1[index1]+flag+10-p2[index2]+'0') + res;
			flag = -1;
		}
		--index1, --index2;
	}

	// 处理较大数的剩余位
	while(index1 >= 0){
		if(flag + p1[index1] < '0'){
			res = char(flag + p1[index1] + 10) + res;
			flag = -1;
		}
		else{
			res =char(flag + p1[index1]) + res;
			flag = 0;
		}
		--index1;
	}

	int zeroCount = 0;
	for(int k = 0; k < res.length (); ++k){
		if(res[k] == '0')
			++zeroCount;
		else
			break;
	}
	res.erase (0, zeroCount);
	if(res.length () == 0)
		return res;
	
	if(flag1 == true)
		res = char('-') + res;
	return res;
}

string bigNumberAddWithoutSign(const char *num1, const char *num2)
{
	string res = "";
	if(!num1 || !num2)
		return res;

	const char *p1, *p2;

	p1 = num1, p2 = num2;
	int len1 = strlen (p1);
	int len2 = strlen (p2);
	int index1 = len1 - 1;
	int index2 = len2 - 1;

	if(len1 < len2){
		p1 = num2;
		p2 = num1;
		index1 = len2 - 1;
		index2 = len1 - 1;
	}

	int flag = 0; //进位标志位
	int temp;
	while(index1 >=0 && index2 >=0){
		temp = flag + p1[index1] + p2[index2] - 2*'0';
		if(temp >= 10)
			flag = 1;
		else
			flag = 0;
		res = char(temp % 10 + '0') + res;
		--index1, --index2;
	}

	while(index1 >= 0){
		temp = flag + p1[index1] - '0';
		if(temp >= 10)
			flag = 1;
		else
			flag = 0;
		res = char(temp % 10 + '0') + res;
		--index1;
	}
	return res;
}

string bigNumberAdd(const char *num1, const char *num2)
{
	bool sign1 = true;
	bool sign2 = true;
	
	const char *p1 = num1;
	const char *p2 = num2;

	if(*p1 == '-'){
		sign1 = false;
		++p1;
	}
	else if(*p1 == '+')
		++p1;

	if(*p2 == '-'){
		sign2 = false;
		++p2;
	}
	else if(*p2 == '+')
		++p2;
	

	string res;
	if(sign1 && sign2)
		return bigNumberAddWithoutSign (p1, p2);
	else if(sign1 && !sign2)
		return bigNumberMinusWithoutSign (p1, p2);
	else if(!sign1 && sign2)
		return bigNumberMinusWithoutSign (p2, p1);
	else{
		res = bigNumberAddWithoutSign (p1, p2);
		res = char('-') + res;
		return res;
	}
}

string bigNumberMinus(const char *num1, const char *num2)
{
	bool sign1 = true;
	bool sign2 = true;
	const char *p1 = num1;
	const char *p2 = num2;

	if(*p1 == '-'){
		sign1 = false;
		++p1;
	}
	else if(*p1 == '+')
		++p1;

	if(*p2 == '-'){
		sign2 = false;
		++p2;
	}
	else if(*p2 == '+')
		++p2;

	if(sign1 && sign2)
		return bigNumberMinusWithoutSign (p1, p2);
	else if(sign1 && !sign2)
		return bigNumberAddWithoutSign (p1, p2);
	else if(!sign1 && sign2){
		string res = bigNumberAddWithoutSign (p1, p2);
		res = char('-') + res;
		return res;
	}
	else
		return bigNumberMinusWithoutSign (p2, p1);
}




大数加减法总结