[Langage] Manipulation de nombres
salut tout le monde, je suis entrain de developper un programme qui calcule le produit de deux grands nombres,
pour cela j'ai pas travaillé avec des int mais plutot avec des string (chaine de caraccteres).
je pense qu'au niveau de conception y a pas de probleme .
alors mon probleme se manifest au cas où je tape un grand nombre pour le premier argument (exp 222222) le programme sort son afficher acun resultat.
voila mon code
gichier .h
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#ifndef _BIGINT_
#define _BIGINT_
#include <string>
#include <iostream>
using namespace std;
class BigInt {
public:
string number; // variable de type string pour maniupler les grands chiffres
BigInt(){}; // constructeur par defaut
BigInt(string s); // constructeur aves un argument de type string
friend ostream& operator<<(ostream&, const BigInt&); // surcharge de l'operateur " << "
friend istream& operator>>(istream&, BigInt&); // surcharge de l'operateur " << "
friend BigInt operator +(BigInt bUn , BigInt bDeux); // surcharge de l'operateur " + "
friend BigInt operator -(BigInt bUn , BigInt bDeux); // surcharge de l'operateur " - "
friend BigInt calculerMulti(BigInt , BigInt); // fonction amie pour calculer la multiplication
int getnumber(string); // fonction membre qui retourne le nombre de chiffre dans le nombre
};
#endif |
fichier .cpp
Code:
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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
#include <iostream>
#include <stdlib.h>
#include "bigInt.h"
#include <sstream>
#include <conio.h>
BigInt::BigInt(string s){
number = s ;
}
ostream& operator <<(ostream& out, const BigInt& b){
out << b.number ;
return out;
}
istream& operator >>(istream& in, BigInt& b){
in >> b.number;
return in;
}
BigInt operator +(BigInt bUn,BigInt bDeux){
BigInt resultatF;
int TbUn,TbDeux,retenu=0,compteur=0,aide1,aide2,resultat;
string s1,s2;
s1 = bUn.number;
s2 = bDeux.number;
TbUn = s1.length();
TbDeux = s2.length();
// créer un flux de sortie
ostringstream oss;
if (TbUn>TbDeux)
{
for(int r=0; r<(TbUn-TbDeux); r++){
s2 = "0" + s2;
}
}
else
{
for(int r=0; r<(TbDeux-TbUn); r++){
s1 = "0" + s1;
}
}
TbUn = s1.length();
char *tab1 = (char *)s1.c_str();
char *tab2 = (char *)s2.c_str();
int tabResultat[TbUn];
for(int j=TbUn-1; j>=0; j--)
{
aide1 = (int)(tab1[j]-'0');
aide2 = (int)(tab2[j]-'0');
resultat = aide1 + aide2 + retenu;
if (resultat>=10 && j!=0)
{resultat = resultat-10;retenu = 1;}
else retenu = 0;
tabResultat[compteur++]= resultat;
}
for(int j=TbUn-1; j>=0; j--)
{oss << tabResultat[j];}
resultatF.number = oss.str();
return(resultatF);
}
BigInt operator -(BigInt bUn , BigInt bDeux){
BigInt resultatF;
int TbUn,TbDeux,retenu=0,compteur=0,aide1,aide2,resultat;
string s1,s2;
bool TenZero;
s1 = bUn.number;
s2 = bDeux.number;
TbUn = s1.length();
TbDeux = s2.length();
ostringstream oss;
if (TbUn>TbDeux)
{
for(int r=0; r<(TbUn-TbDeux); r++){
s2 = "0" + s2;
}
}
else
{
for(int r=0; r<(TbDeux-TbUn); r++){
s1 = "0" + s1;
}
}
TbUn = s1.length();
char *tab1 = (char *)s1.c_str();
char *tab2 = (char *)s2.c_str();
int tabResultat[TbUn];
for(int j=TbUn-1; j>=0; j--)
{
aide1 = (int)(tab1[j]-'0');
aide2 = (int)(tab2[j]-'0');
aide2 = aide2 + retenu;
if (aide1<aide2 )
{retenu = 1; resultat = (aide1+10) - aide2;}
else
{retenu = 0; resultat = aide1 - aide2;}
tabResultat[compteur++]= resultat;
}
for(int j=TbUn-1; j>=0; j--)
{
if (tabResultat[j]!=0)
{
TenZero = false;
break;
}
else
{
TenZero = true;
}
}
for(int j=TbUn-1; j>=0; j--)
{oss << tabResultat[j];}
if (TenZero == false)
{
resultatF.number = oss.str();
}
else
{
resultatF.number = "0";
}
return(resultatF);
}
BigInt calculerMulti(BigInt bUn,BigInt bDeux){
BigInt tempAide("1");
BigInt aideCdeB("0");
int nombreUn,nombreDeux;
istringstream issUn(bUn.number);
istringstream issDeux(bDeux.number);
issUn >> nombreUn;
issDeux >> nombreDeux;
//if(bUn.number=="0" || bDeux.number=="0"){return aideCdeB;}
if(nombreUn==0 || nombreDeux==0){return aideCdeB;}
else{return (bDeux+calculerMulti(bUn-tempAide,bDeux));}
}
int getnumber(string t){
return t.length();
} |
programme principal
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#include <cstdlib>
#include <iostream>
#include <conio.h>
#include <string>
#include "bigInt.h"
using namespace std;
int main()
{
BigInt nbrUn;
BigInt nbrDeux;
BigInt n;
cout << "Tapez SVP le premier nombre = ";
cin >> nbrUn;
cout <<"Tapez SVP le dexieme nombre = ";
cin >> nbrDeux;
cout << calculerMulti(nbrUn,nbrDeux)<<endl;
system("PAUSE");system("PAUSE");system("PAUSE");system("PAUSE");
return EXIT_SUCCESS;
} |