题目:
编写一函数 int count(),使得第一次调用时返回1,第二次调用时返回2。即返回当前的调用次数。
代码:
1.全局变量
#include <iostream>
using namespace std;
int count();
int a = 0; //全局变量
int main()
{
number0:
cout << "这是第" << count() << "次调用函数" << endl;
cout << "请问是否要继续调用?若是请按1,若否请按0" << endl;
bool judge;
cin >> judge;
cout << endl;
if (judge == 0)
{
system("pause");
return 0;
}
else goto number0;
}
int count()
{
return a = a + 1;
}
2.静态的局部变量
#include <iostream>
using namespace std;
int count();
int main()
{
number0:
cout << "这是第" << count() << "次调用函数" << endl;
cout << "请问是否要继续调用?若是请按1,若否请按0" << endl;
bool judge;
cin >> judge;
cout << endl;
if (judge == 0)
{
system("pause");
return 0;
}
else goto number0;
}
int count()
{
static int a = 0;
return a = a + 1;
}