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
fichier .h
#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
#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 principale
#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;
}