2013-10-19 9 views
0

我對類很陌生,雖然我編寫了所有其他代碼,但在我的兩個成員函數結束時,我停滯不前。我在哪裏使用賦值運算符?

這裏是我的頭:

class bignum 
{ 
public: 
// Constructors. 
bignum(); 
bignum(int num_digits); 
bignum(const string &digits); 
bignum(const bignum &other); 

// Destructors. 
~bignum(); 

// Assignment operator. 
bignum &operator=(const bignum &other); 

// Accessors 
int digits() const; 
int as_int() const; 
string as_string() const; 
void print(ostream &out) const; 
bignum add(const bignum &other) const; 
bignum multiply(const bignum &other) const; 
bool equals(const bignum &other) const; 
int PublicNumberTest; 

private: 
// Pointer to a dynamically-allocated array of integers. 

int *digit; 

// Number of digits in the array, not counting leading zeros. 

int ndigits; 
}; 
#endif 

,這裏是我的成員函數:

bignum bignum::multiply(const bignum& other) const{ 
bignum product; 
bignum row; 
int carry = 0; 
int sum = 0; 
int j = 0; 
int *temp_row = new int[]; 
for (int i = 0; i < ndigits-1; i++){ 
    carry = 0; 
    temp_row[i] = 0; 
    for (j; j < other.digits - 1; j++){ 
     sum = digit[i] * other.digit[j] + carry; 
     temp_row[i + j] = sum % 10; 
     carry = sum/10; 
    } 
    if (carry>0) 
     temp_row[i + j] = carry; 
    row = row operator+temp_row //This is what I don't understand. How can I 
     product = product.add(row); //assign the contents of temp_row? 

} 
} 

還有另一種,但它基本上是同樣的問題。我有一個數組,我想複製到我的...課程的內容和地方?我猜?謝謝閱讀。

+1

你想執行c = a * b還是a = a * b?也就是說,你是否打算將兩個參數相乘,產生一個臨時的,並將其分配給第三個參數,或者你是否打算將參數b乘以對象a? – ChuckCottrill

+0

我的意圖是C = A * B – OddLogic

回答

0

如果你想使用temp_row[]作爲一組數字對this對象,你可以這樣做:

delete [] digits; 
digits = new_row; 

你或許應該更新ndigits爲好。

+0

我很抱歉很厚,但我一直在這一整天。爲什麼我需要更新ndigits? – OddLogic

+0

如果位數已更改,則只需更新ndigits。由於涉及到運輸,可能會發生,或者可能不會。 –

0

2周的方式這樣做的:「舊」的方式,這應該是非常相似的調用,則析構函數拷貝構造函數:這也可以寫成

bignum& bignum::operator=(const bignum& other) 
{ 
    delete[] digit; 
    ndigits = other.ndigits; 
    digit = new int[ndigits]; 
    for (int i = 0; i != ndigits; ++i) { 
     digit[i] = other.digit[i]; 
    } 
    return *this; 
} 

代碼:

bignum& bignum::operator=(const bignum& other) 
{ 
    this->~bignum(); 
    new(this) bignum(other); 
    return *this; 
} 

這種老方式現在不鼓勵,因爲不是例外的安全。

「新」的方式,這是異常安全:您定義不會拋出任何異常的掉期()成員方法,並用它來實現運營商=():

void bignum::swap(bignum& other) noexcept 
{ 
    std::swap(ndigits, other.ndigits); 
    std::swap(digit, other.digit); 
} 

bignum& bignum::operator=(const bignum& other) 
{ 
    bignum tmp(other); 
    this->swap(tmp); 
    return *this; 
} 
+0

謝謝你們兩位! – OddLogic

0

而不是鍛鍊身體你多位數乘法,我只留下了幾件事情困擾了,

是否要執行C = A b或A = A b'也就是說,你打算 乘兩個參數,產生一個臨時的,並將其分配給第三個, 或者你打算只乘以一個參數b的對象a?

我的目的是C = A * B

然後你需要分配(新)目標BIGNUM,

使用朋友運算符重載應解決您的需求;這是常見的成語來定義二元運算符(請注意,我並沒有解決您的分配的號碼無誤),

friend bignum operator+(const bignum& a, const bignum& b); //in class 

//you want to calculate A * B --> C 
//you need a temporary place to calculate C, then produce a new C, 
bignum bignum::multiply(const bignum& A, const bignum& B) const 
{ 
    //Your A is this, A.digit[]; 
    //Your B is other, B.digit[]; 
    //Yout C is temp, a buffer for calculations, 
    bignum temp; 

    int carry = 0; 
    int sum = 0; 
    int j = 0; 
    //you don't need this, you have temp.digit[]; 
    for (int i = 0; i < this->ndigits-1; i++) 
    { 
     carry = 0; 
     temp.digit[i] = 0; 
     //what do you want to initialize j to? 
     for (j; j < B.ndigits - 1; j++) 
     { 
      sum = A.digit[i] * B.digit[j] + carry; 
      temp.digit[i + j] = sum % 10; 
      carry = sum/10; 
     } 
     if (carry>0) 
      temp.digit[i + j] = carry; 
    } 
    return bignum(temp); //allocate a new bignum 
} 

對於如何重載二元運算符的例子參見here ...

做你做的方式也有缺點(返回一個常量,不能鏈條超過兩個加法togethere),但基本上你做同樣的事情,但使用(this->)指針,

//you want to calculate A * B --> C 
//you need a temporary place to calculate C, then produce a new C, 
bignum bignum::multiply(const bignum& other) const 
{ 
    //Your A is this, this->digit[]; 
    //Your B is other 
    //Yout C is temp product, a buffer for calculations, 
    bignum temp; 

    int carry = 0; 
    int sum = 0; 
    int j = 0; 
    for (int i = 0; i < this->ndigits-1; i++) 
    { 
     carry = 0; 
     temp.digit[i] = 0; 
     for (j; j < other.ndigits - 1; j++) 
     { 
      sum = this->digit[i] * other.digit[j] + carry; 
      temp.digit[i + j] = sum % 10; 
      carry = sum/10; 
     } 
     if (carry>0) 
      temp.digit[i + j] = carry; 
    } 
    //bignum product; 
    //return *this = temp; //do this, 
    return bignum(temp); //or you should probably return a new object 
} 

你應該return a new object