Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef BIGNUMBER_H_INCLUDED
- #define BIGNUMBER_H_INCLUDED
- #include <iostream>
- #include <algorithm>
- using namespace std;
- class BigNumber
- {
- private:
- string n;
- public:
- BigNumber(string _n); ///Constructor
- string toStr(); ///Devolver string
- BigNumber operator*(BigNumber &b); ///Operador multiplicar
- };
- BigNumber::BigNumber(string _n)
- {
- this->n = _n;
- }
- string BigNumber::toStr()
- {
- return this->n;
- }
- string multiplicar(string a, string b)
- {
- int tam = a.size()+b.size()+10; ///+10 changui
- int res[tam], p;
- for(int i=0; i<tam; i++)
- res[i] = 0;
- reverse(a.begin(), a.end());
- reverse(b.begin(), b.end());
- for(int i=0; i<a.size(); i++)
- {
- for(int j=0; j<b.size(); j++)
- {
- p = (a[i]-'0')*(b[j]-'0');
- res[i+j] += p%10;
- res[i+j+1] += p/10;
- }
- }
- for(int i=0; i<tam-1; i++)
- {
- if(res[i] > 9)
- {
- res[i+1] += res[i]/10;
- res[i] %= 10;
- }
- }
- int maxPos = tam-1;
- while(maxPos >= 0 && res[maxPos] == 0)
- maxPos--;
- string r(maxPos+1, ' ');
- for(int i=maxPos; i>=0; i--)
- r[maxPos-i] = (res[i]+'0');
- return r;
- }
- BigNumber BigNumber::operator*(BigNumber &num)
- {
- return multiplicar(this->toStr(), num.toStr());
- }
- ostream &operator<<(ostream &out, BigNumber &num)
- {
- return out << num.toStr();
- }
- #endif // BIGNUMBER_H_INCLUDED
Advertisement
Add Comment
Please, Sign In to add comment