Click here to Skip to main content
15,902,635 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've got a bigint class that uses an array called SafeArray that I created in a different class. I couldn't 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 bigint class work fine (we don't have to account for negative integers) except my compare method needs work. I want it to be able to compare two bigints and if the (const bigint and &A) number is larger than the other (cout 1) if it is smaller (cout 2) if they are the same (cout 0). Any help with this method would be greatly appreciated. Thanks

C++
int size = 35; //will get bigger, small now just for testing
class bigint
{
    SafeArray<int> *arr;
public:
    bigint()            //initializes to zero
    {
        arr = new SafeArray<int>;
        for(int i =0;i < size; i++)
            arr->set(i,0);
    }

    void print()                 //prints numbers without zeroes in front
    {
        bool start_num=false;
        for(int i = 0;i <arr->get_size() ;i++)
        {
            if(arr->get(i)!=0 && start_num==false )
            {start_num=true;
                cout << arr->get(i);}
         else if(start_num==true)
             cout<<arr->get(i);                
        }

       cout<<endl;
    }

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

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

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

    void add(const bigint &amp;A)                    //add big ints
    {
        int carry=0;
        for(int i=size-1;i&gt;=0;i--)
           {
               int result = arr-&gt;get(i)+A.arr-&gt;get(i)+carry;
               arr-&gt;set(i,result%10);
               carry=result/10;
           }
    }

    void subtract(const bigint &amp;A)                 //subtract big ints
    {
        int borrow = 0;
        for(int i=size-1; i &gt;= 0; --i)
        {
            int result=((arr-&gt;get(i) - A.arr-&gt;get(i) - borrow));
            if(result &lt; 0)
            {
                arr-&gt;set(i, result + 10);
                borrow = 1;
            }
            else
            {
                arr-&gt;set(i, result);
                borrow = 0;
            }
        }
    }

//int compare(const bigint &amp;A)               //compare big ints
//    {
//
//        for(int i&lt;size;i&gt;0;i--)
//            {
//                if(A.arr-&gt;get(i) &gt; arr-&gt;get(i))
//                    {
//                    return 1;
//                    }
//                else if(A.arr-&gt;get(i) &lt; arr-&gt;get(i))
//                    {
//                        return -1;
//                    }
//                else
//                    {
//                        return 0;
//                    }
//            }
//
//    }

};
int main()
{
    bigint a, b, c;

    a.assign(&quot;12345678&quot;);                             //for testing
    b.assign(&quot;12345678&quot;);
    //a.compare(b);
    a.print();
    c.assign(24691357);        // 696969 is small enough to be an int.
    a.add(b);                // a += b;
    a.subtract(c);           // a -= b;
    a.print();

    return 0;
}</pre>
Posted

Your first mistake is using int as a base type, an array element type. You should use unsigned type, even if you wanted to develop signed big int type, otherwise you are loosing half of value range of the element type. This is especially important for comparison.

Look how you compare two big integers on paper. You have, say, decimal digits with 10 values. You align the recorded numbers to compare the digits representing the same degree of 10. Then you start comparison with the most significant digit. Only if the most significant digit is the same, you move to the least significant digit, one by one.

You do the same with your arrays. You start with most significant end. If you do everything else correctly, your array element represents, say, one digit with 232 or 264 values. You compare with most significant end and stop comparison if two array element are not equal. Then you need to compare other array elements.

—SA
 
Share this answer
 
The error in your compare function is obvious. I just can only repeat the advice given to you in all of previous questions: Use a debugger or step through the code with pencil and paper. You will detect the problem in the very first iteration of the for loop when the two numbers are identical in their high-order digit.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900