大整数运算加减重载c++
时间: 2025-06-05 17:17:34 浏览: 11
### C++中实现大整数运算的加减法重载
在C++中,为了处理超出内置数据类型范围的大整数运算,可以使用字符串来存储大整数,并通过重载运算符实现加减法。以下是基于引用内容[^1]、[^2]和[^4]的详细实现方法。
#### 类设计
首先定义一个类 `BigInt` 用于表示大整数。该类包含以下成员:
- 私有成员变量 `strDigit`:用于存储大整数的字符串形式。
- 构造函数:接受一个字符串作为参数初始化大整数。
- 友元函数:用于重载加法和减法运算符,以及输出流操作符。
```cpp
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);
};
```
#### 运算符重载实现
##### 加法运算符重载
加法运算符需要从最低位开始逐位相加,并处理进位逻辑。以下是加法运算符的实现:
```cpp
BigInt operator+(const BigInt& lhs, const BigInt& rhs) {
string result;
int carry = 0;
int i = lhs.strDigit.size() - 1;
int j = rhs.strDigit.size() - 1;
while (i >= 0 || j >= 0 || carry > 0) {
int digitSum = carry;
if (i >= 0) digitSum += lhs.strDigit[i--] - '0';
if (j >= 0) digitSum += rhs.strDigit[j--] - '0';
carry = digitSum / 10;
result.push_back((digitSum % 10) + '0');
}
reverse(result.begin(), result.end());
return BigInt(result);
}
```
##### 减法运算符重载
减法运算符需要从最低位开始逐位相减,并处理借位逻辑。以下是减法运算符的实现:
```cpp
BigInt operator-(const BigInt& lhs, const BigInt& rhs) {
string result;
int borrow = 0;
int i = lhs.strDigit.size() - 1;
int j = rhs.strDigit.size() - 1;
while (i >= 0 || j >= 0) {
int digitDiff = (lhs.strDigit[i--] - '0') - borrow;
if (j >= 0) digitDiff -= (rhs.strDigit[j--] - '0');
if (digitDiff < 0) {
digitDiff += 10;
borrow = 1;
} else {
borrow = 0;
}
result.push_back(digitDiff + '0');
}
// 去掉前导零
while (result.size() > 1 && result.back() == '0') result.pop_back();
reverse(result.begin(), result.end());
return BigInt(result);
}
```
#### 输出流重载
为了方便打印 `BigInt` 对象的结果,可以重载输出流操作符 `<<`:
```cpp
ostream& operator<<(ostream& out, const BigInt& src) {
out << src.strDigit;
return out;
}
```
#### 示例代码
以下是一个完整的示例程序,演示如何使用上述实现进行大整数加减法运算:
```cpp
#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);
};
BigInt operator+(const BigInt& lhs, const BigInt& rhs) {
string result;
int carry = 0;
int i = lhs.strDigit.size() - 1;
int j = rhs.strDigit.size() - 1;
while (i >= 0 || j >= 0 || carry > 0) {
int digitSum = carry;
if (i >= 0) digitSum += lhs.strDigit[i--] - '0';
if (j >= 0) digitSum += rhs.strDigit[j--] - '0';
carry = digitSum / 10;
result.push_back((digitSum % 10) + '0');
}
reverse(result.begin(), result.end());
return BigInt(result);
}
BigInt operator-(const BigInt& lhs, const BigInt& rhs) {
string result;
int borrow = 0;
int i = lhs.strDigit.size() - 1;
int j = rhs.strDigit.size() - 1;
while (i >= 0 || j >= 0) {
int digitDiff = (lhs.strDigit[i--] - '0') - borrow;
if (j >= 0) digitDiff -= (rhs.strDigit[j--] - '0');
if (digitDiff < 0) {
digitDiff += 10;
borrow = 1;
} else {
borrow = 0;
}
result.push_back(digitDiff + '0');
}
while (result.size() > 1 && result.back() == '0') result.pop_back();
reverse(result.begin(), result.end());
return BigInt(result);
}
ostream& operator<<(ostream& out, const BigInt& src) {
out << src.strDigit;
return out;
}
int main() {
BigInt int1("28937697857832167849697653231243");
BigInt int2("9785645649886874535428765");
cout << "Addition: " << int1 + int2 << endl;
cout << "Subtraction: " << int1 - int2 << endl;
return 0;
}
```
### 注意事项
1. 在减法中,假设 `lhs` 大于或等于 `rhs`。如果可能涉及负数结果,需扩展实现以支持符号处理。
2. 输入数据应为非负整数字符串,且不包含前导零(除非是数字 `0`)。
阅读全文
相关推荐


















