2011-10-03 77 views
1

讓我實現BIGNUM類來處理大的整數,我目前想解決我的字符串構造類。我必須能夠讀取數組中的「-345231563567」等字符串,並向後讀取數字(即765365132543)。附加代碼的第一部分檢查第一個字符,看它是正面還是負面,並將正面設置爲真或假。代碼的下一部分檢查可能出現的數字中的前導零,以及數字本身是否爲零。最後一部分是將數字加載到數組中,出於某種原因,我無法讓代碼工作。非常感謝任何解決方案的幫助。BIGNUM類字符串構造錯誤

BigNum::BigNum(const char strin[]) 
{ 
size_t size = strlen(strin); 
positive = true; 
used=0; 
if(strin[0] == '+') 
{ 
    positive = true; 
    used++; 
} 
else if(strin[0] == '-') 
{ 
    positive = false; 
    used++; 
} 
else 
{ 
    positive = true; 
} 

// While loop that trims off the leading zeros 
while (used < size) 
{ 
    if (strin[used] != '0') 
    { 
    break; 
    } 

    used++; 
} 

// For the case of the number having all zeros 
if(used == size) 
{ 
    positive = true; 
    digits = new size_t[1]; 
    capacity = 1; 
    digits[0] = 0; 
    used = 1; 
} 
// Reads in the digits of the number in reverse order 
else 
{ 
    int index = 0; 
    digits = new size_t[DEFAULT_CAPACITY]; 
    capacity = size - used; 


    while(used < size) 
    { 
    digits[index] = strin[size - 1] - '0'; 
    index++; 
    size--; 
    } 
    used = index + 1; 
} 
} 

的BigNum.h可以在這裏找到 http://csel.cs.colorado.edu/%7Eekwhite/CSCI2270Fall2011/hw2/revised/BigNum.h

,我試圖使用測試文件可以在這裏找到。我測試失敗7 http://csel.cs.colorado.edu/%7Eekwhite/CSCI2270Fall2011/hw2/revised/TestBigNum.cxx

+1

你得到的錯誤是什麼,順便說一句? – liaK

+0

當我嘗試並運行設置的文件我失敗測試7這是特別測試字符串的構造函數。由於某些原因,它不會將字符串讀入數組中。 – Sean

+0

任何不使用'std :: string'存儲的理由? –

回答

0

好像你分配DEFAULT_CAPACITY字節,你已經定義爲20,並繼續把22位數字。

0

我只是試圖運行你的代碼,並似乎有與數字=行的問題。這是一個指針,您設置的值等於一個值。可能是你的問題?