Fibonacci.cpp

jbelljbell
Added on 2010-01-13 22:39:50
Fibonacci.cpp - Show - Edit - Download
Re: Fibonacci.cpp - Show - Edit - Download - Differences
#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;
}