2012-01-30 89 views
1

我正在爲學校做一個項目,我需要創建一個bigint類,並且它迄今有4個需求。運算符==超載

1.)寫一個寫一個bigint的方法,每行最多打印80個數字。

2.)編寫一個方法來比較兩個bigint是否相等。它應該返回一個布爾值。

3.)編寫一個方法來初始化一個bigint爲你提供的int值[0,maxint]。

4.)編寫一個方法來初始化bigint到char []。

我認爲我有2和3是正確的,但我在比較兩個bigint時遇到了麻煩,我希望有人能夠在正確的方向上引導我如何將打印限制爲每行80個數字。

這裏是我到目前爲止的代碼:

.h文件中

class bigint 
{ 
public: 

bigint(); //default constructor 

bool operator==(const bigint& num1); 

bigint(int n); 

bigint(char new_digits[]); 


private: 
    int digit[MAX]; 
    int digitb[MAX]; 

}; 

這裏是實現文件:

#include "bigint.h" 
#include<cassert> 
#include<iostream> 


//default constructor 
bigint::bigint() 
{ 
    for (int i = 0; i < MAX; i++) 
    { 
    digit[i] = 0; 
    } 

    } 


    bigint::bigint(int n) 
    { 
    int i = 0; 



     while(n > 0) 
    { 
     digit[i] = n % 10; 
     n = n /10; 
     ++i; 
break; 
    } 


    for(i; i< MAX; ++i) 
     digit[i] = 0; 


    } 

bool bigint::operator==(const bigint& num1) 
    { 

     for(int i = 0; i < MAX; i++) 
      { 
     if (num1.digit == num1.digit) 
       return true; 
      } 
     return false; 

     } 


    bigint::bigint(char new_digit[]) 
     { 
    int i = 0; 

     //Reads the characters of numbers until it is ended by the null symbol 

      while(new_digit[i] != '\0') 
      ++i; 

      --i; 

     //Converts the characters into int values and puts them in the digit array 
     while(i >= 0) 
    { 
      digit[i] = new_digit[i] - '0'; 
      --i; 
    } 


} 


} 



int main() 
    { 

    #include<iostream> 

using namespace std; 
using PROJECT_1::bigint; 

bigint a(0); 

assert(a == 0); 
    } 

順便說一句,我不是要得到答案我我剛剛在一小時前開始的作業。我一整天都在努力工作,最後我投入並尋求幫助。

+5

A)請正確格式化您的代碼,而不是道歉。編輯並不難以掌握B)這真的不是一個「爲我工作」的網站;讓你的帖子可讀後,解釋你已經嘗試了什麼,或者至少說明你目前的思路。如果提供了這兩件事情,人們更願意提供幫助。 – 2012-01-30 06:19:20

+1

哦,而operator ==總是返回true。問題在於: if(num1.digit == num1.digit) 還有其他問題 - 我的第一個問題是:「數字」數組的表示是什麼?更具體地說,「digit [0]」是最重要的數字還是最不重要的數字? – 2012-01-30 06:34:44

回答

1

operator==內部循環的內容不起作用。你正在做的是比較digit陣列(技術上是指針)num1digit陣列指針num1。這將永遠是真實的。您應該將this->digit中的每個索引與num1.digit中的相應索引進行比較。像這樣:

bool bigint::operator==(const bigint& num1) 
{ 
    for(int i = 0; i < MAX; i++) 
    { 
     if (digit[i] != num1.digit[i]) 
      return false; 
    } 
    return true; 
} 

正如你所看到的,我也改變了從等於不等於的比較。這是因爲否則如果只有第一個數字在bigint兩個對象中都是相同的,那麼在僅檢查第一個數字後,使用equal將使函數返回true

0

嗯,我覺得你比較的實現是不正確的(我也許錯了,因爲你沒有指定什麼比較的條件,所以這是我的假設),因爲:

您在比較兩個數字同樣通過BIGINT,看到這一點:

bool bigint::operator==(const bigint& num1) 
{ 
    for(int i = 0; i < MAX; i++) 
    { 
     if (num1.digit == num1.digit) // it'll be always true 
     return true; 
    } 
    return false; 
} 

所以,因爲這是家庭作業,我不會給精確解,但對方法,通過數字比較的數字,看看this操作符重載,它應該幫助。閱讀,嘗試和學習。

對於第二個問題,您需要打印每行80個數字,因此對所有字符從頭到尾運行一個循環,並且當循環計數器達到80(或79是從0開始)時,輸出一個換行符。這是一個解決方案。

並提出要求下次更清晰。

1

您的代碼有許多問題。已經指出最直接的一個 :在==運算符 兩側的表達式是相同的,所以自然該函數返回true。寫在C++中此功能的最 慣用的方法是:

return std::equals( 
    std::begin(digit), std::end(digit), std::begin(num1.digit)); 

在專業的代碼,我會考慮別的差編程。 在一個sudent任務中,它不太清楚,因爲其中一個目標可能是 以瞭解如何自己寫這些東西,並且在這種情況下可能不允許您使用 標準算法。我還是會去的相同 基本方法,但是使用「迭代」,而不是 指標:

int const* current = std::begin(digit); 
int const* other = std::begin(num1.digit); 
int const* end  = std::end(digit); 
while (current != end && *current == *other) { 
    ++ current; 
    ++ other; 
} 
return current == end; 

至於其餘代碼:

  • operator==絕對應該const;否則,即使像myBigInt == 0這樣簡單的東西也不行。

  • 事實上,operator==大概應該是一個非成員。我喜歡 成員函數isEqual,並且有operator==(和 operator!=)稱它,但使它成爲friend也是一個非常有效的 選項。不知道digitb應該做什麼。

  • 你是構造函數使用int是不兼容使用 char[]。你需要決定你的內部表示是 big-endian還是little-endian。 Little-endian算術運算可能更容易,但這意味着您必須在BigInt(char[])中按相反順序處理 數字。事實上,你開始 出來,就好像你要處理以相反的順序字符,但 你最終倒退兩個數組過,從來沒有初始化結束的digit ,並沒有改變順序。 (你還需要檢查 每個char真的是使用標準isdigit 功能的數字—)

在一般情況下,你應該使用標準功能時,他們做 工作(和你的任務允許它)。在BigInt::BigInt(char[])中,對於 示例,new_digit + strlen(new_digit)會給你一個 「迭代器」到'\0'。 (使用迭代器去 倒退比去向前麻煩一點,因爲你允許減小數字超出陣列前端不是 喜歡的東西:

const const* source = new_digits + strlen(new_digits); 
while (source != new_digits) { 
    -- source; 
    // ... 
} 

效果很好,但是

+0

謝謝大家!你的信息。真的有幫助。 – Neil 2012-02-03 03:04:15