6_51.cpp
上传用户:zipjojo
上传日期:2009-07-20
资源大小:70k
文件大小:1k
- #include<iostream.h>
- class Fib
- {
- int value;
- int fib;
- public:
- void get();
- void CalFib();
- void Display()
- {
- cout<<"Fib("<<value<<")="<<fib<<endl;
- }
- };
- inline void Fib::get()
- { int Fib_N;
- cout<<"请输入此数列的第n项!"<<endl;
- cout<<"n=";
- cin>>Fib_N;
- value=Fib_N;
- fib=1;
- }
- void Fib::CalFib()
- {
- int n=value;
- if(n==0||n==1) fib=n;
- else
- {
- int oneback=1,twoback=0,current;
- for(int i=2;i<=n;i++)
- {
- current=oneback+twoback;
- twoback=oneback;
- oneback=current;
- }
- fib=current;
- }
- }
-
- void main()
- {
-
- Fib myFib; //创建Fib类的对象myFib
- myFib.Fib::get(); //与myFib.get();等价
- myFib.CalFib();
- myFib.Display();
- }