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