Fibonacci.cpp
| Added on 2010-01-13 22:39:50 |
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream> #define MAX 5000 #define BASE 10 using namespace std; class VeryLongInt { public: VeryLongInt(int init = 0) :m_array(NULL) { m_array = new char [MAX]; for (long i=0; i<MAX; i++) { m_array[i]=init%BASE; init=init/BASE; } } VeryLongInt(const VeryLongInt & toCopy) { m_array = new char [MAX]; for (long i=0; i<MAX; i++) { m_array[i]=toCopy.m_array[i]; } } ~VeryLongInt() { delete m_array; } const VeryLongInt operator+ (const VeryLongInt & other) const { VeryLongInt result; char carry=0; for (long i=0; i<MAX; i++) { result.m_array[i] = (m_array[i] + other.m_array[i] + carry)%BASE; carry=(m_array[i] + other.m_array[i] + carry)/BASE; } return result; } const VeryLongInt operator= (const VeryLongInt & other) const { for (long i=0; i<MAX; i++) { m_array[i]=other.m_array[i]; } return *this; } void output(ostream & out) { bool nonZero=false; for (long i=MAX; i>0; i--) { if ( m_array[i-1] != 0 || nonZero ) { out << static_cast<int>(m_array[i-1]); nonZero=true; } } } private: char * m_array; }; int main() { VeryLongInt a=0, b=1, c=1; long n=0; cout << "Vous voulez le n° : "; cin >> n; for ( long i=2; i<=n; i++ ) { c=b+a; a=b; b=c; } cout << "Voici : " << endl; c.output(cout); cout << endl; return 0; }
