home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 379b.lha / p2c_v1.13a / examples / c / e.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-03  |  2.5 KB  |  163 lines

  1. /* Output from p2c, the Pascal-to-C translator */
  2. /* From input file "dist/examples/e.p" */
  3.  
  4.  
  5. #include <p2c/p2c.h>
  6.  
  7.  
  8. #define NDIGITS         1007
  9. #define NPRINT          1000
  10.  
  11.  
  12. typedef uchar digitarray[NDIGITS + 1];
  13.  
  14.  
  15. Static uchar *s, *x, *t;
  16. Static long xs, ts, i;
  17.  
  18.  
  19. Static Void initinteger(x, n)
  20. uchar *x;
  21. long n;
  22. {
  23.   long i;
  24.  
  25.   x[0] = n;
  26.   for (i = 1; i <= NDIGITS; i++)
  27.     x[i] = 0;
  28. }
  29.  
  30.  
  31. Static Void divide(x, xs, n, y, ys)
  32. uchar *x;
  33. long xs, n;
  34. uchar *y;
  35. long *ys;
  36. {
  37.   long i, c;
  38.  
  39.   c = 0;
  40.   for (i = xs; i <= NDIGITS; i++) {
  41.     c = c * 10 + x[i];
  42.     y[i] = c / n;
  43.     c %= n;
  44. /* p2c: dist/examples/e.p, line 37:
  45.  * Note: Using % for possibly-negative arguments [317] */
  46.   }
  47.   *ys = xs;
  48.   while (*ys <= NDIGITS && y[*ys] == 0)
  49.     (*ys)++;
  50. }
  51.  
  52.  
  53. Static Void add(s, x, xs)
  54. uchar *s, *x;
  55. long xs;
  56. {
  57.   long i, c;
  58.  
  59.   c = 0;
  60.   for (i = NDIGITS; i >= xs; i--) {
  61.     c += s[i] + x[i];
  62.     if (c >= 10) {
  63.       s[i] = c - 10;
  64.       c = 1;
  65.     } else {
  66.       s[i] = c;
  67.       c = 0;
  68.     }
  69.   }
  70.   i = xs;
  71.   while (c != 0) {
  72.     i--;
  73.     c += s[i];
  74.     if (c >= 10) {
  75.       s[i] = c - 10;
  76.       c = 1;
  77.     } else {
  78.       s[i] = c;
  79.       c = 0;
  80.     }
  81.   }
  82. }
  83.  
  84.  
  85. Static Void sub(s, x, xs)
  86. uchar *s, *x;
  87. long xs;
  88. {
  89.   long i, c;
  90.  
  91.   c = 0;
  92.   for (i = NDIGITS; i >= xs; i--) {
  93.     c += s[i] - x[i];
  94.     if (c < 0) {
  95.       s[i] = c + 10;
  96.       c = -1;
  97.     } else {
  98.       s[i] = c;
  99.       c = 0;
  100.     }
  101.   }
  102.   i = xs;
  103.   while (c != 0) {
  104.     i--;
  105.     c += s[i];
  106.     if (c < 0) {
  107.       s[i] = c + 10;
  108.       c = -1;
  109.     } else {
  110.       s[i] = c;
  111.       c = 0;
  112.     }
  113.   }
  114. }
  115.  
  116.  
  117. main(argc, argv)
  118. int argc;
  119. Char *argv[];
  120. {
  121.   PASCAL_MAIN(argc, argv);
  122.   s = (uchar *)Malloc(sizeof(digitarray));
  123.   x = (uchar *)Malloc(sizeof(digitarray));
  124.   initinteger(s, 0L);
  125.   initinteger(x, 1L);
  126.   xs = 0;
  127.   add(s, x, xs);
  128.   i = 0;
  129.   do {
  130.     i++;
  131.     divide(x, xs, i, x, &xs);
  132.     add(s, x, xs);
  133.     printf("\015Series: %5.2f%%", xs * 100.0 / (NDIGITS + 1));
  134.   } while (xs <= NDIGITS);
  135.   printf("\n%45se = %d.\n", "", s[0]);
  136.   i = 0;
  137.   for (i = 1; i <= NPRINT; i++) {
  138.     printf("%d", s[i]);
  139.     if (i % 1000 == 0)
  140.       putchar('\n');
  141. /* p2c: dist/examples/e.p, line 121:
  142.  * Note: Using % for possibly-negative arguments [317] */
  143.     if (i % 100 == 0)
  144.       putchar('\n');
  145.     else if (i % 10 == 0)
  146.       putchar(' ');
  147. /* p2c: dist/examples/e.p, line 122:
  148.  * Note: Using % for possibly-negative arguments [317] */
  149.   }
  150.   printf("\nFinal digits: ");
  151.   for (i = NPRINT + 1; i <= NDIGITS; i++)
  152.     printf("%d", s[i]);
  153.   putchar('\n');
  154.   exit(0);
  155.  
  156. /* p2c: dist/examples/e.p, line 123:
  157.  * Note: Using % for possibly-negative arguments [317] */
  158. }
  159.  
  160.  
  161.  
  162. /* End. */
  163.