[英]Changing a Global Variable to a Reference Parameter C++
所以,經過很多網站的大量研究,我終於崩潰了,並做了一個帳戶。 我是新手,也是編程新手,所以請記住這一點:)
我通過制作一個使用歐幾里得方法作為最大公約數的程序來制作遞歸的經典示例。 我還需要跟蹤函數調用自身的次數/遞歸發生的次數。
我的程序運行良好,但我使用全局(非常糟糕!)變量來保存遞歸計數。 我需要通過我的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;
}
嘗試通過引用傳遞該參數。
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;
}
將引用參數添加到函數定義中。
int gcd(int first, int second, int& depth)
{
if(second != 0)
{
depth++;
return gcd(second, first % second, depth);
}
else
return first;
}
然后在main()
中將depth
聲明為局部變量,將其初始化為0
,然后調用gcd(firstNum, secondNum, depth)
。
int gcd(int first, int second, int& depth)
{
if(second != 0)
{
++depth;
return gcd(second, first % second, depth);
}
else
{
return first;
}
}
你必須稱之為有點不同。 創建新的 int,將其初始化為0
並作為第三個參數傳遞。 完成此操作后,它將保持深度。
向原型參數添加深度(注意參考符號)它不需要命名為深度。
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.