【发布时间】:2021-02-27 03:26:02
【问题描述】:
简介:
我有一个家庭作业,要为“大于现有类型可以存储的整数”创建一个类型。 我们应该将数字存储在一个数字数组中(向后以便于数学逻辑)。 我正在使用动态数组将信息存储在对象中。 我至少需要重载 +、-、*、、> 运算符。 .cpp 和 .h 文件也必须分开。
问题:
不太清楚如何根据类属性和所需的操作重载 >> 运算符。
BigIntegers.h
#include <string>
#include <iostream>
typedef int* BigIntPtr;
class BigIntegers {
private:
int size; // based on string size, if neg string size -1
BigIntPtr number; // dynamic array ptr
bool isNeg; // set default to false, assumes a positive number
public:
explicit BigIntegers(std::string num = "");
BigIntegers(const BigIntegers &bi);
~BigIntegers();
friend std::istream &operator>>(std::istream &is, BigIntegers &bi) {
/**
* using eg "is >> bi.data;" doesn't seem viable given the data manipulation needed
* see constructor
*/
std::string input;
getline(is,input);
bi = BigIntegers(input);
return is;
}
friend std::ostream &operator<<(std::ostream &os, const BigIntegers &bi) {
if(bi.isNeg) //add sign if needed
os << '-';
for(int s=bi.size-1;s>-1;s--) //print reverse
{
os << bi.number[s];
}
return os;
}
};
BigIntegers.cpp
#include <algorithm>
#include "BigIntegers.h"
BigIntegers::BigIntegers(std::string num) {
//if null
if(num.empty())
{
size = 0;
number = NULL;
isNeg = 0;
return;
}
//determine if its negative
if (num.find('-') == 0)
{
num.erase(remove(num.begin(),num.end(), '-'),num.end());
isNeg =true;
}else {isNeg= false;}
size = num.length();
number = new int[size];
//add array backwards for math optimization
std::string rev; rev.assign(num.rbegin(),num.rend());
for(int i = 0; i < size; i++)
{
number[i]=rev[i]-'0';
}
}
BigIntegers::~BigIntegers() {
delete [] number;
size =0;
isNeg =0;
}
#include <iostream>
#include "BigIntegers.h"
using std::cout;
using std::cin;
using std::string;
int main() {
//basic functionality test
string stringInt = "123456";
string stringIntNeg = "-99987654321";
BigIntegers test1(stringInt);
cout << test1 << "\n";
BigIntegers test2(stringIntNeg);
cout << test2 << "\n";
//iostream test
cout << "Enter a big integer in the form 123456 or -123456.\n";
BigIntegers test3;
cin >> test3;
cout << test3 << "\n";
return 0;
}
output
pr4_bigIntegers\cmake-build-debug\pr4_bigIntegers.exe
123456
-12345678987654321
Enter a big integer in the form 123456 or -123456.
5789256
-57883070081-2144186072
Process finished with exit code 0
注意:
此外,有时输出几乎是正确的,但包含负数或其他一些垃圾值。例如) cin >> 5314 , cout
edit - 我意识到在 4 位数之后引入了垃圾。实验仍在继续。
分配说明 - (对于其他上下文,这是直接复制/粘贴)
C++ 中现有的整数类型不能存储非常大的整数。我们需要一种新类型来存储我们在处理科学问题时可能需要的这些大整数。 您可以通过将整数存储为数字数组来表示整数。
设计并实现一个整数运算类,其中一个数字被实现为一个数字数组。数组的每个条目都是从 0 到 9(含)的数字。 表示的数字是数组中数字的串联。
您至少需要为此类重载 +、-、*、、> 运算符。尝试重载除法运算符 /。
不要忘记实现三组:赋值运算符、复制构造函数和析构函数。
除法运算符是整数运算符,因此它返回商的整数部分。您需要了解此类的目的是存储大整数,因此在重载这些运算符的过程中,您不应将数组表示形式转换为常规整数表示形式。同样,我们假设这些整数不能通过使用内置整数类型来处理,因此您的显式构造函数应该有一个字符串类型参数,而不是整数类型参数,并从字符串中获取每个字符,将其转换为数字并存储它到你的阵列。为了轻松执行操作,您可能希望在数组中以相反的顺序存储一个整数。
- 使用动态数组存储整数。
- 包括代码的专业文档和适当的缩进
- 将头文件与实现文件分开
- 测试课堂的各个方面。
来自老师的电子邮件说明
只需回答你们中的几个问题。
-
整数是有符号的,因为当你做减法时,你可能会得到一个负整数。所以使用数组的第一个点来存储 0 或 1(0 表示负数,1 表示正数)。
-
说明不允许您将字符串参数转换为整数。我的意思是您不应该将字符串 s="123456" 转换为 int n=123456。但是,您必须将字符 1 转换为整数 1,...,字符 6 转换为整数 6 并将每个字符存储到您的数组中。
【问题讨论】:
-
将其作为字符串输入,一次解析一个字符。
-
stackoverflow.com/help/minimal-reproducible-example。在这里提出问题时,使用固定输入而不是在运行时读取输入是个好主意。您的 test2 输出与输入不匹配。这是一个错误还是 operator
-
一个问题是,如果你在输入中没有找到负号,你永远不会设置
isNeg -
前 2 个测试是固定的,每次对我来说都可以正常工作。第三个测试检查分配所需的 >> 运算符的重载。
-
但是你的问题是什么。负号是由于在构造函数中未将 isNeg 设置为 false 造成的。您的 >> 运算符是正确的。
标签: c++ operator-overloading cin dynamic-arrays