/* Problem 1--Long Division
   This problem is solved by emulating the long division algorithm
   learned in grade school days.  In other words, you compute the
   quotient one digit at a time and halt when you reach a remainder of
   zero or a remainder previously computed (indicating a repeating
   decimal). */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX 10000

int main (int argc, char **argv);
void Process (int n, int d);

FILE *in, *out;

int main (int argc, char **argv) {

  int n, d;

 in = fopen ("prob1.in","r");
 out = fopen ("prob1.out","w");
 while (1) {
  fscanf (in,"%d %d",&n,&d); /* get numerator and denominator */
  if (d==0) break;
  Process (n,d);
 }
 fclose (in);
 fclose (out);
 return EXIT_SUCCESS;
}

/* Process does the division and prints out the actual remainder */

void Process (int n, int d) {

  char A[MAX], O[2*MAX]; /* A is the initial decimal string.  O is what
                            will be output. */
  int R[MAX] = {0}, rem = n%d, ct=0;  /* The initial remainder is n%d*/

 while (rem > 0 && !R[rem]) { /* We bring down a zero and compute the */
  A[ct++] = '0' + 10*rem/d;   /* next digit, logging the remainder, */
  R[rem] = ct;                /* until zero is hit or a previous */
  rem = 10*rem%d;             /* remainder is found. */
 }
 A[ct] = 0;
 sprintf (O,"%d/%d = %d",n,d,n/d); /* The first part of the output */
 if (n%d != 0) { /* It didn't come out even */
  strcat (O,".");
  if (rem==0)
   strcat (O,A);  /* A terminating decimal */
  else {
   strncat (O,A,R[rem]-1);  /* Repeating decimal */
   strcat (O,"(");
   strcat (O,&A[R[rem]-1]);
   strcat (O,")");
  }
 }
 for (ct = 0; O[ct] > 0; ct++) { /* Printing output 50 chars at a time*/
  if (ct > 0 && ct%50==0) fprintf (out,"\n");
  fprintf (out,"%c",O[ct]);
 }
 fprintf (out,"\n\n");
}
