简体   繁体   中英

Bigint Subtraction in C++

Here I have a bigint class that uses an array called SafeArray that I created in a different class(we couldnt use vectors) the set and get function calls are from the SafeArray class, get takes an int parameter for array position and set takes 2 int parameters(one for position and one for value) all methods in this class work fine except for my subtract and compare methods. What I really need to work is my subtraction method. Any help? Thanks

 int size = 20;   //just for testing, will increase later
class bigint
{
    SafeArray<int> *arr;
public:
bigint()
{
    arr = new SafeArray<int>;
    for(int i =0;i < size; i++)
        arr->set(i,0);
}

void print()
{
    for(int i = 0;i <arr->get_size() ;i++)
    {

        cout << arr->get(i);

    }
   cout<<endl;
}

void assign(const bigint &A)
{
    for(int i=0;i<arr->get_size();i++)
    {
        arr->set(i,A.arr->get(i));
    }

}

    void assign(int num)
{
    for(int i = arr->get_size()- 1; i >= 0; i--)
    {
        arr->set(i,num%10);
        num /=10;
    }


}

void assign(string num)
{
    long len = num.length();
    int j=arr->get_size()-1;
    for(long i=len-1;i>=0;i--)
    {
        arr->set(j,num[i]-48);
        j--;
    }
}

void add(const bigint &A)
    {

        int carry=0;
        for(int i=size-1;i>=0;i--)
       {
           int result = arr->get(i)+A.arr->get(i)+carry;
           arr->set(i,result%10);
           carry=result/10;}

        }

void subtract(const bigint &A) {
    for(int i=0, borrow=0; i<size; ++i)

    {    int result=((arr->get(i) - A.arr->get(i) + borrow));
                     if(borrow == result < 0) {
            A.arr->set(i,result+=10);
        }   }   }

void compare(const bigint & A)
{
   //int comp;
   //for(int i =0;i<size;i++)
   if(arr->get(size-1)>A.arr->get(size-1))
        cout<<0;

      else
         cout<<1;

 }
};
int main()
{
 bigint A,B,C,D;
 A.assign(12345);
 A.print();
 cout<<endl;
 C.assign("123456789000");
 C.print();
 cout<<endl;
 B.add(C);
 B.print();
 //B.compare(A);
 //B.subtract(A);
 //B.print();



return 0;

}

I think it should look like this

void subtract(const bigint &A) {
    int borrow = 0;
    for(int i=size-1; i >= 0; --i)
    {
        int result=((arr->get(i) - A.arr->get(i) - borrow));
        if(result < 0) {
            arr->set(i, result + 10);
            borrow = 1;
        } else {
            arr->set(i, result);
            borrow = 0;
        }
    }
}
// equals A
bool equals(const bigint &A) {
    for(int i=0; i < size; ++i) {
        if(A.arr->get(i) != arr->get(i)) {
            return false;
        }
    }
    return true;
}
// less than A
bool lt(const bigint &A) {
    for(int i=0; i < size; ++i) {
        if(arr->get(i) != A.arr->get(i)) {
            return arr->get(i) < A.arr->get(i);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM