【发布时间】:2015-06-03 03:47:37
【问题描述】:
我正在解决一个问题,我必须添加将数字存储在字符串对象中的 HugeInteger 对象。当我运行程序并输入 2 个值(即 100000000000000000000000000000000000000000000000000000 1)时,我得到一个 Debug Assertion Failed...Expression: string subscript out or range 消息。我认为问题是由于我的 operator += 函数造成的。不确定如何实现进位部分/添加字符串。将不胜感激有关如何解决此问题的一些想法。
这是我的代码。
HugeInteger.h
#include <iostream>
#include <array>
#include <string>
class HugeInteger
{
// need to offer friendship to these 2 functions
friend std::istream & operator >> (std::istream & src, HugeInteger & value);
friend std::ostream & operator << (std::ostream & dest, const HugeInteger & value);
public:
//ctor that converts a "long long" into a HugeInteger
HugeInteger(long long value = 0LL); //0LL is constant literal value 0
// of type long long
//ctor that converts a string into a HugeInteger
HugeInteger(const char *str);
//Convert a string into a HugeInteger
void input(const std::string& str);
//adds RHS into LHS (the object pointed to by the "this" pointer) and returns result
HugeInteger & operator +=(const HugeInteger & RHS);
//adds a "long long" (RHS) and LHS and puts result into a temp HugeInteger
// and returns result
HugeInteger operator +(long long RHS) const;
//adds a string (which will be converted into a HugeInteger) with LHS into a temp
// HugeInteger and returns result
HugeInteger operator +(const char * RHS) const;
// overload preincrement operator for the HugeInteger class
HugeInteger & operator ++ (void);
private:
bool negative; // will be true if number is negative
std::string hugeInt; // each digit is stored in a string object
};
//overloads the << and >> operators for the HugeInteger class
std::istream & operator >> (std::istream & src, HugeInteger & value);
std::ostream & operator << (std::ostream & dest, const HugeInteger & value);
HugeInteger.cpp
#include "HugeInteger.h"
#include <sstream>
#include <iostream>
using namespace std;
HugeInteger::HugeInteger(long long value)
{
// set all MaxDigit digits to zero to start
this->negative = false;
if (value < 0LL){ // 0LL is constant literal 0 of type long long
this->negative = true;
value = -value; // make the value positive
}
unsigned int i = 0;
for (; i < hugeInt.size(); i++)
{
this->hugeInt[i] = '0';
}
this->hugeInt[i] = '\0';
// convert individual digits of input value into a HugeInteger
for (unsigned int j = hugeInt.size() - 1; j >= 0 && value != 0LL; j--)
{
short result = value % 10;
char c = (char)result;
this->hugeInt[j] = c;
value /= 10;
}
// test to make sure that HugeInteger was able to contain value
if (value != 0LL){
*this = 0LL; // set to -0, to signal overflow
this->negative = true; // Possibly should increase value assigned
} // to MaxDigit to fix this problem.
}
// converts string into a HugeInteger object
HugeInteger::HugeInteger(const char *str)
{
this->input(str);
}
// converts long long into HugeInteger and then invokes
// HugeInteger::operator +=(const HugeInteger & )
HugeInteger HugeInteger::operator +(long long value) const
{
HugeInteger temp = *this;
return temp += (HugeInteger(value));
}
//converts string into HugeInteger and then invokes
// HugeInteger::operator +=(const HugeInteger & )
HugeInteger HugeInteger::operator +(const char *str) const
{
HugeInteger temp = *this;
return temp += (HugeInteger(str));
}
// Adds into the HugeInteger pointed to by the "this" pointer
// the HugeInteger op.
// Then the calculated result is returned
HugeInteger & HugeInteger::operator+=(const HugeInteger &op)
{
int carry = 0;
for (int i = op.hugeInt.size() - 1; i >= 0; i--)
{
this->operator++();
int temp = this->hugeInt[i];
temp += carry;
this->hugeInt[i] = char(temp);
if (int(this->hugeInt[i]) > 9)
{
int temp = int(this->hugeInt[i]);
temp -= 10;
this->hugeInt[i] = char(temp);
carry = 1;
}
else
{
carry = 0;
}
}
return *this;
}
void HugeInteger::input(const std::string& str)
{
// assume positive for now
this->negative = false;
// init. to all zeros first
unsigned int i = 0;
this->hugeInt.clear();
while (i < str.size())
{
if (isdigit(str[i]))
this->hugeInt += str[i];
i++;
}
}
// Pre-increment operator
HugeInteger & HugeInteger::operator ++ ()
{
string key = this->hugeInt;
istringstream in(key);
int int_key;
in >> int_key;
int_key++;
ostringstream out;
out << int_key;
key = out.str();
this->hugeInt = key;
return *this;
}
istream & operator>>(istream & input, HugeInteger & value)
{
string inputString;
input >> inputString;
value.input(inputString);
return input;
}
ostream & operator << (ostream & output, const HugeInteger & value)
{
// find first non-zero digit
unsigned int i = 0;
if (value.hugeInt.size() == 0)
{
cout << '0';
}
while (i < value.hugeInt.size()){
if (value.hugeInt[i] != '0'){
break;
}
++i;
}
// if all zeros, just output a single 0
if (i == 40)
{
cout << '0';
return output;
}
// check if we need to ouput a negative sign
if (value.negative){
cout << '-';
}
// output remaining digits
for (; i < value.hugeInt.size(); i++)
{
cout << value.hugeInt[i];
}
return output;
}
主程序
#include "HugeInteger.h" // include definiton of class HugeInteger
using namespace std;
int main()
{
HugeInteger A, B, C, D;
// input value for A & B
cout << "****** Test << & >> operators ******\n\n";
cout << "Input values for A and B: ";
cin >> A >> B;
cout << "\nA = " << A << "\nB = " << B;
D = B;
// test += operator
cout << "\n\n****** Test += operator ******\n\n";
cout << "A = " << A << "\nB = " << B << "\nC = " << C << "\n\n";
cout << "C = B += A\n";
C = B += A;
cout << "\nA = " << A << "\nB = " << B << "\nC = " << C;
B = D; // restore B's value
system("pause");
return 0;
} // end main
【问题讨论】:
-
您能提供一组更小的可重现代码吗?也许删除对您描述的行为没有任何影响的功能?
-
使用现有的 bignum 库,例如 GMPlib