Type bigint

5/5 (3 avis)

Vue 5 239 fois - Téléchargée 249 fois

Description

Type BigInt : entier signéde de taille (en nombre d'octets en mémoire) arbitraire.
Vous pouvez paramétrer certains éléments
- BigInt::defaultSize est la taille par défaut lors de la création du BigInt. Pour modifier la taille d'un entier en utilisant setSize(taille).
- BigInt::autoAjust permet d'autoriser ou non l'ajustement automatique de la taille de l'entier.
- BigInt::defaultBase spécifie la base prise par défaut par un nouveau BigInt. Vous pouvez spécifier une base différente pour chaque entier en utilisant setBase(base).

Je vous ai mis l'entête pour vous faire une idée, vous la retrouverez dans le zip avec le fichier cpp.

Source / Exemple :


// fonction de groupage de chiffres à l'affichage ::m_groupDigits

#ifndef __BIGINT_H__
#define __BIGINT_H__

typedef unsigned char byte;

class BigInt {
protected:
  bool m_sg;
  long m_size;
  int m_base;
  byte * m_sequence;
  void init ();
public:

  // Parametres
  static bool autoAjust;
  static long defaultSize;
  static int defaultBase;

  // Constructeurs
  BigInt (long num = 0);
  BigInt (const BigInt & num);
  BigInt (char * num);
  ~BigInt ();

  // Affectation
  BigInt & operator = (long num);
  BigInt & operator = (const BigInt & num);
  BigInt & operator = (const char * num);
  BigInt & Empty ();

  // Modification
  BigInt & operator += (const BigInt & num);
  BigInt & operator -= (const BigInt & num);
  BigInt & operator *= (const BigInt & num);
  BigInt & operator /= (const BigInt & num);
  BigInt & operator %= (const BigInt & num);
  BigInt & operator ++ ();
  BigInt & operator ++ (int);
  BigInt & operator -- ();
  BigInt & operator -- (int);
  BigInt & operator &= (const BigInt & num);
  BigInt & operator |= (const BigInt & num);
  BigInt & operator ^= (const BigInt & num);
  BigInt & operator <<= (long num);
  BigInt & operator >>= (long num);
  BigInt & negate ();

  // Autres calculs
  BigInt & operator + (const BigInt & num) const;
  BigInt & operator + () const;
  BigInt & operator - (const BigInt & num);
  BigInt & operator - () const;
  BigInt & operator * (const BigInt & num) const;
  BigInt & operator / (const BigInt & num) const;
  BigInt & operator % (const BigInt & num) const;
  BigInt & operator & (const BigInt & num) const;
  bool operator && (const BigInt & num) const;
  BigInt & operator | (const BigInt & num) const;
  bool operator || (const BigInt & num) const;
  BigInt & operator ^ (const BigInt & num) const;
  BigInt & operator << (long num) const;
  BigInt & operator >> (long num) const;

  // Comparaison
  bool operator ! () const;
  bool operator == (const BigInt & num) const;
  bool operator < (const BigInt & num) const;
  bool operator <= (const BigInt & num) const;
  bool operator > (const BigInt & num) const;
  bool operator >= (const BigInt & num) const;
  bool operator != (const BigInt & num) const;
  bool isNull () const;
  bool isNeg () const;
  bool isPos () const;

  // Parametrage
  long getSize () const;
  void setSize ();
  void setSize (long size);
  int getBase () const;
  void setBase (int base);

  // Transformation, extraction
  operator void * () const;
  operator char * () const;
  char * c_str (int base) const;
  operator long () const;
  byte operator [] (long num) const;

  // functions Friend
  friend BigInt & abs (const BigInt & num);
};

#ifdef __STD_IOSTREAM__
  ostream & operator << (ostream & o, const BigInt & num);
  istream & operator >> (istream & i, BigInt & num);
#endif // __STD_IOSTREAM__

// Parametres par defaut
bool BigInt::autoAjust = true;
long BigInt::defaultSize = 4;
int BigInt::defaultBase = 10;

bool isNum (char c);
char toChar (byte num);
byte fromChar (char c);
long log2 (long num);

BigInt & abs (const BigInt & num);

#endif // __BIGINT__H__

Conclusion :


pour l'affichage en base 2, 4 ou 16, je vous conseille la fonction c_str (base) : c'est bien plus performant qu'en utilisant l'opérateur (char*).

Je suis à la recherche d'éventuels bugs, si vous en trouvez faites moi signe ;)

Codes Sources

A voir également

Ajouter un commentaire Commentaires
koolj Messages postés 3 Date d'inscription jeudi 20 mars 2003 Statut Membre Dernière intervention 5 mars 2004
5 mars 2004 à 10:38
En effet j'ai aussi trouvé les même bugs en testant, l'affectation par chaine de caractères a de gros problèmes. Mais ce n'est pas l'ajustement automatique qui cause cela. J'ai déjà corrigé une partie, je le poste quand j'aurais réglé les autres pti problèmes.

En fait elle semble bien marcher avec l'auto ajustement désactivé et une affectation par type long ou int (je sais, ca donne qqc de restreint dsl). Je vais corriger tout ca et je reposte la version débuggée.
cosmobob Messages postés 700 Date d'inscription mardi 30 décembre 2003 Statut Membre Dernière intervention 27 janvier 2009 4
4 mars 2004 à 22:46
en gros elle pue ta classe !!! t'aurais pu mieux tester avant de la poster ici.
cosmobob Messages postés 700 Date d'inscription mardi 30 décembre 2003 Statut Membre Dernière intervention 27 janvier 2009 4
4 mars 2004 à 18:15
il faut pas mettre les initialisations de variable statique ds le .h (sinon ca compile que quand un seul fichier fait référence a BigInt.h !!!!!!) mais ds le .cpp. Sinon ds le zip tu aurais pu au- mettre un fichier d'exemple, histoire ke il y ait kelke chose a compiler...
sinon effectivement ya des bugs, mais comme en particulier la fonction de conversion en char* bogue, impossible de tester davantage...
essaie ca:
void main()
{

BigInt a = "314159265358979323";
cout << a.c_str(10) << endl;
}
il s'affiche : 4137591035. en gros l'auto ajustement ne s'est pas fait.

et chez moi (visual c++) ca me mets meme un 'debug error' des ke j'execute, si je change la taille par défaut (genre jmets 8).

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.