ostream& BigInt::operator<<(ostream & outs, BigInt& a)
{
for(int i = 0; i < a.size,i++)
{
outs<<a.data[i];
}
return outs;
}
BigInt::BigInt(int s,int d[])
{
size = s;
data = d;
}
//BigInt BigInt::operator*(const &BigInt b)
//{
//}
BigInt BigInt::operator+(const BigInt& b)
{
int* n;
int* m;
if(size > b.size){
n = new int[size];
m = data;
//create a new array full of zeros
for(int i = 0; i < size; i++)
n[i]=0;
for(int i = 1; i < b.size; i++)
{
n[size-i] = b.data[b.size-i];
}
}
if(size < b.size){
n = new int[b.size];
m = b.data;
//create a new array full of zeros
for(int i = 0; i < b.size; i++)
n[i]=0;
for(int i = 1; i < size; i++)
{
n[b.size-i] = b.data[size-i];
}
}
//add n and m
int* newdata;
newdata =new int[m.length + 1];
for(int i = 0; i < newdata.length; i++)
newdata[i]=0;
int carry = 0;
for(int i = m.size -1; i >=0; i--)
{
int x = 0;
x = n[i] + m[i]+ newdata[i];
newdata[i] = x%10;
if(x >= 10)newdata[i-1]= 1;
}
BigInt newBig = new BigInt(newdata.length,newdata);
return newBig;
}
BigInt BigInt::operator-(const BigInt& b)
{
int *m,*n,*newdata;
if(*this > b) // will result in positive number.
{
m[7];
m = new int[size];
n = new int[size];
newdata = new int[size];
if(size = b.size)
{
for(int i = size; i > 0 ; i--)
{
m[i-1]= data[i-1];
n[i-1]= b.data[i-1];
}
}
else
{
for(int i = size; i > 0 ; i--)
{
m[i-1]= data[i-1];
}
for(int i= 0; i < b.size ; i++)
{
n[size-i]= b.data[b.size-i];
}
} // at this point the numbers should be in 2 arrays of the same size.
for(int i = size-1; i>=0;i--) // this loop does the actual subtraction.
{
if(m[i] >= n[i])
{
newdata[i] = m[i] - n[i];
}
else
{
m[i-1] = m[i-1] - 1;
newdata[i] = (m[i] + 10) - n[i];
}
}
}
else // will result in negative number or 0.
{
m = new int[b.size];
n = new int[b.size];
newdata = new int[size];
for(int i= 0; i < size ; i++)
{
n[size-i]= data[size-i];
}
for(int i= 0; i < b.size ; i++)
{
m[size-i]= b.data[b.size-i];
}
for(int i = b.size-1; i>=0;i--) // this loop does the actual subtraction.
{
if(m[i] >= n[i])
{
newdata[i] = m[i] - n[i];
}
else
{
m[i-1] = m[i-1] - 1;
newdata[i] = (m[i] + 10) - n[i];
}
}
newdata[0] = newdata[0] * (-1);
}
return BigInt(newdata.length,newdata);
}
int main()
{
int s;
int *d;
cout<<"enter a size\n";
cin>> s;
char input[s];
cout<<"Enter a big integer.\n";
cin>> input;
d = new int[s];
for(int i = 0; i < s; i++)
{
d[i] = input[i];
}
BigInt *a = new BigInt(s,d);
}