IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C++ Discussion :

Recursivité et Objet


Sujet :

C++

  1. #1
    Nouveau candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Novembre 2015
    Messages
    1
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Allemagne

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Novembre 2015
    Messages : 1
    Par défaut Recursivité et Objet
    Salut à tous.
    Je viens de terminé un exercice qui me demande de calculer le resultat de la suite de fibonacci d'un nombre entré par l'utilisateur. Mais le nombre que rentre l'utilisateur est de type Bigint un objet . le souci est le suivant : quand je teste pour 1 et 0 cela fonctionne mais pour plus de 1 le programme me renvoie une boite de dialogue comme quoi le programme ne fonctionne pas suite a un problème rencontré.
    Merci de bien vouloir m'aider
    PS : je developpe sur dev c++ et mon code est le suivant :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
     
    #include <iostream>
    #include<string>
    #include "BigInt.h"
     
    BigInt fibo(BigInt x)
    {
    	BigInt res,vor("0"),nach("1");
    	if (x==vor)
    		return x;
    	if (x==nach)
    		return x;
    	if (x>1)
    	{
    		return fibo(x-(nach+nach)) + fibo(x-nach);
    	}
    }
     
    int main()
    {
    	BigInt zahl,res;
    	cout<<"Geben sie einen Zahl ein : ";
    	cin>>zahl;
    	res=fibo(zahl);
    	cout<<"f ( "<<zahl<<" ) : "<<res<<endl;
    }

  2. #2
    Rédacteur/Modérateur


    Homme Profil pro
    Network game programmer
    Inscrit en
    Juin 2010
    Messages
    7 147
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : Canada

    Informations professionnelles :
    Activité : Network game programmer

    Informations forums :
    Inscription : Juin 2010
    Messages : 7 147
    Billets dans le blog
    4
    Par défaut
    Salut,

    à quoi servent toutes ces variables ?
    Si Bigint est un objet, vaudrait mieux le passer en const&
    Pour quelle valeur ton "programme ne répond plus" ? Vu que c'est récursif, tu dois faire exploser la callstack
    Dev-c++ est obsolète depuis une dizaine d'années, faudrait en changer rapidement
    Pensez à consulter la FAQ ou les cours et tutoriels de la section C++.
    Un peu de programmation réseau ?
    Aucune aide via MP ne sera dispensée. Merci d'utiliser les forums prévus à cet effet.

  3. #3
    Membre émérite
    Avatar de Daïmanu
    Homme Profil pro
    Développeur touche à tout
    Inscrit en
    Janvier 2011
    Messages
    735
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur touche à tout

    Informations forums :
    Inscription : Janvier 2011
    Messages : 735
    Par défaut
    Bonsoir.

    La ligne return fibo(x-(nach+nach)) + fibo(x-nach); me semble très compliquée. Elle ne devrait pas plutôt être return fibo(x-"1") + fibo(x-"2"); ?
    Et la variable res dans fibo() n'est pas utilisée.

  4. #4
    Expert confirmé
    Homme Profil pro
    Ingénieur développement matériel électronique
    Inscrit en
    Décembre 2015
    Messages
    1 599
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 62
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Ingénieur développement matériel électronique
    Secteur : High Tech - Électronique et micro-électronique

    Informations forums :
    Inscription : Décembre 2015
    Messages : 1 599
    Par défaut
    Bonjour,

    Le prototype ou la documentation de BigInt devrait permettre d'y voir clair.

    Cependant ( x > 1 ) semblant compiler, je propose de remplacer les deux variables nach et vor par 1 et 0, plutôt que "1" et "0".

  5. #5
    Membre éclairé
    Homme Profil pro
    Inscrit en
    Février 2013
    Messages
    70
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations forums :
    Inscription : Février 2013
    Messages : 70
    Par défaut
    Il serait utile de savoir de quel bigint.h on parle.

    J'ai trouvé celui-ci sur internet. Le constructeur qui prend un string est sans doute utile pour transférer le contenu d'un widget, qui est toujours d'un type chaine de caractères. Cependant, on ne devrait pas l'utiliser par choix par ce qu'il est certainement plus lent que le constructeur qui accepte un int.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    #ifndef _BIGINT_H
    #define _BIGINT_H
     
    // author: Owen Astrachan
    // 8/23/95, modified 7/5/96
    //
    // implements an arbitrary precision integer class
    // 
    // constructors:
    //
    // BigInt()            -- default constructor, value of integers is 0
    // BigInt(int n)       -- initialize to value of n (C++ int)
    // BigInt(const string & s) -- initialize to value specified by s
    //        it is an error if s is an invalid integer, e.g.,
    //        "1234abc567".  In this case the bigint value is garbage
    // BigInt(const BigInt & b)  -- copy constructor
    //
    //
    // *****  arithmetic operators:
    //
    // all arithmetic operators +, -, *, /, % are overloaded both
    // in form +=, -=, *=, /=, %=, and as binary operators
    //
    // multiplication and division also overloaded for *= int and /= int
    // e.g., BigInt a *= 3 (mostly to facilitate implementation)
    //
    //  ***** logical operators:
    //
    // bool operator == (const BigInt & lhs, const BigInt & rhs)
    // bool operator != (const BigInt & lhs, const BitInt & rhs)
    // bool operator <  (const BigInt & lhs, const BigInt & rhs)
    // bool operator <= (const BigInt & lhs, const BigInt & rhs)
    // bool operator >  (const BigInt & lhs, const BigInt & rhs)
    // bool operator >= (const BigInt & lhs, const BigInt & rhs)
    //
    //  ***** I/O operators:
    //
    //  ostream & Print()
    //        prints value of BigInt (member function)
    //  ostream & operator << (ostream & os, const BigInt & b)
    //        stream operator to print value
    //
     
     
    #include <iostream.h>
    #include <string>            // for strings
    #include "apvector.h"              // for sequence of digits
     
    class BigInt
    {
      public:
     
        BigInt();                  // default constructor, value = 0
        BigInt(int);               // assign an integer value
        BigInt(const string &);    // assign a string 
        BigInt(const BigInt &);    // copy constructor
        ~BigInt();                 // destructor
     
        ostream & Print(ostream & os) const;    // so << isn't a friend
     
        BigInt & operator = (const BigInt &);
     
        // operators: arithmetic, relational
     
        BigInt & operator += (const BigInt &);
        BigInt & operator -= (const BigInt &);
        BigInt & operator *= (const BigInt &);
        BigInt & operator *= (int num);
        BigInt & operator /= (const BigInt &);
        BigInt & operator /= (int num);
        BigInt & operator %= (const BigInt &);
     
        string toString() const;   // convert to string
     
        friend bool operator == (const BigInt &, const BigInt &);    
        friend bool operator < (const BigInt &, const BigInt &);
     
      private:
     
        enum BigError{
    	underflow, overflow, not_a_number,infinity,no_error
        };
     
        // private state/instance variables
     
        bool myIsNegative;
        apvector<char> myDigits;
        int myNumDigits;
        BigError myError;
     
        // helper functions
     
        bool IsNeg() const;        // return true iff number is negative
        int getDigit(int k) const;
        void addSigDigit(int value);
        void changeDigit(int digit, int value);
        int size() const;
        void Normalize();
        void DivideHelp(const BigInt & lhs, const BigInt & rhs,
    		    BigInt & quotient, BigInt & remainder);
     
    };
     
    // free functions
     
    ostream & operator <<(ostream &, const BigInt &);
    istream & operator >>(istream &, BigInt &);
     
    BigInt operator +(const BigInt & lhs, const BigInt & rhs);
    BigInt operator -(const BigInt & lhs, const BigInt & rhs);
    BigInt operator *(const BigInt & lhs, const BigInt & rhs);
    BigInt operator *(const BigInt & lhs, int num);
    BigInt operator *(int num, const BigInt & rhs);
    BigInt operator /(const BigInt & lhs, const BigInt & rhs);
    BigInt operator /(const BigInt & lhs, int num);
    BigInt operator %(const BigInt & lhs, const BigInt & rhs);
     
    bool operator != (const BigInt & lhs, const BigInt & rhs);
    bool operator >  (const BigInt & lhs, const BigInt & rhs);
    bool operator >= (const BigInt & lhs, const BigInt & rhs);
    bool operator <= (const BigInt & lhs, const BigInt & rhs);
     
     
    #endif   // _BIGINT_H not defined

Discussions similaires

  1. codage objet
    Par charly dans le forum Algorithmes et structures de données
    Réponses: 18
    Dernier message: 22/08/2002, 16h49
  2. algo : rotation d'objet 3d
    Par numeror dans le forum Algorithmes et structures de données
    Réponses: 4
    Dernier message: 19/08/2002, 22h58
  3. Importer des objets de 3dsMax
    Par Anonymous dans le forum OpenGL
    Réponses: 3
    Dernier message: 06/05/2002, 13h53
  4. Peux t'on créer une copie locale de l'objet partagé?
    Par Anonymous dans le forum CORBA
    Réponses: 8
    Dernier message: 16/04/2002, 16h20
  5. [Kylix] Erreur objet
    Par Anonymous dans le forum EDI
    Réponses: 1
    Dernier message: 22/03/2002, 09h41

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo