大数加减法

完成以下类的实现,实现大数加减法

//大整数类型
class BigInt
{
public:
	BigInt(string str) :strDigit(str){}
private:
	string strDigit;   // 使用字符串存储大整数

	friend ostream& operator<<(ostream &out, const BigInt &src);
	friend BigInt operator+(const BigInt &lhs, const BigInt &rhs);
	friend BigInt operator-(const BigInt &lhs, const BigInt &rhs);
};
// 打印函数
ostream& operator<<(ostream &out, const BigInt &src)
{
	out << src.strDigit ;
	return out;
}
// 大数加法
BigInt operator+(const BigInt &lhs, const BigInt &rhs)
{
//1,从后往前遍历lhs和rhs,进行相加操作,相加的过程中考虑借位操作
	int size1 = lhs.strDigit.length();
	int size2 = rhs.strDigit.length();
	bool flag = false;//借位标志
	vector<int>NumVec;
	int i = size1 - 1, j = size2 - 1;
	int sum = 0;
	for (; i >= 0 && j >= 0; --i, --j)
	{
		int num1 = lhs.strDigit[i] - '0';
		int num2 = rhs.strDigit[j] - '0';
		if (flag)
		{
			sum = num1 + num2 + 1;
			flag = false;
		}
		else
		{
			sum = num1 + num2;
		}
		if (sum >= 10)
		{
			flag = true;
		}
		NumVec.push_back(sum%10);
	}
	//哪段数字长,考虑借位继续添加
	for (; i >= 0; --i)
	{
		int num = lhs.strDigit[i] - '0';
		if (flag)
		{
			sum = num + 1;
			flag = false;
		}
		else
		{
			sum = num;
		}
		if (sum >= 10)
		{
			flag = true;
		}
		NumVec.push_back(sum % 10);
	}
	for (; j >= 0; --j)
	{
		int num = rhs.strDigit[j] - '0';
		if (flag)
		{
			sum = num + 1;
			flag = false;
		}
		else
		{
			sum = num;
		}
		if (sum >= 10)
		{
			flag = true;
		}
		NumVec.push_back(sum % 10);
	}
	if (flag)
	{
		NumVec.push_back(1);
	}
	//得到相加的数字,存放到vector中
	//vector中的结果存放到string里面进行返回
	string str = "";
	for (int i = NumVec.size() - 1; i >= 0; --i)
	{
		str.push_back(NumVec[i]+'0');
	}
	return str;
}
// 大数减法
BigInt operator-(const BigInt &lhs, const BigInt &rhs)
{
	BigInt big(""), small("");
	int size1 = lhs.strDigit.length();
	int size2 = rhs.strDigit.length();
	bool flag = false;//借位标记
	bool isMinor = false;//负数标记
	vector<int>NumVec;
	int minor = 0;
	int i = 0, j = 0;
	if (size1 > size2)
	{
		big = lhs;
		small = rhs;
	}
	else if (size1 < size2)
	{
		big = rhs;
		small = lhs;
		isMinor = true;
	}
	else
	{
		for (; i < size1&&j < size2; ++i, ++j)
		{
			if (lhs.strDigit[i]>rhs.strDigit[j])
			{
				big = lhs;
				small = rhs;
				break;
			}
			else if(lhs.strDigit[i] < rhs.strDigit[j])
			{
				big = rhs;
				small = lhs;
				break;
			}
		}
		if (i==size1&&j==size2)
		{
			return "0";
		}
	}
	int m = big.strDigit.size() - 1;
	int n = small.strDigit.size() - 1;
	for (; m >= 0&&n >= 0; --m, --n)
	{
		int num1 = big.strDigit[m] - '0';
		int num2 = small.strDigit[n] - '0';
		if (flag)
		{
			minor = num1-1-num2;
			flag = false;
		}
		else
		{
			minor = num1 - num2;
		}
		if (minor < 0)
		{
			flag = true;
			minor = minor + 10;
		}
		NumVec.push_back(minor);
	}
	for (; m >= 0; --m)
	{
		int num = big.strDigit[m] - '0';
		if (flag)
		{
			minor = num - 1;
			flag = false;
		}
		else
		{
			minor = num;
		}
		if (minor < 0)
		{
			flag = true;
			minor = minor + 10;
		}
		NumVec.push_back(minor);
	}
	if (isMinor)
	{
		NumVec.push_back('-' - '0');
	}

	//string str = "";
	//for (int i = NumVec.size() - 1; i >= 0; --i)
	//{
	//	str.push_back(NumVec[i] + '0');
	//}
	//string::iterator itr =str.begin();
	//while (itr!=str.end())
	//{
	//	if (*itr == '0')
	//	{
	//		if (str.end() == itr + 1)
	//		{
	//			break;
	//		}
	//		itr = str.erase(itr);
	//	}
	//	else
	//	{
	//		break;
	//	}
	//}
	//
	string str = "";
	for (int i = NumVec.size() - 1; i >= 0; --i)
	{
		str.push_back(NumVec[i] + '0');
	}
	int k = 0;
	string::reverse_iterator itr = str.rbegin();
	while (itr != str.rend())
	{
		if (*itr!= 0)
		{
			break;
		}
		else
		{
			itr++;
			k++;
		}
		
	}
	str.substr(0, str.length() - k);
	return str;

}
int main()
{
BigInt int1 = "28937697857832167849697653231243";
BigInt int2 = "9785645649886874535428765";
cout << int1 + int2 << endl;
cout << int1 - int2 << endl;
return 0;
}

大数加减法_第1张图片

你可能感兴趣的:(C++,大数加减法)