简体   繁体   中英

C++ designing a class for big integer addition and subtraction using linked list, template and stack

Current I have implemented only the addition part.

In the main function, my program can actually print out n2 n3 respectively, but it can't print out the next "n2+n3" case as it gets run-time error.

After I set the breakpoints, I found that when bigInteger& n is passing into case "n2+n3", the follow statement didnt work and thus the content of n is not modified.
linkedListIterator r = n.digits.begin();


Below are three pieces of codes of the program. There's another linked list header file which defines the uses of node, iterator (p, q) and some member function like insert() and length().

Thanks anyways for your help.

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);
};


The second one is about member function implementation.


 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; } 


The last part is the main function.

 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; } 

The problem is that you return the result by reference to a local variable that will disapear as soon as you return.

Define your operator as returning by value and it should work much better:

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

Additional information:

  • Here is an article with guidelines explaining the issue pretty well (section "Returning Objects by Value").

  • Here an article about operator overloading which explains by family of operators the approach to take and pittfalls to avoid.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM