我正在实现一个大型库,但我遇到了一个奇怪的问题。我有一个乘法的递归实现,但是第一个递归调用甚至没有开始解决。这是代码的一部分(在 BigInt 类中):
BigInt multiply_utility(BigInt num1, BigInt num2)
{
cout << "Location 1";
if(num1.get_length() == 1)
{
if(num1.get(MAX-1) == 0 || num2.get(MAX-1) == 0)
{
BigInt zero;
return zero;
}
BigInt temp = num2;
for(int i = 1; i < num1.get(MAX-1); i++)
num2 = temp.add(num2);
return num2;
}
else if(num2.get_length() == 1)
{
if(num1.is_zero() || num2.is_zero())
{
BigInt zero;
return zero;
}
BigInt temp = num1;
for(int i = 1; i < num2.get(MAX-1); i++)
num1 = temp.add(num1);
return num1;
}
int m = max(num1.get_length(), num2.get_length());
BigInt low1, low2, high1, high2;
for(int i = MAX-num1.get_length(); i < MAX-(num1.get_length()/2); i++)
low1.set((MAX-((MAX-(num1.get_length()/2))-i)) ,num1.get(i));
low1.auto_set_length();
for(int i = MAX-num2.get_length(); i < MAX-(num2.get_length()/2); i++)
low2.set((MAX-((MAX-(num2.get_length()/2))-i)) ,num2.get(i));
low2.auto_set_length();
for(int i = MAX-(num1.get_length()/2); i < MAX; i++)
high1.set(i,num1.get(i));
high1.auto_set_length();
for(int i = MAX-(num2.get_length()/2); i < MAX; i++)
high2.set(i,num2.get(i));
high2.auto_set_length();
cout << "low1 = " << low1.str() << endl;
cout << "high1 = " << high1.str() << endl;
cout << "low2 = " << low2.str() << endl;
cout << "high2 = " << high2.str() << endl;
BigInt z0,z1,z2;
BigInt handle;
cout << "Location 2";
z0 = multiply_utility(low1,low2);
cout << "Location 3";
z1 = multiply_utility(low1.add(high1),low2.add(high2));
z2 = multiply_utility(high1,high2);
BigInt a;
a = z1.subtract(z2);
a = a.subtract(z0);
return (((z2.shift(m)).add(a.shift(m/2))).add(z0));
/*
return (z2*10^(m))+((z1-z2-z0)*10^(m/2))+(z0)
*/
}
它打印“位置 1”,然后打印“位置 2”,但不会再次打印“位置 1”,因为它应该使用递归调用。知道有什么问题吗?莫非是类方法函数?