简体   繁体   English

C ++中的Bigint减法

[英]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. 在这里,我有一个bigint类,它使用一个名为SafeArray的数组,该数组是在另一个类中创建的(我们不能使用向量),set和get函数调用来自SafeArray类,get接受一个int参数作为数组位置,set接受2个int参数(一个用于位置,一个用于值)在该类中的所有方法都可以正常工作,除了我的减法和比较法。 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);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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