如何重载可以处理类大整数对象的+运算符,该整数可以在数组中存储多达100位的数字



我正在从一本名为";使用C++的数据结构";D.S.Malik。我目前正在解决以下书面编程练习:

在C++中,最大的int值是2147483647。所以一个大于这个的整数不能作为整数存储和处理。类似地,如果两个正整数大于2147483647,则结果不正确。存储和操作大整数的一种方法是存储每个单独的数字数组中数字的。设计类较大的整数,以便这个类的对象可以存储一个最长为100位的整数。过载运算符+和–分别对两个对象的值进行加法和减法运算这一类的。(在第3章的编程练习中,我们将超载乘法运算符。(重载赋值运算符以复制一个大整数的值转换为另一个大的整数。使流过载提取和插入运算符,便于输入和输出。您的程序必须包含适当的构造函数才能初始化类的对象大型整数。(提示:将数字作为字符串读取,并存储数字的顺序相反。添加实例变量以存储数字和数字的符号。(

下面是我对上面的代码:

#include <bits/stdc++.h>
using namespace std;
class largeIntegers {
private:
/* data */
int num[100] = {0};
int digits;
bool isPositive;
public:
friend istream &operator>>(istream &, largeIntegers &);
largeIntegers &operator+(largeIntegers &);
void getLargeInteger(void);
void setLargeInteger(string);
void print(void);
largeIntegers();
largeIntegers(string);
};
int main() {
largeIntegers num1;
cin >> num1;
largeIntegers num2;
num2 = num1;
cin >> num1;
largeIntegers num3;
num3 = num1 + num2;
num3.print();
return 0;
}
largeIntegers &largeIntegers::operator+(largeIntegers &integer) {
largeIntegers temp;
int remainder = 0;
int digi = max(digits, integer.digits);
temp.digits = digi;
for (int i = 0; i < digi; i++) {
temp.num[i] = (remainder + num[i] + integer.num[i]) % 10;
remainder = (remainder + num[i] + integer.num[i]) / 10;
}
if (remainder) {
temp.num[digi] = remainder;
temp.digits++;
}
return temp;
}
largeIntegers::largeIntegers(string n) { setLargeInteger(n); }
largeIntegers::largeIntegers() {
isPositive = true;
digits = 0;
}
void largeIntegers::setLargeInteger(string n) {
int start = 0;
if (n[0] == '-') {
isPositive = false;
start = 1;
}
digits = 0;
for (int i = n.length() - 1; i >= start; i--) {
num[digits] = int(n[i]) - int('0');
digits++;
}
}
void largeIntegers::getLargeInteger(void) {
cout << "Enter a large integer:n";
string n;
cin >> n;
setLargeInteger(n);
}
istream &operator>>(istream &isObject, largeIntegers &largeInt) {
string num;
isObject >> num;
largeInt.setLargeInteger(num);
return isObject;
}
void largeIntegers::print(void) {
if (!isPositive)
cout << "-";
for (int i = digits - 1; i >= 0; i--) {
cout << num[i];
}
cout << endl;
}

但是我的代码不适用于加法操作。它给出分段故障(核心转储(错误。有人能解释一下问题出在哪里以及如何解决吗?

问题是重载的operator+返回了对局部变量的引用。要解决此问题,您可以使用以下版本的operator+:

//return by value 
largeIntegers operator+(const largeIntegers &lhs, const largeIntegers &integer)
{
largeIntegers temp;
int remainder = 0;
int digi = max(lhs.digits, integer.digits);
temp.digits = digi;
for (int i = 0; i < digi; i++) {
temp.num[i] = (remainder + lhs.num[i] + integer.num[i]) % 10;
remainder = (remainder + lhs.num[i] + integer.num[i]) / 10;
}
if (remainder) {
temp.num[digi] = remainder;
temp.digits++;
}
return temp;
}

为了实现上述功能,通过在类中添加如下好友声明,使operator+成为好友:

friend largeIntegers operator+(const largeIntegers &lhs, const largeIntegers &rhs);

使用上述修改的完整工作程序可以在这里看到,如下所示:

#include <bits/stdc++.h>
using namespace std;
class largeIntegers {
private:
/* data */
int num[100] = {0};
int digits;
bool isPositive;
public:
friend istream &operator>>(istream &, largeIntegers &);
//i have added this friend declaration
friend largeIntegers operator+(const largeIntegers &lhs, const largeIntegers &rhs);
void getLargeInteger(void);
void setLargeInteger(string);
void print(void);
largeIntegers();
largeIntegers(string);
};
int main() {
largeIntegers num1;
cin >> num1;
largeIntegers num2;
num2 = num1;
cin >> num1;
largeIntegers num3;
num3 = num1 + num2;
num3.print();
return 0;
}
//i have modified the operator+ as shown below
largeIntegers operator+(const largeIntegers &lhs, const largeIntegers &integer)
{
largeIntegers temp;
int remainder = 0;
int digi = max(lhs.digits, integer.digits);
temp.digits = digi;
for (int i = 0; i < digi; i++) {
temp.num[i] = (remainder + lhs.num[i] + integer.num[i]) % 10;
remainder = (remainder + lhs.num[i] + integer.num[i]) / 10;
}
if (remainder) {
temp.num[digi] = remainder;
temp.digits++;
}
return temp;
}

largeIntegers::largeIntegers(string n) { setLargeInteger(n); }
largeIntegers::largeIntegers() {
isPositive = true;
digits = 0;
}
void largeIntegers::setLargeInteger(string n) {
int start = 0;
if (n[0] == '-') {
isPositive = false;
start = 1;
}
digits = 0;
for (int i = n.length() - 1; i >= start; i--) {
num[digits] = int(n[i]) - int('0');
digits++;
}
}
void largeIntegers::getLargeInteger(void) {
cout << "Enter a large integer:n";
string n;
cin >> n;
setLargeInteger(n);
}
istream &operator>>(istream &isObject, largeIntegers &largeInt) {
string num;
isObject >> num;
largeInt.setLargeInteger(num);
return isObject;
}
void largeIntegers::print(void) {
if (!isPositive)
cout << "-";
for (int i = digits - 1; i >= 0; i--) {
cout << num[i];
}
cout << endl;
}

查看上面代码中的注释以检查我所做的修改

最新更新