Overloaded Split Operator for HugeInt

Possible duplicate:
Department with really big rooms

I need to overload the / operator to work on two HugeInt objects that are defined simply as an array of 30 shorts. This is homework, by the way, but for several days I was looking for my brain in this problem.

I have already overloaded the * operator:

HugeInt HugeInt::operator*(const HugeInt &op2){ HugeInt temp; short placeProducts[hugeIntSize + 1][hugeIntSize] = {0}; short product; int carry = 0; int k, leftSize, rightSize, numOfSumRows; leftSize = getDigitLength(); rightSize = op2.getDigitLength(); if(leftSize <= rightSize) { numOfSumRows = leftSize; for(int i = (hugeIntSize - 1), k = 0; k < numOfSumRows; i--, k++) { for(int j = (hugeIntSize - 1); j >= k; j--) { product = integer[i] * op2.integer[j] + carry; if (product > 9) { carry = product / 10; product %= 10; } else { carry = 0; } placeProducts[k][j - k] = product; } } } else { numOfSumRows = rightSize; for(int i = (hugeIntSize - 1), k = 0; k < numOfSumRows; i--, k++) { for(int j = (hugeIntSize - 1); j >= k; j--) { product = integer[j] * op2.integer[i] + carry; if (product > 9) { carry = product / 10; product %= 10; } else { carry = 0; } placeProducts[k][j - k] = product; } } } sumProductsArray(placeProducts, numOfSumRows); for(int i = 0; i < hugeIntSize; i++) { temp.integer[i] = placeProducts[hugeIntSize][i]; } return temp;} 

But how do I overload / op? My main problem is not C ++ code or syntax, but what my algorithm divides. When I multiply, I can make it a number. I store each product (aka 1 digit of the lower value of each digit above, and then a 10-digit number, each number higher, using my transfer algorithm) in my 2-dimensional array. Each time I get a new product, it shifts to the left by n + 1, which "multiplies" it by the required power of 10. Then I just summarize all the columns.

I canโ€™t understand for my whole life how to code the long separation method. Since I am dealing with two arrays, it should be digit by digit, and I suspect that it can be as simple as reversing the multiplication algorithm. Nested loops and subtraction? Do I need a variable for private and reminder? Is there a better method? I just have to be directed in the right direction.

+4
source share
1 answer

In computational division of integers there are several interesting results:

  • numerator <denominator means quotient = 0
  • numerator == denominator implies quotient = 1
  • numerator> denominator, a long division is required to determine the private sector.

The first two conditions can be satisfied by a for loop. You can overload a relational operator with a smaller and equal ratio to encapsulate this behavior.

For a long division, you will need your multiplication operator, as well as overloaded operators of smaller size and subtraction, as well as the function of adding the number of digits to perform the operation.

This is brute force, but must do its job.

+1
source

Source: https://habr.com/ru/post/1439504/


All Articles