C++ 添加HugeInteger字符串对象
我正在解决一个问题,我必须添加HugeInteger对象,它是字符串对象中存储数字的对象。当我运行程序并输入2个值(即100000000000000000000000000000000000000000000 1)时,我得到一个调试断言失败…表达式:字符串下标输出或范围消息。我认为这个问题是由于我的运算符+=函数造成的。不确定如何实现进位部分/添加字符串。如果您能提供一些解决方法,我将不胜感激 这是我的密码 HugeIntegerC++ 添加HugeInteger字符串对象,c++,string,stdstring,C++,String,Stdstring,我正在解决一个问题,我必须添加HugeInteger对象,它是字符串对象中存储数字的对象。当我运行程序并输入2个值(即100000000000000000000000000000000000000000000 1)时,我得到一个调试断言失败…表达式:字符串下标输出或范围消息。我认为这个问题是由于我的运算符+=函数造成的。不确定如何实现进位部分/添加字符串。如果您能提供一些解决方法,我将不胜感激 这是我的密码 HugeInteger #include <iostream> #inclu
#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班
{
//需要为这两个功能提供友谊
friend std::istream&operator>>(std::istream&src、HugeInteger&value);
friend std::HugeInteger类的ostream和运算符
std::istream&operator>>(std::istream&src、HugeInteger&value);
std::ostream&运算符负值=false;
如果(value<0LL){//0LL是long-long类型的常量文字0
这->否定=正确;
value=-value;//使值为正
}
无符号整数i=0;
对于(;ihugeInt[i]=“0”;
}
此->hugeInt[i]='\0';
//将输入值的各个数字转换为大整数
对于(unsigned int j=hugeInt.size()-1;j>=0&&value!=0LL;j--)
{
短结果=值%10;
char c=(char)结果;
this->hugeInt[j]=c;
数值/=10;
}
//测试以确保HugeInteger能够包含值
如果(值!=0LL){
*this=0LL;//设置为-0表示溢出
this->negative=true;//可能应该增加分配的值
}//要解决此问题,请转到MaxDigit。
}
//将字符串转换为HugeInteger对象
HugeInteger::HugeInteger(const char*str)
{
此->输入(str);
}
//将long-long转换为HugeInteger,然后调用
//HugeInteger::运算符+=(常量HugeInteger&)
HugeInteger HugeInteger::运算符+(长-长值)常量
{
HugeInteger温度=*此;
返回温度+=(HugeInteger(值));
}
//将字符串转换为HugeInteger,然后调用
//HugeInteger::运算符+=(常量HugeInteger&)
HugeInteger HugeInteger::运算符+(常量字符*str)常量
{
HugeInteger温度=*此;
返回温度+=(HugeInteger(str));
}
//添加到“this”指针指向的HugeInteger中
//HugeInteger op。
//然后返回计算结果
HugeInteger和HugeInteger::运算符+=(常量HugeInteger和运算符)
{
整数进位=0;
对于(int i=op.hugeInt.size()-1;i>=0;i--)
{
这个->操作符++();
int temp=this->hugeInt[i];
温度+=进位;
this->hugeInt[i]=char(temp);
if(int(this->hugeInt[i])>9)
{
int temp=int(这个->hugeInt[i]);
温度-=10;
this->hugeInt[i]=char(temp);
进位=1;
}
其他的
{
进位=0;
}
}
归还*这个;
}
void HugeInteger::input(const std::string和str)
{
//现在假设是肯定的
这->否定=错误;
//先初始化为全零
无符号整数i=0;
此->hugeInt.clear();
而(ihugeInt+=str[i];
i++;
}
}
//预增量运算符
HugeInteger&HugeInteger::运算符++()
{
字符串键=此->hugeInt;
istringstream输入(键);
int_键;
在>>输入键中;
int_key++;
ostringstream out;
out hugeInt=键;
归还*这个;
}
istream&operator>>(istream&input、HugeInteger&value)
{
字符串输入字符串;
输入>>输入字符串;
值。输入(inputString);
返回输入;
}
ostream&operator在HugeInteger::operator+=()
中
i
以op.hugeInt.size()-1
为边界,但它可能超出this->hugeInt
的范围,因为this->hugeInt
可能要短得多。您能提供一组更小的可复制代码吗?可能会删除对您描述的行为没有任何影响的函数?请使用现有的bignum库,如
#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
for (int i = op.hugeInt.size() - 1; i >= 0; i--)
{
this->operator++();
int temp = this->hugeInt[i];