Nines compliment

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


CPP file

#include <iostream>
#include <cstring>
#include <fstream>
#include <cctype>
#include "big.h"
using namespace std;

bigint::bigint(){
digit = NULL;
size=0;
}

bigint::~bigint(){
}


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];

}
else {
c=x.size;
for(b=0;b<x.size;b++)
digit[b]=9-digit[b];
}

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 &y )
{

bigint temp;
temp.findsize(y.size+size+1);
int j,tmp,carry,m;
for (int i=0; i<y.size; i++)
{
bigint temp2;
temp2.findsize(y.size+size+1);
carry=0;
m=0;
for (j=0; j< size; j++)
{
tmp = digit[ j ] * y.digit[i] + carry;
temp2.digit[j+i] =tmp%10;
carry=tmp/10;
}
temp2.digit[i+j]=carry;
temp=temp+temp2;
}
return temp;
}

int main()
{
bigint a1,a2,answer;
a1.Input();
a2.Input();
a1.Output();
cout <<" + ";
a2.Output();
answer=a1+a2;
cout<< " = " ;
answer.Output();
cout << "\n\n";
a1.Output();
cout <<" - ";
a2.Output();
answer=a1-a2;
cout<< " = " ;
answer.Output();
cout << "\n\n";
a1.Output();
cout <<" * ";
a2.Output();
answer=a1*a2;
cout<< " = " ;
answer.Output();
cout << "\n\n";
int x;
cin>>x;
return 0;
}
};
Topic archived. No new replies allowed.