C ++使用链表,模板和堆栈设计用于大整数加法和减法的类

时间:2014-10-27 19:10:32

标签: c++ class templates linked-list biginteger

目前我只实施了添加部分。

在main函数中,我的程序实际上可以分别打印出n2 n3, 但它无法打印下一个“n2 + n3”情况,因为它会出现运行时错误。

设置断点后,我发现当bigInteger& n传入案例“n2 + n3”, 以下声明不起作用,因此n的内容不会被修改。
linkedListIterator r = n.digits.begin();


以下是该程序的三段代码。 还有另一个链表头文件,它定义了node,iterator(p,q)的使用和一些成员函数,如insert()和length()。

非常感谢你的帮助。

class bigInteger
{
private:
int sign;                       // set 0 for positive, 1 for negative
linkedListType<int> digits;     // internal linked-list for storing digits in reverse order

public:
bigInteger();                           // default constructor
bigInteger(const bigInteger& other);    // copy constructor

// Overload constructor
// Use an numerical string to construct this bigInteger
// For negative number, the first char in the string is '-'
// e.g. "-12345"
bigInteger(const string& number);       

// overload the assignment operator
const bigInteger& operator= (const bigInteger& other);

// Return a new bigInteger that is equal to *this + other
// The contents of this and other should not be modified
bigInteger& operator+ (bigInteger& other);

// Return a new bigInteger that is equal to *this - other
// The contents of this and other should not be modified
bigInteger& operator- (bigInteger& other);

// Print a big integer
// Since the digits are stored in reverse order in the internal
// list, you should print the list reversely
// Print "undefined" if the digits list is empty
friend ostream& operator<<(ostream& os, bigInteger& n);
};


第二个是关于成员函数的实现。


bigInteger& bigInteger::operator+ ( bigInteger& other )
{
bigInteger resultBI; //saving the answer
bigInteger forSwap;  //not used
bool explicitSign = 0; //..
stack<int> resultStack; // stack saving the answer, later converting to Type BigInteger
int result;    //local var for addition
int carry = 0; //..
linkedListIterator<int> p = digits.begin();       //iterator marking the first node
linkedListIterator<int> q = other.digits.begin(); //..


if ( this->digits.length() >= other.digits.length() )
{
    while ( q != NULL )
    {
        result = ( *p + *q + carry ) % 10;  //  '*' acts like dereference
        carry = ( *p + *q + carry ) / 10;
        ++p;                                // "++' acts like increment to the link
        ++q;
        resultStack.push(result);          
    }
    while ( p != NULL )  //remaining carry
    {
        result = ( *p + carry ) % 10;
        carry = ( *p + carry ) / 10;
        ++p;
        resultStack.push(result);
    }
}

if ( this->digits.length() < other.digits.length() )
{
    while ( p != NULL )
    {
        result = ( *p + *q + carry ) % 10;
        carry = ( *p + *q + carry ) / 10;
        ++p;
        ++q;
        resultStack.push(result);
    }
    while ( q != NULL )
    {
        result = ( *q + carry ) % 10;
        carry = ( *q + carry ) / 10;
        ++q;
        resultStack.push(result);
    }
}

if ( carry != 0 )             //push and remaining carry
{
    resultStack.push(carry);
}


while ( !resultStack.empty() )                  //convert the stack to Type bigInteger
{
    resultBI.digits.insert ( resultStack.top() );
    resultStack.pop();
}

if ( explicitSign == 1 )  // not used
    resultBI.sign = 1;

return resultBI;

}


最后一部分是主要功能。

int main()
{

//-------- Test Case 1
bigInteger n1;                  
bigInteger n2("987654321");     
bigInteger n3("123456789");     
cout << "n1 = ";
cout << n1 << endl;             // undefined
cout << "n2 = ";
cout << n2 << endl;             // 987654321
cout << "n3 = ";
cout << n3 << endl;             // 123456789

//-------- Test Case 2
cout << "n2 + n3 = ";
cout << n2 + n3 << endl;    // 1111111110 //run-time error
return 0;
}

1 个答案:

答案 0 :(得分:3)

问题是你通过引用返回结果,一旦你返回就会消失的局部变量。

将运算符定义为按值返回,它应该可以更好地运行:

bigInteger operator+ (bigInteger& other);   // return by value.  

其他信息:

  • 这是一个article,其指南很好地解释了这个问题(“按值返回对象”部分)。

  • 这里有一个关于运算符重载的article,它由运营商家族解释了采取的方法和要避免的陷阱。