GastonFontenla

Solucion Gaston

Nov 11th, 2018
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.48 KB | None | 0 0
  1. #ifndef BIGNUMBER_H_INCLUDED
  2. #define BIGNUMBER_H_INCLUDED
  3.  
  4. #include <iostream>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. class BigNumber
  10. {
  11. private:
  12.     string n;
  13. public:
  14.     BigNumber(string _n); ///Constructor
  15.     string toStr(); ///Devolver string
  16.     BigNumber operator*(BigNumber &b); ///Operador multiplicar
  17. };
  18.  
  19. BigNumber::BigNumber(string _n)
  20. {
  21.     this->n = _n;
  22. }
  23.  
  24. string BigNumber::toStr()
  25. {
  26.     return this->n;
  27. }
  28.  
  29. string multiplicar(string a, string b)
  30. {
  31.     int tam = a.size()+b.size()+10; ///+10 changui
  32.     int res[tam], p;
  33.  
  34.     for(int i=0; i<tam; i++)
  35.         res[i] = 0;
  36.  
  37.     reverse(a.begin(), a.end());
  38.     reverse(b.begin(), b.end());
  39.  
  40.     for(int i=0; i<a.size(); i++)
  41.     {
  42.         for(int j=0; j<b.size(); j++)
  43.         {
  44.             p = (a[i]-'0')*(b[j]-'0');
  45.             res[i+j] += p%10;
  46.             res[i+j+1] += p/10;
  47.         }
  48.     }
  49.  
  50.     for(int i=0; i<tam-1; i++)
  51.     {
  52.         if(res[i] > 9)
  53.         {
  54.             res[i+1] += res[i]/10;
  55.             res[i] %= 10;
  56.         }
  57.     }
  58.  
  59.     int maxPos = tam-1;
  60.     while(maxPos >= 0 && res[maxPos] == 0)
  61.         maxPos--;
  62.  
  63.     string r(maxPos+1, ' ');
  64.     for(int i=maxPos; i>=0; i--)
  65.         r[maxPos-i] = (res[i]+'0');
  66.  
  67.     return r;
  68. }
  69.  
  70. BigNumber BigNumber::operator*(BigNumber &num)
  71. {
  72.     return multiplicar(this->toStr(), num.toStr());
  73. }
  74.  
  75. ostream &operator<<(ostream &out, BigNumber &num)
  76. {
  77.     return out << num.toStr();
  78. }
  79.  
  80. #endif // BIGNUMBER_H_INCLUDED
Advertisement
Add Comment
Please, Sign In to add comment