G
G
gulushki2020-09-09 16:33:55
Java
gulushki, 2020-09-09 16:33:55

How does the program calculate intermediate values ​​(long division)?

I need to write a program that performs long division. There is a ready-made code, I found it on the forum, but I don’t understand the arithmetic that happens there. How exactly does the program calculate intermediate values ​​(subtraction) in a column? Namely, how does it blow the difference to the bottom line and the remaining digits of the dividend? Help me figure this out:

public class Division {
private StringBuilder result = new StringBuilder();
private StringBuilder quotient = new StringBuilder();
private StringBuilder remainder = new StringBuilder();

public String makeDivision(Integer dividend, Integer divisor) {

    if (divisor == 0) {
        throw new IllegalArgumentException("Can not divide by zero");
    }

    dividend = Math.abs(dividend);
    divisor = Math.abs(divisor);

    String oneByOne = "";
    String[] digits = String.valueOf(dividend).split(oneByOne);
    Integer remainderNumber;
    Integer multiplyResult;
    Integer divisorDigit = calculateDigit(divisor);
    Integer mod;

    for (int i = 0; i < digits.length; i++) {
        remainder.append(digits[i]);
        remainderNumber = Integer.parseInt(remainder.toString());

        if (remainderNumber >= divisor) {
            mod = remainderNumber % divisor;
            multiplyResult = remainderNumber / divisor * divisor;

            String lastRemainder = String.format("%" + (i + 2) + "s", "_" + remainderNumber.toString());
            result.append(lastRemainder).append("\n");

            String multiply = String.format("%" + (i + 2) + "d", multiplyResult);
            result.append(multiply).append("\n");

            Integer tab = lastRemainder.length() - calculateDigit(multiplyResult);
            result.append(makeDivisor(remainderNumber, tab)).append("\n");

            quotient.append(remainderNumber / divisor);

            remainder.replace(0, remainder.length(), mod.toString());
            remainderNumber = Integer.parseInt(remainder.toString());
        } else {
            if (i >= divisorDigit) {
                quotient.append(0);
            }
        }

        if (i == digits.length - 1) {
            result.append(String.format("%" + (i + 2) + "s", remainderNumber.toString())).append("\n");
        }
    }
    modifyResultToView(dividend, divisor);
    return result.toString();
}

private String makeDivisor(Integer remainderNumber, Integer tab) {
    return assemblyString(tab, ' ') + assemblyString(calculateDigit(remainderNumber), '-');
}

private void modifyResultToView(Integer dividend, Integer divisor) {
    int[] index = new int[3];
    for (int i = 0, j = 0; i < result.length(); i++) {
        if (result.charAt(i) == '\n') {
            index[j] = i;
            j++;
        }
        if (j == 3) {
            break;
        }
    }
    int tab = calculateDigit(dividend) + 1 - index[0];
    result.insert(index[2], assemblyString(tab, ' ') + "│" + quotient.toString());
    result.insert(index[1], assemblyString(tab, ' ') + "│" + assemblyString(quotient.length(), '-'));
    result.insert(index[0], "│" + divisor);
    result.replace(1, index[0], dividend.toString());
}

private int calculateDigit(Integer algebra) {
    return (int) Math.log10(algebra) + 1;
}

private String assemblyString(Integer numberOfSymbols, char symbol) {
    StringBuilder string = new StringBuilder();
    for (int i = 0; i < numberOfSymbols; i++) {
        string.append(symbol);
    }
    return string.toString();
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question