Well I am doing the BigInt Calculator project and I am having some issues with my subtraction. Basically I get the 9s complement of the smaller number and add it to the bigger one but having trouble erasing the extra 1 from the front and adding it to the end. Example: 2111+0123=2111+9876=1(1987) --> that '1' in the front and adding it to the end. Also when I implement the array[b]=9-array[b], it screws up the output of my multiplication function. I cant really explain it but when I take out the array[b]=9-array[b] my multiplication function works perfectly fine. Lets say I input Input1=2111, Input2=10 -> Output=2111, Output2=89 and it will multiply 2111*89 rather than 2111*10. Any help is appreciated. Here is the code.
Header:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
class bigint
{
private:
int *digit;
int size;
public:
bigint();
~bigint();
void Input(); // converts user input
void Output(); // prints the users input
int findsize(int);;
bigint operator+(bigint &); // + operator
bigint operator-(bigint &); // - operator
bigint operator*(bigint &); // * operator
int bigint::findsize(int s){
int i=0;
if (digit!=NULL) delete []digit;
size=s;
digit=new int[size];
while(i<size){
digit[i]=0;
i++;
}
return size;
}
void bigint::Input()
{
string in;
int j,k;
cout<<"Enter a number\n";
cin>>in;
findsize(in.length());
k=0;
for(j=size-1;j>=0;j--)
digit[j]=in[k++]-48;
}
void bigint::Output()
{
int i;
for (i=size-1;i >= 0; i-- )
cout<<digit[i];
}
bigint bigint::operator+( bigint &x )
{
bigint temp;
int carry = 0;
int c,i;
if(size>x.size){
c=size;
for( int i = x.size; i < size; i++)
x.digit[i] = 0;
}
else {
c=x.size;
for( int i = size; i < x.size; i++)
digit[i] = 0;
}
temp.findsize(c+1);
for ( i=0; i<c; i++ ){
temp.digit[ i ] = digit[ i ] + x.digit[ i ] + carry;
if ( temp.digit[ i ] > 9 )
{
temp.digit[ i ] %= 10;
carry = 1;
}
else
carry = 0;
}
if(carry==1)
{
temp.size=c+1;
temp.digit[i]=carry;
}
else
temp.size=c;
return temp;
}
bigint bigint::operator-( bigint &x )
{
bigint temp;
int carry=0;
int c,b,i;
if(size>x.size){
c=size;
for(b=0;b<size;b++)// if i take out this for loop, my multiplication function turns out fine.
x.digit[b]=9-x.digit[b];