Re: Fibonacci.cpp
| Added on 2010-01-15 21:15:28 |
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
72
73
74
75
#include <iostream> #include <vector> #include <string> #include <sstream> #include <algorithm> #define TYPE unsigned long #define BASE 1000000000 #define LENGTH 9 using namespace std; class VeryLongInt { public: VeryLongInt(long init = 0) :m_array() { while ( init != 0 ) { m_array.push_back(init%BASE); init=init/BASE; } } VeryLongInt(const VeryLongInt & toCopy) { m_array = toCopy.m_array; } ~VeryLongInt() { } const VeryLongInt operator+ (const VeryLongInt & other) const { VeryLongInt result; vector<TYPE> other_=other.m_array, m_array_=m_array; TYPE carry = 0; long size = max(m_array.size(), other.m_array.size()); other_.resize(size, 0); m_array_.resize(size, 0); result.m_array.resize(size, 0); for ( long i = 0; i < size; i++ ) { result.m_array[i] = (m_array_[i] + other_[i] + carry)%BASE; carry = (m_array_[i] + other_[i] + carry)/BASE; } if ( carry != 0 ) result.m_array.push_back(carry); return result; } void output(ostream & out) { long aSize = m_array.size(); out << static_cast<unsigned long>(m_array[aSize-1]); for ( long i = aSize - 2; i >= 0; i-- ) { ostringstream oss; oss << m_array[i]; string number = oss.str(); long nSize = number.size(); for ( long j = 0; j < LENGTH - nSize; j++ ) out << "0"; out << static_cast<unsigned long>(m_array[i]); } } private: vector<TYPE> 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; }

