No.27657
Доброняши, подскажите как реализовать счетчик вызова функции zet() и fib() - переменный count1 и count2 соответственно? Счетчики внезапно считают, в power() и Fibonaci() выдают правильный результат, а в main() выдают нули.
#include "stdafx.h"
#include <iomanip>
#include <iostream>
using namespace std;
int count1;
int count2;
int zet(double &y, double &x, unsigned int &k)
{
count1++;
if (k>0)
{
if (k%2==1)
{
y*=x; k--;
}
else
{
x*=x; k/=2;
};
zet(y,x,k);
}
return count1;
}
double power(double x, unsigned int n)
{
double y =1;
zet(y, x, n);
return y;
}
//////////////////////////////////////////////////////////////////////////
void fib(double &f1, double &f2, unsigned int n)
{
count2++;
if (n>=2)
{
double f =f2; f2+=f1; f1=f;
fib(f1, f2, n-1);
}
return;
};
double Fibonaci (unsigned int n)
{
double f0=0, f1=1;
switch ( n )
{
case 0:
return f0;break;
case 1:
return f1; break;
default:
fib(f0, f1, n);
return f1;
}
};
/////////////////////////////////////////////////////
int tmain(int argc, TCHAR* argv[])
{
double x;
unsigned int n;
cout<<"________"<<endl<<setw(22)<<"Power"<<endl<<"________"<<endl;
cout<<"Enter the number"<<endl;
//cin>>x;
x = 5;
cout<<x<<endl;
cout<<"Enter the power"<<endl;
//cin>>n;
n = 3;
cout<<n<<endl;
unsigned int numb;
cout<<"x = "<<x<<" n = "<<n<<endl;
cout<<"Result: "<<power(x,n)<<endl<<"Number of occurrences: "<<endl;
cout<<"________"<<endl<<setw(24)<<"Fibonaci"<<endl<<"________"<<endl;
cout<<"Enter number what you want"<<endl;
//cin>>numb;
numb = 8;
cout<<"numb = "<<numb<<endl;
cout<<"Result: "<<Fibonaci(numb)<<endl<<"Number of occurrences: "<<count2<<endl;
return 0;
}