大数运算(加减乘)

废话不多说直接上代码,我觉得看代码应该都能看懂!

//利用数组实现大数相加
void bdadd(string strnum1, string strnum2, string& restult) {
	int len1 = strnum1.length();
	int len2 = strnum2.length();
	int len3 = (len1 > len2 ? len1 : len2);    //保存相加结果的位数,要多一位出来

	int* ptnum1 = new int[len3];
	int* ptnum2 = new int[len3];
	int* ptrlt = new int[len3 + 1];
	int i = 0;
	for (i = 0; i < len3; i++) {
		ptnum1[i] = 0;
		ptnum2[i] = 0;
	}

	for (i = 0; i < len3 + 1; i++) {
		ptrlt[i] = 0;
	}

	for (i = 0; i < len1; i++) {
		if('0' >= strnum1[len1 - i - 1] && strnum1[len1 - i - 1] <= '9')
			ptnum1[len3 - i - 1] = strnum1[len1 - i - 1] - '0';
		else {
			cerr << "数据有误!";
			exit(-1);
		}
	}

	for (i = 0; i < len2; i++) {
		if('0' <= strnum2[len2 - i - 1] && strnum2[len2 - i - 1] <= '9')
			ptnum2[len3 - i - 1] = strnum2[len2 - i - 1] - '0';
		else {
			cerr << "数据有误!";
			exit(-1);
		}
	}

	for (i = len3; i > 0; i--) {
		ptrlt[i] += ptnum1[i - 1] + ptnum2[i - 1];
		if (ptrlt[i] >= 10) {
			ptrlt[i] -= 10;
			++ptrlt[i - 1];
		}
	}

	if (ptrlt[0] > 0)
		restult += to_string(ptrlt[0]);
	for (int i = 1; i < len3 + 1; i++) {
		restult += to_string(ptrlt[i]);
	}

	delete[]ptnum1;
	delete[]ptnum2;
	delete[]ptrlt;
}

//利用数组实现大数相减
//实时证明,大数据的相减用数组更好实现
void BDMinus(string strNum1, string strNum2, string& strRlt) {
	int len1;
	int len2;
	int lenMax;
	bool isRltMinus = false;
	if (strNum1.length() < strNum2.length()) {
		isRltMinus = true;
		string strTmp = strNum1;
		strNum1 = strNum2;
		strNum2 = strTmp;
	}

	else if (strNum1.length() == strNum2.length()) {     //如果strNum1的长度与strNum2的长度相等,那么循环判断是否strNum1<strNum2
		for (int i = 0; i < strNum1.length(); i++) {
			if (strNum1[i] < strNum2[i]) {
				isRltMinus = true;
				string strTmp = strNum1;
				strNum1 = strNum2;
				strNum2 = strTmp;
				break;
			}
		}
	}
	else{

	}

	len1 = strNum1.length();
	len2 = strNum2.length();
	lenMax = len1;

	int* ptNum1 = new int[lenMax];
	int* ptNum2 = new int[lenMax];
	int* ptRlt = new int[lenMax];
	int i = 0;
	for (i = 0; i < lenMax; i++) {
		ptNum1[i] = 0;
		ptNum2[i] = 0;
		ptRlt[i] = 0;
	}

	for (i = 0; i < len1; i++) {
		if('0' <= strNum1[len1 - i - 1] && strNum1[len1 - i - 1] <= '9')
			ptNum1[lenMax - i - 1] = strNum1[len1 - i - 1] - '0';
		else {
			cerr << "数据有误!";
			exit(-1);
		}
	}

	for (i = 0; i < len2; i++) {
		if('0' <= strNum2[len2 - i - 1] && strNum2[len2 - i - 1] <= '9')
			ptNum2[lenMax - i - 1] = strNum2[len2 - i - 1] - '0';
		else {
			cerr << "数据有误!";
			exit(-1);
		}
	}

	for (i = lenMax - 1; i >= 0; i--) {
		ptRlt[i] += (ptNum1[i] - ptNum2[i]);
		if (ptRlt[i] < 0) {
			ptRlt[i] += 10;
			if(i - 1 >= 0)
				--ptRlt[i - 1];
		}
	}

	if (isRltMinus)
		strRlt += "-";
	//将结果里面的前面如果是0那就毙掉
	int count = 0;
	while (ptRlt[count] == 0) {
		count++;
	}

	for (int i = count; i < lenMax; i++) {
		strRlt += to_string(ptRlt[i]);
	}

	delete[]ptNum1;
	delete[]ptNum2;
	delete[]ptRlt;
}



//大数据的乘法计算
void BDMul(string strNum1, string strNum2, string& strRlt) {
	//采取累乘的方式然后再统一进位
	bool isNum1Minus = false;
	bool isNum2Minus = false;
	bool isRktMinus = false;
	//首先判断strNum1与strNum2会不会在首字母中出现‘+’或者‘-’;
	//如果出现正负好,要将正负号去掉,不做运算处理
	if (strNum1[0] == '-' || strNum1[0] == '+') {
		if (strNum1[0] == '-')
			isNum1Minus = true;
		strNum1 = strNum1.substr(1, strNum1.length());
	}
	
	if (strNum2[0] == '-' || strNum2[0] == '+') {
		if (strNum2[0] == '-')
			isNum2Minus = true;
		strNum2 = strNum2.substr(1, strNum2.length());
	}

	//如果isNum1Minus != isNum2Minus,说明两个因数一正一负
	//结果就是负号
	if (isNum1Minus != isNum2Minus)
		isRktMinus = true;


	int len1 = strNum1.length();
	int len2 = strNum2.length();
	int lenRlt = len1 + len2;       //正常len1 * len2 - 1,但是多一位用于预留符号位
	
	int* ptNum1 = new int[len1];
	int* ptNum2 = new int[len2];
	int* ptRlt = new int[lenRlt]();

	for (int i = 0; i < len1; i++) {
		if('0' <= ptNum1[i] && ptNum1[i] <= '9')
			ptNum1[i] = strNum1[i] - '0';
		else {
			cout << "数据有误!\n";
			exit(-1);
		}
	}
		for (int i = 0; i < len2; i++) {
			if('0' <= ptNum2[i] && ptNum2[i] <= '9')
				ptNum2[i] = strNum2[i] - '0';
			else {
				cout << "数据有误!\n";
				exit(-1);
			}
	}

	//先累加,然后统一移位
	for (int i = 0; i < len1; i++)
		for (int j = 0; j < len2; j++)
			ptRlt[i + j + 1] += ptNum1[i]* ptNum2[j];                       //之所以加1时因为第一位是符号位

	//统一处理进位
	for (int i = lenRlt - 1; i > 0; i--) {
		if (ptRlt[i] >= 10) {
			ptRlt[i - 1] += ptRlt[i] / 10;
			ptRlt[i] %= 10;
		}
	}

	int countZero = 0;
	while (ptRlt[countZero] == 0)
		countZero++;

	if (countZero == lenRlt)
		strRlt = "0";
	else {
		if (isRktMinus)
			strRlt = "-";
		for (int i = countZero; i < lenRlt; i++)
			strRlt += to_string(ptRlt[i]);
	}

	delete[]ptNum1;
	delete[]ptNum2;
	delete[]ptRlt;
}



int main() {

	//大数的加法运算,利用栈实现
	string strNum1;
	string strNum2;
	string strRlt;
	getline(cin, strNum1);
	getline(cin, strNum2);
	/*stack<int>stkRlt;
	BDSub(strNum1, strNum2, stkRlt);
	while (stkRlt.size() > 0) {
		cout << stkRlt.top();
		stkRlt.pop();
	}*/
	BDMul(strNum1, strNum2, strRlt);
	cout << strRlt;

	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值