home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / D / P2C / P2C-1.20 / P2C-1 / p2c / examples / e.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-17  |  2.5 KB  |  164 lines

  1. /* Output from p2c, the Pascal-to-C translator */
  2. /* From input file "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: e.p, line 37: Note: Using % for possibly-negative arguments [317] */
  47.   }
  48.   *ys = xs;
  49.   while (*ys <= NDIGITS && y[*ys] == 0)
  50.     (*ys)++;
  51. }
  52.  
  53.  
  54. Static Void add(s, x, xs)
  55. digit *s, *x;
  56. long xs;
  57. {
  58.   long i, c;
  59.  
  60.   c = 0;
  61.   for (i = NDIGITS; i >= xs; i--) {
  62.     c += s[i] + x[i];
  63.     if (c >= 10) {
  64.       s[i] = c - 10;
  65.       c = 1;
  66.     } else {
  67.       s[i] = c;
  68.       c = 0;
  69.     }
  70.   }
  71.   i = xs;
  72.   while (c != 0) {
  73.     i--;
  74.     c += s[i];
  75.     if (c >= 10) {
  76.       s[i] = c - 10;
  77.       c = 1;
  78.     } else {
  79.       s[i] = c;
  80.       c = 0;
  81.     }
  82.   }
  83. }
  84.  
  85.  
  86. Static Void sub(s, x, xs)
  87. digit *s, *x;
  88. long xs;
  89. {
  90.   long i, c;
  91.  
  92.   c = 0;
  93.   for (i = NDIGITS; i >= xs; i--) {
  94.     c += s[i] - x[i];
  95.     if (c < 0) {
  96.       s[i] = c + 10;
  97.       c = -1;
  98.     } else {
  99.       s[i] = c;
  100.       c = 0;
  101.     }
  102.   }
  103.   i = xs;
  104.   while (c != 0) {
  105.     i--;
  106.     c += s[i];
  107.     if (c < 0) {
  108.       s[i] = c + 10;
  109.       c = -1;
  110.     } else {
  111.       s[i] = c;
  112.       c = 0;
  113.     }
  114.   }
  115. }
  116.  
  117.  
  118. main(argc, argv)
  119. int argc;
  120. Char *argv[];
  121. {
  122.   PASCAL_MAIN(argc, argv);
  123.   s = (digit *)Malloc(sizeof(digitarray));
  124.   x = (digit *)Malloc(sizeof(digitarray));
  125.   initinteger(s, 0L);
  126.   initinteger(x, 1L);
  127.   xs = 0;
  128.   add(s, x, xs);
  129.   i = 0;
  130.   do {
  131.     i++;
  132.     divide(x, xs, i, x, &xs);
  133.     add(s, x, xs);
  134.     printf("\015Series: %5.2f%%", xs * 100.0 / (NDIGITS + 1));
  135.   } while (xs <= NDIGITS);
  136.   printf("\n%45se = %d.\n", "", s[0]);
  137.   i = 0;
  138.   for (i = 1; i <= NPRINT; i++) {
  139.     printf("%d", s[i]);
  140.     if (i % 1000 == 0)
  141.       putchar('\n');
  142. /* p2c: e.p, line 121:
  143.  * Note: Using % for possibly-negative arguments [317] */
  144.     if (i % 100 == 0)
  145.       putchar('\n');
  146.     else if (i % 10 == 0)
  147.       putchar(' ');
  148. /* p2c: e.p, line 122:
  149.  * Note: Using % for possibly-negative arguments [317] */
  150.   }
  151.   printf("\nFinal digits: ");
  152.   for (i = NPRINT + 1; i <= NDIGITS; i++)
  153.     printf("%d", s[i]);
  154.   putchar('\n');
  155.   exit(EXIT_SUCCESS);
  156.  
  157. /* p2c: e.p, line 123:
  158.  * Note: Using % for possibly-negative arguments [317] */
  159. }
  160.  
  161.  
  162.  
  163. /* End. */
  164.