import algds.IOUtils;
/*
 * AlgDs WS99 Aufgabe 12.
 * Berechnet x/y = z Rest r, nur unter Verwendung von
 * +, -, ==, <, if else und Rekursion
 *
 * @author Christian Semrau, 8.11.1999, 24.12.1999<br>
 * <a href="mailto:Christian.Semrau@student.uni-magdeburg.de">
 * Christian.Semrau@student.uni-magdeburg.de</a>
 */
public class ChS_Aufg12 {

static int div(int x, int y) throws ArithmeticException {
  if (y<0)
	return( -div(x,-y) );  // Aufruf von div wegen sgn(x)
  else
  if (y==0)
	throw new ArithmeticException("div("+x+",0) undefined");
  else  // ab hier ist y>0
  if (x<0)
	return( -divi(-x,y) );
  else
	return( divi(x,y) );  // hier ist x>=0
}

static int divi(int x, int y) {  // hier ist x>=0, y>0
  return( (y<=x) ? (divi(x-y, y)+1) : 0 );
}
/**
 * Liefert <code>x/y</code> und <code>x%y</code>.
 * @return int[] <code>={x/y, x%y}</code>
 */
public static int[] divMod(int x, int y) {
  int z = x/y;
  int[] res = {z, x-z*y};
  return res;
}

public static void main(String[] args) {
  int x,y,z;
  System.out.println("AlgDs WS99 Aufgabe 12, Christian Semrau 08.11.1999");
  System.out.println("Berechnet x / y");

  System.out.print("x:"); x = IOUtils.readInt();
  System.out.print("y:"); y = IOUtils.readInt();

  if (y!=0) {
	z = x/y;
	System.out.println("    "+x+"/"+y+" = "+z+" + "+(x-z*y)+"/"+y);
  } else
	System.out.println("    "+x+"/"+y+" = ndef");

  z = div(x,y);
  System.out.println("div("+x+","+y+")= "+z+" + "+(x-z*y)+"/"+y);
} // main()
} // class ChS_Aufg12