实现一个支持任意精度计算的大数类。
面试宝典
2023-06-12
Web前端开发工程师
32
可以使用字符串或数组来存储大整数,并针对加法、减法、乘法、除法等操作进行实现。
以下是一个简单的大数类实现(使用字符串存储):
```cpp
#include <iostream>
#include <string>
using namespace std;
class BigInteger {
private:
string num;
bool isNegative;
public:
BigInteger(string str = "0") {
if (str[0] == '-') {
isNegative = true;
num = str.substr(1);
} else {
isNegative = false;
num = str;
}
}
friend BigInteger operator+(const BigInteger& left, const BigInteger& right) {
string leftNum = left.num;
string rightNum = right.num;
int leftLen = leftNum.length();
int rightLen = rightNum.length();
int carry = 0;
string result = "";
// 对齐两个数,补齐前导零
while (leftNum.length() < rightNum.length()) {
leftNum = "0" + leftNum;
}
while (rightNum.length() < leftNum.length()) {
rightNum = "0" + rightNum;
}
// 从低位向高位逐位相加,注意进位
for (int i = leftLen - 1; i >= 0; i--) {
int sum = (leftNum[i] - '0') + (rightNum[i] - '0') + carry;
result = to_string(sum % 10) + result;
carry = sum / 10;
}
if (carry > 0) {
result = to_string(carry) + result;
}
BigInteger resultInt(result);
// 判断正负号
if (left.isNegative && right.isNegative) {
resultInt.isNegative = true;
} else if (left.isNegative) {
// 左数负号,相当于右数减去左数的相反数
resultInt.isNegative = right < left;
} else if (right.isNegative) {
resultInt.isNegative = left < right;
}
return resultInt;
}
friend bool operator<(const BigInteger& left, const BigInteger& right) {
if (left.isNegative && !right.isNegative) {
return true;
} else if (!left.isNegative && right.isNegative) {
return false;
} else if (left.num.length() != right.num.length()) {
return (left.isNegative && right.isNegative) ? left.num.length() > right.num.length() : left.num.length() < right.num.length();
} else {
if (left.isNegative && right.isNegative) {
for (int i = 0; i < left.num.length(); i++) {
if (left.num[i] != right.num[i]) {
return left.num[i] > right.num[i];
}
}
return false;
} else {
for (int i = 0; i < left.num.length(); i++) {
if (left.num[i] != right.num[i]) {
return left.num[i] < right.num[i];
}
}
return false;
}
}
}
void print() const {
if (isNegative) {
cout << "-";
}
cout << num << endl;
}
};
int main() {
BigInteger a("123456789123456789");
BigInteger b("-987654321987654321");
BigInteger c("9999999999999999999");
BigInteger d("-9999999999999999999");
BigInteger sum1 = a + b; // -864197532864197532
sum1.print();
BigInteger sum2 = a + c; // 10123456789012345678
sum2.print();
BigInteger sum3 = b + d; // -19753086419753086430
sum3.print();
cout << (a < b) << endl; // false
cout << (a < c) << endl; // true
cout << (b < d) << endl; // false
return 0;
}
```
此外,还可以实现更多高精度运算,例如减法、乘法、除法等。
以下是一个简单的大数类实现(使用字符串存储):
```cpp
#include <iostream>
#include <string>
using namespace std;
class BigInteger {
private:
string num;
bool isNegative;
public:
BigInteger(string str = "0") {
if (str[0] == '-') {
isNegative = true;
num = str.substr(1);
} else {
isNegative = false;
num = str;
}
}
friend BigInteger operator+(const BigInteger& left, const BigInteger& right) {
string leftNum = left.num;
string rightNum = right.num;
int leftLen = leftNum.length();
int rightLen = rightNum.length();
int carry = 0;
string result = "";
// 对齐两个数,补齐前导零
while (leftNum.length() < rightNum.length()) {
leftNum = "0" + leftNum;
}
while (rightNum.length() < leftNum.length()) {
rightNum = "0" + rightNum;
}
// 从低位向高位逐位相加,注意进位
for (int i = leftLen - 1; i >= 0; i--) {
int sum = (leftNum[i] - '0') + (rightNum[i] - '0') + carry;
result = to_string(sum % 10) + result;
carry = sum / 10;
}
if (carry > 0) {
result = to_string(carry) + result;
}
BigInteger resultInt(result);
// 判断正负号
if (left.isNegative && right.isNegative) {
resultInt.isNegative = true;
} else if (left.isNegative) {
// 左数负号,相当于右数减去左数的相反数
resultInt.isNegative = right < left;
} else if (right.isNegative) {
resultInt.isNegative = left < right;
}
return resultInt;
}
friend bool operator<(const BigInteger& left, const BigInteger& right) {
if (left.isNegative && !right.isNegative) {
return true;
} else if (!left.isNegative && right.isNegative) {
return false;
} else if (left.num.length() != right.num.length()) {
return (left.isNegative && right.isNegative) ? left.num.length() > right.num.length() : left.num.length() < right.num.length();
} else {
if (left.isNegative && right.isNegative) {
for (int i = 0; i < left.num.length(); i++) {
if (left.num[i] != right.num[i]) {
return left.num[i] > right.num[i];
}
}
return false;
} else {
for (int i = 0; i < left.num.length(); i++) {
if (left.num[i] != right.num[i]) {
return left.num[i] < right.num[i];
}
}
return false;
}
}
}
void print() const {
if (isNegative) {
cout << "-";
}
cout << num << endl;
}
};
int main() {
BigInteger a("123456789123456789");
BigInteger b("-987654321987654321");
BigInteger c("9999999999999999999");
BigInteger d("-9999999999999999999");
BigInteger sum1 = a + b; // -864197532864197532
sum1.print();
BigInteger sum2 = a + c; // 10123456789012345678
sum2.print();
BigInteger sum3 = b + d; // -19753086419753086430
sum3.print();
cout << (a < b) << endl; // false
cout << (a < c) << endl; // true
cout << (b < d) << endl; // false
return 0;
}
```
此外,还可以实现更多高精度运算,例如减法、乘法、除法等。
相关文章
- 请描述在Golang中使用MongoDB时的最佳实践。
- PHP中如何进行单元测试以及如何在开发过程中保证代码质量?
- PHP中如何处理文件上传和下载?
- PHP7和PHP5的性能上有什么差别?
- 请解释一下PHP中的MVC模式是如何工作的?
- 请列出与PHP相关的缓存机制及其优缺点。
- 如何在Golang中进行并发编程?
- 请问PHP中如何实现多线程?
- 请谈谈您对PHP的垃圾回收机制的了解及实践。
- 请给一个例子解释一下PHP中的闭包函数是什么?
- PHP中常用的设计模式有哪些?
- 请举例说明PHP中如何处理异常?
- 请解释什么是defer语句,以及它有什么作用?
- 如何在Golang中实现单例模式?
- 在PHP中,Magic Method都有哪些,并举例说明它们的作用?
- 如何通过PHP来保护您的代码免受SQL注入攻击?
- 请解释HTTP的基本概念,以及在Golang中如何使用HTTP?
- 聊一下高并发和高性能的区别和联系?
- 请提供至少三个通过PHP实现的网站性能优化技巧。
- 请解释下PHP中会话(session)和Cookie(cookie)的作用。
微信收款码
支付宝收款码