Pagini recente » Cod sursa (job #259416) | Cod sursa (job #1454902) | Cod sursa (job #2338126) | Cod sursa (job #2618992) | Cod sursa (job #3130962)
#include <bits/stdc++.h>
using namespace std;
const int MAX_DIGITS = 55;
const int BASE = 10;
class HugeN
{
private:
int x[MAX_DIGITS];
public:
//constructori
HugeN()
{
memset(x,0,sizeof(x));
x[0] = 1;
}
HugeN(string s)
{
memset(x,0,sizeof(x));
x[0] = s.size();
int cnt = 0;
for(int i = s.size() - 1; i >= 0 ;i--)
x[++cnt] = s[i] - '0';
}
HugeN( int n)
{
memset(x,0,sizeof(x));
x[0] = 0;
do
{
x[++x[0]] = n%10;
n = n/10;
}
while(n);
}
HugeN(const HugeN& other)
{
memcpy(x,other.x,sizeof(other.x));
}
void print()
{
for( int i = x[0]; i >= 1; i--)
cout << x[i];
cout << "\n";
}
// compararea a doua HugeN
int cmp(const HugeN& other)
{
// x ? other
if (x[0] < other.x[0]) return -1;
else if (x[0] > other.x[0]) return 1;
for ( int i = x[0] ; i > 0 ; i--)
if ( x[i] < other.x[i]) return -1;
else if ( x[i] > other.x[i]) return 1;
return 0;
}
//supraincarcarea operatorilor
bool operator < (const HugeN& other );
bool operator <= (const HugeN& other );
bool operator > (const HugeN& other );
bool operator >= (const HugeN& other );
bool operator == (const HugeN& other );
bool operator != (const HugeN& other );
HugeN operator +(const HugeN& other );
HugeN operator -(const HugeN& other );
HugeN operator /( HugeN& other );
HugeN operator *(int k);
HugeN operator / (int k);
HugeN operator +=(const HugeN& other );
HugeN operator -=( HugeN& other );
HugeN operator *(const HugeN& other );
HugeN operator *=(int k);
HugeN operator /=(int k);
int operator %(int k);
HugeN operator <<= (int k); // x = x * 10^k
HugeN operator >>=(int k); // x = x / 10^k
};
inline bool HugeN:: operator < (const HugeN& other )
{
// x < other.x
if ((*this).cmp(other) == -1)
return 1;
return 0;
}
inline bool HugeN:: operator <= (const HugeN& other )
{
// x <= other.x
if ((*this).cmp(other) <=0)
return 1;
return 0;
}
inline bool HugeN:: operator > (const HugeN& other )
{
// x > other.x
if ((*this).cmp(other) == 1)
return 1;
return 0;
}
inline bool HugeN:: operator >= (const HugeN& other )
{
// x >= other.x
if ((*this).cmp(other) >= 0)
return 1;
return 0;
}
inline bool HugeN:: operator == (const HugeN& other )
{
// x == other.x
if ((*this).cmp(other) == 0)
return 1;
return 0;
}
inline bool HugeN:: operator != (const HugeN& other )
{
// x != other.x
if ((*this).cmp(other) != 0)
return 1;
return 0;
}
HugeN HugeN:: operator +(const HugeN& other )
{
// c = a + b; c = x + other.x
int i,t;
HugeN c;
c.x[0] = x[0] > other.x[0] ? x[0] : other.x[0];
for ( t=0, i = 1; i <= c.x[0]; i++)
{
t =x[i]+other.x[i]+t;
c.x[i] = t % 10;
t /= 10;
}
if (t)
c.x[0]++, c.x[c.x[0]] = t;
return c;
}
HugeN HugeN:: operator -=( HugeN& other )
{
int t = 0;
for(int i = other.x[0] + 1; i <= x[0]; i++)
other.x[i] = 0;
for(int i = 1; i <= x[0]; ++i)
{
x[i] = x[i] - (other.x[i] + t);
if(x[i] < 0)
t = 1;
else
t = 0;
if(t)
x[i] += 10;
}
while(!x[x[0]] && x[0] > 1)
x[0]--;
return *this;
}
HugeN HugeN::operator *=(int k)
{
// x = x * k
int r = 0, i;
for( i = 1; i<=x[0] ; i++)
{
r=r+x[i]*k;
x[i]=r%10;
r=r/10;
}
while(r>0)
{
x[++x[0]]=r%10;
r=r/10;
}
return *this;
}
HugeN HugeN:: operator += (const HugeN& other )
{
// a = a + b; x = x + other.x
int i,t;
//x[0] = x[0] > other.x[0] ? x[0] : other.x[0];
x[0] = max(x[0],other.x[0]);
for ( t=0, i = 1; i <= x[0]; i++)
{
t =x[i]+other.x[i]+t;
x[i] = t % 10;
t /= 10;
}
if (t)
x[0]++, x[x[0]] = t;
return *this;
}
HugeN HugeN::operator /=(int k)
{
// x = x / k
int r = 0, i;
for( i = x[0]; i>0 ; i--)
{
r = r * 10 + x[i];
x[i] = r / k;
r = r % k;
}
while(x[0] > 1 && !x[x[0]])
--x[0];
return *this;
}
int HugeN::operator %(int k)
{
// r = x % k
int r = 0, i;
for( i = x[0]; i>0 ; i--)
{
r = r * 10 + x[i];
r %= k;
}
return r;
}
HugeN HugeN::operator * (const HugeN& other )
{
HugeN c;
c.x[0] = x[0] + other.x[0] - 1;
int i,j,t;
for ( i = 1; i<=x[0]; ++i)
for( j = 1; j <= other.x[0]; ++j)
c.x[i+j-1] += x[i]*other.x[j];
t = 0;
for ( i = 1;i<=c.x[0]; ++i)
{
t = t + c.x[i];
c.x[i] = t % BASE;
t = t / BASE;
}
int l = c.x[0];
while (t)
{
l++; c.x[l] = t%BASE; t/=BASE;
}
c.x[0]=l;
return c;
}
HugeN HugeN:: operator / ( HugeN& other ) /// catul
{
HugeN r;
HugeN c;
c.x[0] = x[0];
for(int i = x[0];i; i--)
{
r *= 10;
r.x[1] = x[i];
c.x[i] = 0;
while(other <= r)
{
c.x[i]++;
r -= other;
}
}
while(!c.x[c.x[0]] && c.x[0] > 1)
c.x[0]--;
return c; /// modifica r daca vrei restul
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
string aa, bb;
cin >> aa;
cin >> bb;
HugeN a(aa);
HugeN b(bb);
a = a + b;
a.print();
return 0;}