#include< iostream >
using namespace std;
bool fibo( int pos, int & elem );
bool fibo_display( int pos );
int main()
{
int pos;
int elem=0;
bool bl = true;
char ch;
while( bl )
{
cout<<" input the pos : ";
cin>>pos;
if(fibo( pos, elem ))
cout<<" the num at the pos "<<pos<<" is "<<elem<<endl;
fibo_display( pos );
cout<<" Do you want to continue,Y/N ? ";
cin>>ch;
if( ch == 'N' || ch == 'n' )
bl = false;
}
return 0;
}
bool fibo( int pos, int & elem )
{
if( pos <= 0 || pos > 1024 )
{
elem = 0;
return false;
}
elem = 1;
int n_1 = 1;
int n_2 = 1;
for( int ix = 2; ix < pos; ix++ )
{
elem = n_1 + n_2;
n_1 = n_2;
n_2 = elem;
}
return true;
}
// 由于这里并没有什么意义,只是用来显示这个数列,所以也可以用一个void返回类型的,
// 但里面的return语句是要改写的。
bool fibo_display( int pos )
{
int elem;
if( pos <= 0 || pos > 1024 )
{
elem = 1;
return false;
}
// 此处很值得欣赏的。。。。
switch( pos )
{
// break;
case 2:
cout<< " elem : 1 ";
//break;
default:
cout<< " elem : 1 ";
case 1:
cout<< " 1 ";
break;
}
int n_1 = 1;
int n_2 = 1;
for( int ix = 2; ix < pos; ix++ )
{
elem = n_1 + n_2;
n_1 = n_2;
n_2 = elem;
cout<< elem << " ";
//要想在此处控制每行都是十行的元素这样写好像是不行的。还要改写的。
if(!(ix%10))
cout<<endl;
}
cout<<endl;
return true;
}