Home java division in the column on java

division in the column on java

Author

Date

Category

You need to write a class that would divide one number to another, withdrawing division into a column. The division is integer. If the residue remains, simply outputting it in the last line if it remains not to delete it. An example of the result that must come back:

My code:

Public Class Longdivutil {
  Private String Result = "";
  Private INT Remdiv = 0;
  Private INT QUOTIENT;
  Private int dividend;
  Private Int Divider;
  Private int [] numbersdividend;
  Private String [] NumbersofdividendInstrval;
  Public Longdivutil (Int Dividend, Int Divider) {
    this.dividend = dividend;
    this.divider = Divider;
    this.NumbersOFDIVIDENDINSTRVAL = (Dividend + ") .split (" ");
    this.numbersdividend = new int [numbersoffdividendinstrval.length];
    For (int i = 0; i & lt; numbersoffdividendinstrval.length; i ++) {
      numbersdividend [i] = integer.paraseint (numbersofdividendinstrval [i]);
    }
  }
  Public void executedivision () {
    System.out.PrintLN ("Execute");
    System.out.PrintLN (this.dividend + "|" + this.divider);
    //////////////////////////////////////// // ///////////////
    INT TMPINT = NUMBERSDIVIDEND [0];
    boolean iscontinue = false;
    for (int i = 0; i & lt; numbersofdividendinstrval.length || iscontinue; i ++) {
      If (TMPINT & GT; = Divider) {
        QUOTIENT = TMPINT / DIVIDER;
        Result + = QUotient;
        REMDIV = TMPINT% DIVIDER;
        iscontinue = false;
        System.out.PrintLN ("Result =" + Result); ////////////////
        System.Out.printLN ("REMDIV =" + REMDIV); //////////////
        if (Remdiv! = 0 & amp; & amp; i! = numbersofdividendinstrval.length - 1) {// 1
          tmpint = integer.paraseint (((Remdiv + ") + (numbersdividend [i] +" "));
          System.out.printLN (TMPINT);
          Continue;
        }
        if (i & lt; numbersofdividendinstrval.length -1) {// 2
          TMPINT = NUMBERSDIVIDEND [++ I];
          Iscontinue = True;
        }
        Continue;
      } else {
        if (tmpint == 0) {// 4
          Result + = TMPint;
          If (I & LT; NumbersofdividendInstrval.Length) {
            TMPINT = I;
          }
          iscontinue = false;
          Continue;
        }
        If (TMPINT & LT; Divider) {
          tmpint = integer.paraseint (numbersofdividendinstrval [i] + numbersofdividendinstrval [++ i]);
          Iscontinue = True;
          Continue;
        }
      }
    }
    //////////////////////////////////////// // ///////////
    System.out.PrintLN ("Out");
  }
}

In Neta, often the question, but did not find anything in the recommendations. While I am writing myself, but it turns out, to put it mildly, not very. Who faced with this?


Answer 1, Authority 100%

The code should be corrected, but the example represented shows the basic principles that must be implemented.

Public Class Longdivutil {
Private int dividend;
Private Int Divider;
Private int n;
Private StringBuffer DividendSB;
Private StringBuffer Result = New StringBuffer ("");
Private StringBuffer FirstsplitedString;
Private StringBuffer SecondsplitedString;
Private StringBuffer Print = New StringBuffer ("");
Public Longdivutil (Int Dividend, Int Divider) {
  this.dividend = dividend;
  this.divider = Divider;
  result = new stringbuffer ("");
  this.dividendsb = new stringbuffer (integer.tostring (this.dividend));
}
Public Void PrintsomeCharsometimes (String S, Int N) {
  For (int i = 0; i & lt; n; i ++) {
    print.append (s);
  }
}
Public StringBuffer ReturnResult () { 
print.append (dividendSB + "|" + divider + "\ n");
  printSomeCharSomeTimes ("", dividendSB.length ());
  printSomeCharSomeTimes ("-", Integer.toString (divider) .length () + 1);
  StringBuffer t = new StringBuffer ("");
  for (int i = 0; this.dividend & gt; = this.divider; i ++) {
    print.append ("\ n" + t.toString () + this.getLeftDividendNumber ());
    count ();
    print.append ("\ n" + t.toString () + n * divider);
    t.append ("");
  }
  if (this.dividend! = 0)
    result.append (".");
  int numberOfDigits = 5;
  while (this.dividend! = 0 & amp; & amp; numberOfDigits! = 0) {
    for (int i = 0; dividend & lt; divider; i ++) {
      dividend = Integer.parseInt (dividendSB.append ("0"). toString ());
      if (i & gt; 0) {
        result.append ("0");
      }
    }
    count ();
    numberOfDigits--;
  }
return result;
}
public int getLeftDividendNumber () {
  int i = 1;
  while (Integer.parseInt (Integer.toString (this.dividend) .substring (0, i)) & lt; this.divider) {
    i ++;
  }
  this.firstSplitedString = new StringBuffer (Integer.toString (this.dividend) .substring (0, i));
  this.secondSplitedString = new StringBuffer (Integer.toString (this.dividend) .substring (i, Integer.toString (this.dividend) .length ()));
  return Integer.parseInt (this.firstSplitedString.toString ());
}
public void count () {
  this.n = this.getLeftDividendNumber () / divider;
  result.append (n);
  this.dividendSB = new StringBuffer (Integer.toString (this.getLeftDividendNumber ()% divider));
  this.dividendSB.append (this.secondSplitedString);
  dividend = Integer.parseInt (this.dividendSB.toString ());
}
public static void main (String [] args) {
LongDivUtil l = new LongDivUtil (234678,356);
  l.returnResult ();
  System.out.println (l.print.toString ());
}
}

In the example, I output only the result of integer division.

234678 | 356
   ----
2346
2136
 2107
 1780
 3278
 3204

Answer 2, authority 91%

The point is that the class first implements the division pipeline. Each stage is added to the result line. Then the line is modified to display a nice long division.
At the input of the main method, there are two numbers (dividend and divisor), at the output we get a line – a full drawing of the division into a column. The method is easy to test.

public class Division {
  private StringBuilder result = new StringBuilder ();
  private StringBuilder quotient = new StringBuilder ();
  private StringBuilder reminder = new StringBuilder ();
  public String makeDivision (int dividend, int divisor) {
    if (divisor == 0) {
      throw new IllegalArgumentException ("Divisor cannot be 0, division by zero");
    }
    dividend = Math.abs (dividend);
    divisor = Math.abs (divisor);
    if (dividend & lt; divisor) {
      return "" + dividend + "/" + divisor + "= 0";
    }
    String [] digits = String.valueOf (dividend) .split ("");
    Integer reminderNumber;
    Integer multiplyResult;
    Integer divisorDigit = calculateDigit (divisor);
    Integer mod;
    for (int i = 0; i & lt; digits.length; i ++) {
      reminder.append (digits [i]);
      reminderNumber = Integer.parseInt (reminder.toString ());
      if (reminderNumber & gt; = divisor) {
        mod = reminderNumber% divisor;
        multiplyResult = reminderNumber / divisor * divisor;
        String lastReminder = String.format ("%" + (i + 2) + "s", "_" + reminderNumber.toString ());
        result.append (lastReminder) .append ("\ n");
        String multiply = String.format ("%" + (i + 2) + "d", multiplyResult);
        result.append (multiply) .append ("\ n");
        Integer tab = lastReminder.length () - calculateDigit (multiplyResult);
        result.append (makeDivider (reminderNumber, tab)). append ("\ n");
        quotient.append (reminderNumber / divisor);
        reminder.replace (0, reminder.length (), mod.toString ());
        reminderNumber = Integer.parseInt (reminder.toString ());
      } else { 
If (I & GT; = Divisordigit) {
          QUOTIENT.APPEND (0);
        }
      }
      if (i == digits.length - 1) {
        result.append (string.format ("%" + (i + 2) + "s", remindernumber.tostring ())). Append ("\ n");
      }
    }
    ModifyResulttoview (Dividend, Divisor);
    Return result.tostring ();
  }
  Private String MakedIvider (Integer ReminderNumber, Integer Tab) {
    Return AssemblyString (Tab, '') + Assemblystring (CalculateDigit (ReminderNumber), '-');
  }
  Private Void ModifyResulttoview (Integer Dividend, Integer Divisor) {
    int [] index = new int [3];
    for (int i = 0, j = 0; i & lt; 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 (Int i) {
    Return (int) math.log10 (i) + 1;
  }
  Private String AssemblyString (Int Numberofsymbols, Char Symbol) {
    StringBuilder String = New StringBuilder ();
    For (int i = 0; i & lt; numberofsymbols; i ++) {
      String.append (Symbol);
    }
    Return string.tostring ();
  }
}

Example of result:

_ 10210│5
 10 │ ----
 - │2042.
 _21
  twenty
  -
  _ten
  ten
  -
   0.

Test example:

Division Division = New Division ();
  @Test
  Public Void ShouldMakedIVision () {
    String Expected = "_14789│20 \ n" +
              "140 │ --- \ n" +
              "--- │739 \ n" +
              "_78 \ n" +
              "60 \ n" +
              "- \ n" +
              "_189 \ n" +
              "180 \ n" +
              "--- \ n" +
              "9 \ n";
    AssertEquals (EXPECTED, DIVISION.MAKEDIVITION (14789, 20));
  }

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions