伙计们,我正在学习名为 LINT(大整数)的课程,直到知道为止,一切都很顺利。我坚持实施 operator/(const LINT&)。这里的问题是,当我想将 LINT 除以 LINT 时,我进入了递归 fnc 调用,即:
//unfinished
LINT_rep LINT_rep::divide_(const LINT_rep& bottom)const
{
typedef LINT_rep::Iterator iter;
iter topBeg = begin();
iter topEnd = end();
iter bottomBeg = bottom.begin();
iter bottomEnd = bottom.end();
LINT_rep topTmp;//for storing smallest number (dividend) which can be used to divide by divisor
while (topBeg != topEnd)
{
topTmp.insert_(*topBeg);//Number not large enough add another digit
if (topTmp >= bottom)
{//ok number >= we can divide
LINT_rep topShelf = topTmp / bottom;//HERE I'M RUNNING INTO TROUBLE
}
else
{
}
++topBeg;
}
return LINT_rep("-1");//DUMMY
}
我想要做的是实现这一点,就好像我会用手将这些数字相除一样,所以例如对于除数 1589 和除数 27 我会这样:
- 检查第一个数字是否 >= 除数,如果是则除
- 如果不添加到第一个数字另一个数字并检查是否 a > b
在某些时候它会更大(在简化的场景中),如果是这样,我必须分开,但在这种情况下,我遇到了递归调用,我不知道如何打破它。
注意:例如,作为 tmp,我必须使用 LINT 而不是 int,因为这些数字不适合 int。
所以一般来说,我要问的是还有其他方法可以进行除法吗?或者我的想法可能存在错误的逻辑(很可能)。谢谢你。