簡體   English   中英

C ++使用鏈表,模板和堆棧設計用於大整數加法和減法的類

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

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

在main函數中,我的程序實際上可以分別打印出n2 n3,但它不能打印出下一個“n2 + n3”的情況,因為它會得到運行時錯誤。

設置斷點后,我發現當bigInteger&n傳入case“n2 + n3”時,follow語句不起作用,因此n的內容不會被修改。


以下是該程序的三段代碼。 還有另一個鏈表頭文件,它定義了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; } 

問題是您通過引用返回結果返回一個局部變量,該變量將在您返回后立即消失。

將運算符定義為按值返回,它應該更好地工作:

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

附加信息:

  • 這篇文章的指南很好地解釋了這個問題(“按值返回對象”一節)。

  • 這里有一篇關於運算符重載的文章 ,該文章由運營商家族解釋了采取的方法和要避免的陷阱。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM