题目要求
要求设计程序能够计算大数加减法,如完成:
9785645649886874535428765 + 28937697857832167849697653231243
思路分析
加法:使用字符串存储大数,按右边对齐两个数,从右向左每一位相加并记录进位,进位要加到下一位的计算中。
减法:同样按右边对齐两个数,从右向左每一位相减,并且要记录借位和处理,最后结果注意去除前导0。
解答完整代码
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
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)
{
string result;
bool flag = false; // 进位
int size1 = lhs.strDigit.length() - 1;
int size2 = rhs.strDigit.length() - 1;
int i = size1, j = size2;
for (; i >= 0 && j >= 0; --i, --j)
{
int ret = lhs.strDigit[i] - '0' + rhs.strDigit[j] - '0';
if (flag)
{
ret += 1;
flag = false;
}
if (ret >= 10)
{
ret %= 10;
flag = true;
}
result.push_back(ret + '0');
}
if (i >= 0) // 左边操作数更长的处理
{
while (i >= 0)
{
int ret = lhs.strDigit[i] - '0';
if (flag)
{
ret += 1;
flag = false;
}
if (ret >= 10)
{
ret %= 10;
flag = true;
}
result.push_back(ret + '0');
i--;
}
}
else if (j >= 0)
{
while (j >= 0)
{
int ret = lhs.strDigit[j] - '0';
if (flag)
{
ret += 1;
flag = false;
}
if (ret >= 10)
{
ret %= 10;
flag = true;
}
result.push_back(ret + '0');
j--;
}
}
if (flag)
{
result.push_back('1');
}
reverse(result.begin(), result.end());
return result;
}
BigInt operator-(const BigInt& lhs, const BigInt& rhs)
{
string result;
bool flag = false; // 记录借位
bool minor = false; // 记录负号
// 保证maxStr大于等于minStr
string maxStr = lhs.strDigit;
string minStr = rhs.strDigit;
// lhs位数小于rhs位数,必然结果为负
if (maxStr.length() < minStr.length())
{
maxStr = rhs.strDigit;
minStr = lhs.strDigit;
minor = true;
}
else if (maxStr.length() == minStr.length())
{
if (maxStr < minStr)
{
maxStr = rhs.strDigit;
minStr = lhs.strDigit;
minor = true;
}
else if (maxStr == minStr)
{
return string("0");
}
}
int size1 = maxStr.length() - 1;
int size2 = minStr.length() - 1;
int i = size1, j = size2;
for (; i >= 0 && j >= 0; --i, --j)
{
int ret = maxStr[i] - minStr[j];
if (flag) // 前一位数字相加发生借位
{
ret -= 1;
flag = false;
}
if (ret < 0)
{
ret += 10;
flag = true; // 发生借位,记录
}
result.push_back(ret + '0');
}
// maxStr > minStr, 最后处理的必然是maxStr剩余位
while (i >= 0)
{
int ret = maxStr[i] - '0';
if (flag)
{
ret -= 1;
flag = false;
}
if (ret < 0)
{
ret += 10;
flag = true; // 发生借位,记录
}
result.push_back(ret + '0');
i--;
}
string retStr;
auto it = result.rbegin();
// 跳过前导0
for (; it != result.rend(); ++it)
{
if (*it != '0')
{
break;
}
}
for (; it != result.rend(); ++it)
{
retStr.push_back(*it);
}
if (minor)
{
retStr.insert(retStr.begin(), '-');
}
return retStr;
}
int main()
{
BigInt int1("9785645649886874535428765");
BigInt int2("28937697857832167849697653231243");
BigInt int3("9785645649886874213315428765");
cout << int1 + int2 << endl;
cout << int1 + int3 << endl;
cout << int1 - int3 << endl;
BigInt int4("9999");
BigInt int5("9998");
cout << int4 - int5 << endl;
return 0;
}