2011-10-03 45 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

我只是试图运行你的代码,并似乎有与数字=行的问题。这是一个指针,您设置的值等于一个值。可能是你的问题?