所以,经过对很多网站的大量研究,我终于崩溃了,并且开了帐户。我是新手,也是编程的新手,所以请记住:)
我通过制作一个使用Euclid方法获得最大公约数的程序来制作递归的经典例子。我还需要跟踪函数调用自身的次数/递归发生的次数。
我让程序运行得很完美,但我使用全局(非常糟糕!)变量来保存递归计数。我需要用通过我的gcd函数传递的引用参数替换全局变量,但是我对非常很难理解它是如何工作的。这是我的问题。有人可以告诉我如何使用引用参数删除全局变量吗?非常感谢你!
#include <iostream>
using namespace std;
//Prototype functions
int gcd(int first, int second);
//Global varibles
//Depth will count how many times the recursive function runs.
int depth = 0;
int main ()
{
//variables
int firstNum, secondNum;
do
{
cout << "Welcome to Euclid's method for finding the GCD." << endl << "To quit, enter 0 for either number." << endl;
cout << "Enter the first number:";
cin >> firstNum;
cout << "Enter the second number:";
cin >> secondNum;
//The program quits if the user enters 0. If this happens, the program won't bother with running the recursive function.
if (firstNum != 0 && secondNum != 0)
{
cout << "The GCD of " << firstNum << " and " <<secondNum << " is ";
cout << gcd(firstNum, secondNum) << "." << endl;
cout << "The recursive calculation required a depth of " << depth <<"." << endl;
}
}
while (firstNum != 0 && secondNum != 0); //Program quits on 0 being entered
return (0);
}
//Recursive function. It will call itself until second number is 0.
int gcd(int first, int second)
{
if(second != 0)
{
depth++;
return gcd(second, first % second);
}
else
return first;
}
答案 0 :(得分:1)
尝试通过引用传递该参数。
int main ()
{
//variables
int firstNum, secondNum;
int depth = 0;
do
{
cout << "Welcome to Euclid's method for finding the GCD." << endl << "To quit, enter 0 for either number." << endl;
cout << "Enter the first number:";
cin >> firstNum;
cout << "Enter the second number:";
cin >> secondNum;
//The program quits if the user enters 0. If this happens, the program won't bother with running the recursive function.
if (firstNum != 0 && secondNum != 0)
{
cout << "The GCD of " << firstNum << " and " <<secondNum << " is ";
cout << gcd(firstNum, secondNum, depth) << "." << endl;
cout << "The recursive calculation required a depth of " << depth <<"." << endl;
}
}
while (firstNum != 0 && secondNum != 0); //Program quits on 0 being entered
return (0);
}
//Recursive function. It will call itself until second number is 0.
int gcd(int first, int second, int &depth)
{
if(second != 0)
{
depth++;
return gcd(second, first % second, depth);
}
else
return first;
}
答案 1 :(得分:1)
将参考参数添加到函数定义中。
int gcd(int first, int second, int& depth)
{
if(second != 0)
{
depth++;
return gcd(second, first % second, depth);
}
else
return first;
}
然后在depth
中将main()
声明为局部变量,将其初始化为0
,然后致电gcd(firstNum, secondNum, depth)
。
答案 2 :(得分:1)
int gcd(int first, int second, int& depth)
{
if(second != 0)
{
++depth;
return gcd(second, first % second, depth);
}
else
{
return first;
}
}
你必须把它称之为有点不同。创建新的int,将其初始化为0
并作为第三个参数传递。完成此操作后,它将保持深度。
答案 3 :(得分:1)
将原型参数的深度添加到(注意参考符号),不需要将其命名为深度。
int gcd(int first, int second, int &depth);
删除深度的全局定义。
为局部变量添加深度
int firstNum, secondNum, depth=0;
为第一次调用添加深度(通过引用传递)
std::cout << gcd(firstNum, secondNum, depth) << "." << std::endl;
为功能定义添加深度
int gcd(int first, int second, int &depth)
为递归调用添加深度
return gcd(second, first % second, depth);