BigNum 类字符串构造函数错误
所以我正在实现一个 BigNum 类来处理大整数,并且目前正在尝试修复我的字符串构造函数类。我必须能够读取数组中的“-345231563567”等字符串,并向后读取数字(即765365132543)。所附代码的第一部分检查第一个字符,看看它是正数还是负数,并将正数设置为 true 或 false。代码的下一部分检查数字中可能出现的前导零以及数字本身是否为零。最后一部分是将数字加载到数组中,由于某种原因我无法让代码工作。非常感谢任何有关解决方案的帮助。
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
So i am implementing a BigNum Class to deal with large integers and am currently trying to fix my string constructor class. I have to be able to read Strings such as "-345231563567" in an array with the numbers being read in backwards (i.e. 765365132543). The first part of the code attached checks the first character to see if it is positive or negative and sets positive to true or false. The next part of the code checks for leading zeros in the number that may occur as well as if the number is zero itself. the last part is what is loading the number into the array and for some reason i can not get the code to work. any help with a solution is much appreciated.
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;
}
}
The BigNum.h can be found here
http://csel.cs.colorado.edu/%7Eekwhite/CSCI2270Fall2011/hw2/revised/BigNum.h
and the Test file i am trying to use can be found here. I fail test 7
http://csel.cs.colorado.edu/%7Eekwhite/CSCI2270Fall2011/hw2/revised/TestBigNum.cxx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我只是尝试运行您的代码,但 digital= 行似乎有问题。它是一个您设置为等于某个值的指针。这可能是你的问题吗?
I just tried to run your code and there seems to be a problem with the digit= line. It is a pointer that you are setting equal to a value. Might that be your problem?
似乎您分配了定义为 20 的 DEFAULT_CAPACITY 字节,并继续在其中放入 22 位数字。
Seems like you allocate DEFAULT_CAPACITY bytes which you have defined as 20 and continue to put 22 digits in it.