我正在研究一个问题,我必须在字符串对象中添加存储数字的HugeInteger对象。当我运行程序并输入2个值(即100000000000000000000000000000000000000000000000000000000 1)时,我得到一个Debug Assertion Failed ... Expression:string下标out或range message。我认为问题是由于我的运算符+ =函数。不确定如何实现进位部分/添加字符串。希望了解如何解决这个问题的一些想法。
这是我的代码。
HugeInteger.h
method.invoke(annotation, (Object[])null) or method.invoke(annotation, new Object[0])
HugeInteger.cpp
#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);
MainProgram(主程序)
#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;
}
答案 0 :(得分:0)
在HugeInteger::operator+=()
,
for (int i = op.hugeInt.size() - 1; i >= 0; i--)
{
this->operator++();
int temp = this->hugeInt[i];
i
受op.hugeInt.size() - 1
限制,但this->hugeInt
可能超出范围,因为this->hugeInt
可能会短得多。