#include "projet.h" // Constructeur sans argument/par défaut Grandentier::Grandentier() : negatif(false) {this->lg = 1; this->tab = new int [this->lg]; this->tab[0] = 0;} // Constructeur Grandentier::Grandentier(int nombre) { if (nombre == 0)// Si le nombre est égal à 0 {this->negatif = false; // Initialisation à 0 this->lg = 1; this->tab = new int [this->lg]; this->tab[0] = 0; } else { if ( nombre < 0 ) // Si le nombre est négatif {this->negatif = true; nombre = -nombre; } else {this->negatif = false ;} int * temp = new int [128]; // Allocation d'un tableau temporaire d'entiers int i = 0; while (nombre != 0) // Tant que le nombre n'est pas égal à 0, décomposition du nombre { temp[i] = nombre % 10; nombre /= 10; i++; } this->lg = i; // Allocation du tableau this->tab = new int [this->lg]; for (int j = 0 ; j < this->lg ; j++) {this->tab[j] = temp[j];} delete [] temp;// Libèration de la mémoire allouée pour le tableau temporaire } } Grandentier::Grandentier(const std::string& val) { int nombre; std::istringstream iss(val); iss>>nombre; if (nombre == 0)// Si le nombre est égal à 0 {this->negatif = false; // Initialisation à 0 this->lg = 1; this->tab = new int [this->lg]; this->tab[0] = 0; } else { if ( nombre < 0 ) // Si le nombre est négatif {this->negatif = true; nombre = -nombre; } else {this->negatif = false ;} int * temp = new int [128]; // Allocation d'un tableau temporaire d'entiers int i = 0; while (nombre != 0) // Tant que le nombre n'est pas égal à 0, décomposition du nombre { temp[i] = nombre % 10; nombre /= 10; i++; } this->lg = i; // Allocation du tableau this->tab = new int [this->lg]; for (int j = 0 ; j < this->lg ; j++) {this->tab[j] = temp[j];} delete [] temp; } } // constructeur ne fonctionnant pas pour le moment... /*Grandentier::Grandentier(const char *s) { this->negatif= false; while(*s == '+' || *s == '-') if(*(s++) == '-') this->negatif = !this->negatif; while(*s == '0' && isdigit(*(s + 1))) //enlève tous les zéros au début ++s; int len = 0; while(isdigit(s[len])) ++len; for(int i = 0; i < len; ++i) { int x = len - i; int a = s[x - 1]; this->lg = len; this->tab[i]=a; } }=*/ // Constructeur par recopie Grandentier::Grandentier(const Grandentier &GE) { this->negatif = GE.negatif; this->lg = GE.lg; // Allocation du tableau this->tab = new int [this->lg]; for (int i = 0 ; i < this->lg ; i++) { this->tab[i] = GE.tab[i]; } } // Destructeur Grandentier::~Grandentier() {delete [] this->tab;// Libèration de la mémoire allouée pour le tableau } // Opérateur d'affectation Grandentier& Grandentier::operator= (const Grandentier &GE) { // Si pas d'auto-affectation if (this != &GE) { this->negatif = GE.negatif; this->lg = GE.lg; // Libèration de l'espace mémoire du tableau delete [] this->tab; // Allocation du tableau this->tab = new int [this->lg]; for (int i = 0 ; i < this->lg ; i++) { this->tab[i] = GE.tab[i]; } } return (*this); } // Opérateur addition // this + GE Grandentier Grandentier::operator+ (const Grandentier &GE) { Grandentier add; if (!this->negatif && !GE.negatif) // Si this et GE sont positifs {add = this->addition(GE); } else if (this->negatif && GE.negatif)// Si this et GE sont négatifs { add = this->addition(GE); add.negatif = true; } else if (!this->negatif && GE.negatif) // Si this est positif et GE négatif {add = this->soustraction(GE); } else if (this->negatif && !GE.negatif) // Si this est négatif et GE positif {add = this->soustraction(GE); add.negatif = !add.negatif; } return add; } // Opérateur soustraction // this - GE Grandentier Grandentier::operator- (const Grandentier &GE) { Grandentier diff; if (!this->negatif && GE.negatif) // Si this est positif et GE négatif {diff = this->addition(GE); } else if (this->negatif && !GE.negatif) // Si this est négatif et GE positif {diff = this->addition(GE); diff.negatif = true; } else if (!this->negatif && !GE.negatif && *this > GE) // Si this et GE sont positifs et this > GE {diff = this->soustraction(GE); } else if (!this->negatif && !GE.negatif && *this < GE) // Si this et GE sont positifs et this < GE {diff = this->soustraction(GE); diff.negatif = true; } else if (this->negatif && GE.negatif && *this > GE) // Si this et GE sont négatifs et this > GE {diff = this->soustraction(GE); diff.negatif = false; } else if (this->negatif && GE.negatif && *this < GE) // Si this et GE sont négatifs et this < GE { diff = this->soustraction(GE); diff.negatif = true; } return diff; } // Opétateur multiplicateur // this * GE Grandentier Grandentier::operator* (const Grandentier &GE) { Grandentier multi; if ((!this->negatif && !GE.negatif) || (this->negatif && GE.negatif))// Si this et GE sont positifs ou this et GE sont négatifs {multi = this->multiplication(GE); } else if ((!this->negatif && GE.negatif) || (this->negatif && !GE.negatif))// Si this est positif et GE négatif ou this est négatif et GE positif {multi = this->multiplication(GE); multi.negatif = true; } return multi; } // Opération d'addition Grandentier Grandentier::addition (const Grandentier &GE) { int nb_chif_max = (this->lg > GE.lg) ? this->lg: GE.lg; // Taille du résultat de l'addition int nb_car = nb_chif_max + 1; int * temp = new int [nb_car]; // Allocation d'un tableau temporaire int somme, chif1, chif2, i; // Déclaration des variables int retenue = 0; for (i = 0 ; i < nb_chif_max ; i++) { chif1 = (i < this->lg) ? this->tab[i] : 0; chif2 = (i < GE.lg) ? GE.tab[i] : 0; somme = chif1 + chif2 + retenue; if (somme >= 10) // Si la somme est supérieure ou égale à 10 { somme -= 10; // on enlève 10 à cette somme retenue = 1; // la retenue reçoit 1 } else { retenue = 0;// la retenue reçoit 0 } temp[i] = somme; // l'élément courant reçoit la somme } if (retenue == 1) // Si la dernière retenue est égale à 1 {temp[i] = 1; // l'élément courant reçoit 1 } else { nb_car--; // on enlève un élément } Grandentier res; res.lg = nb_car; delete [] res.tab;// Libèration de l'espace mémoire du tableau res.tab = new int [res.lg]; // Allocation du tableau for (int j = 0 ; j < res.lg; j++) {res.tab[j] = temp[j];} delete [] temp; // Libèration de l'espace mémoire du tableau temporaire return (res); } // Opération de soustraction Grandentier Grandentier::soustraction(const Grandentier &GE) {int nb_chif_max = (this->lg > GE.lg) ? this->lg: GE.lg; // Taille du résultat de la soustraction int nb_car = nb_chif_max + 1; int * temp = new int [nb_car]; // Allocation d'un tableau temporaire int diff, chif1, chif2, i; // Déclaration des variables int retenue = 0; Grandentier res; Grandentier e1,e2; e1 = *this; // Affectation de this dans e1 et GE dans e2 e2 = GE; e1.negatif = false;// e1 et e2 sont positifs e2.negatif = false; if (e1 < e2)// Si e1 est inférieur à e2 {e1 = GE;// Affectation de this dans e2 et GE dans e1 e2 = *this; res.negatif = true; // le résultat de GE - this est négatif } for (i = 0 ; i < nb_chif_max ; i++) { chif1 = (i < e1.lg) ? e1.tab[i] : 0; chif2 = (i < e2.lg) ? e2.tab[i] : 0; if (chif1 < (chif2+retenue)) // Si le chif1 est inférieur au chif2 plus la retenue {chif1 += 10; // on rajoute 10 à chif1 diff = chif1 - (chif2+retenue); // Différence entre chif1 et chif2 plus la retenue retenue = 1; // la retenue reçoit 1 } else { diff = chif1 - (chif2+retenue);// Différence entre chif1 et chif2 plus la retenue retenue = 0; // la retenue reçoit 0 } temp[i] = diff; // l'élément courant reçoit la différence } if (retenue == 1) // Si la dernière retenue est égale à 1 {temp[i] = 1; // l'élément courant reçoit 1 } else { nb_car--; // on enlève un élément } for (int k = nb_car - 1; temp[k] == 0; k--) // Suppression des 0 en début du tableau {nb_car--;} res.lg = nb_car; delete [] res.tab; // Libèration de l'espace mémoire du tableau res.tab = new int [res.lg]; // Allocation du tableau for (int j = 0 ; j < nb_car; j++) { res.tab[j] = temp[j];} delete [] temp ; // Libèration de l'espace mémoire du tableau temporaire return res; } // Opération de multiplication Grandentier Grandentier::multiplication(const Grandentier &GE) {int produit,retenue,nb_car,j; Grandentier res,tmp; int nb_chif_max = (this->lg > GE.lg) ? this->lg: GE.lg; int nb_chif_min = (this->lg < GE.lg) ? this->lg: GE.lg; for (int i = 0; i < nb_chif_min; i++) {retenue = 0; nb_car = nb_chif_max + 1; int * temp = new int [nb_car+i];// Allocation d'un tableau temporaire for (int l = i; l >= 0; l--) // Ajout des zéros dans les termes de la multiplication {temp[l] = 0;} for (j = 0; j < nb_chif_max; j++) {produit = (this->lg > GE.lg) ? (GE.tab[i] * this->tab[j]) + retenue : (GE.tab[j] * this->tab[i]) + retenue ; if (produit >= 10) // Si le produite est supérieur à 10 { retenue = produit/10;// la retenue reçoit un dixième du produit produit = produit - retenue*10; // le produit reçoit le produit moins dix fois la retenue } else { retenue = 0;} // la retenu reçoit 0 temp[j+i] = produit; // l'élément courant reçoit le produit } if (retenue >= 1) // Si la dernière retenue est supérieure ou égale à 1 {temp[j+i] = retenue; // l'élément courant reçoit la retenue } else { nb_car--;} // on enlève un élément tmp.lg = nb_car+i; delete [] tmp.tab; // Libèration de l'espace mémoire du tableau tmp.tab = new int [tmp.lg]; // Allocation du tableau for (int j = 0 ; j < tmp.lg; j++) {tmp.tab[j] = temp[j];} delete [] temp ; // Libèration de l'espace mémoire du tableau temporaire res = res + tmp; // le résultat reçoit le résultat plus l'élément courant } return res; } // Opérateurs de comparaison bool Grandentier::operator==(const Grandentier &GE) {if (this->negatif != GE.negatif) {return false; } else { if (this->lg == GE.lg) {for (int i = 0; i < this->lg; i++) {if (this->tab[i] != GE.tab[i]) {return false;} } } else {return false;} return true; } } bool Grandentier::operator!=(const Grandentier &GE) {return !(*this == GE);} bool Grandentier::operator<(const Grandentier &GE) {if (this->negatif) {if (this->lg < GE.lg) {return false;} if (this->lg > GE.lg) {return true;} for (int i = this->lg - 1 ; i >= 0 ; i--) { if (this->tab[i] < GE.tab[i]) {return false;} if (this->tab[i] > GE.tab[i]) {return true;} } return false; } else { if (this->lg < GE.lg) {return true;} if (this->lg > GE.lg) {return false;} for (int i = this->lg - 1 ; i >= 0 ; i--) { if (this->tab[i] < GE.tab[i]) {return true;} if (this->tab[i] > GE.tab[i]) {return false;} } return false; } } bool Grandentier::operator<=(const Grandentier &GE) {return ((*this < GE) || (*this == GE));} bool Grandentier::operator>(const Grandentier &GE) {return !(*this <= GE);} bool Grandentier::operator>=(const Grandentier &GE) {return !(*this < GE);} // Convertion d'un Grandentier en "int" int convEnInt (const Grandentier& GE) {int nb = 0;// Déclaration des variables int tmp; for (int i = 0; i < GE.lg; i++) {tmp = 1; // calcul de la puissance courante for (int j = i; j > 0 ; j--) {tmp *= 10;} nb += GE.tab[i] * tmp; // le nombre reçoit l'élément courant fois la puissance } return nb; } // Opérateurs d'entrée-sortie équivalent de cin et cout ostream& operator<< (ostream& sortie, Grandentier &GE) { // Si GE est négatif if (GE.negatif) { // affichage du signe sortie << "-"; } for (int i = GE.lg - 1 ; i >= 0 ; i--) { // affichage des éléments de GE sortie << GE.tab[i]; } return sortie ; } istream& operator>> (istream& entree, Grandentier &GE) { int nb; // Saisie d'un nombre entree >> nb; GE = Grandentier(nb); return entree ; } int main(){ int x,y; int z,t; Grandentier a,b; cout<<"entrer un grand nombre"<> y ; b=Grandentier(y); cout<<"a+b="; z = convEnInt(a+b); cout<