所以我正在实现一个 BigNum 类来处理大整数,目前正在尝试修复我的字符串构造函数类。我必须能够读取数组中的字符串,例如“-345231563567”,其中的数字被向后读取(即765365132543)。附加代码的第一部分检查第一个字符以查看它是正面还是负面,并将正面设置为真或假。代码的下一部分检查可能出现的数字中的前导零以及数字本身是否为零。最后一部分是将数字加载到数组中的内容,由于某种原因,我无法使代码正常工作。非常感谢任何有关解决方案的帮助。
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