home *** CD-ROM | disk | FTP | other *** search
/ Boston 2 / boston-2.iso / DOS / PROGRAM / C / APMTEST / APMTEST.C < prev    next >
C/C++ Source or Header  |  1993-12-01  |  5KB  |  285 lines

  1. #include <stdio.h>
  2. #include <setjmp.h>
  3. #include "apm.h"
  4.  
  5. extern void exit();
  6. extern long strtol();
  7. extern long time();
  8.  
  9. #ifndef MSC
  10. extern char *malloc();
  11. extern char *realloc();
  12. extern void free();
  13. #endif /* ! MSC */
  14.  
  15. #ifdef MSDOS
  16. # define STRING_SIZE    512
  17. #else
  18. # define STRING_SIZE    8192
  19. #endif /* MSDOS */
  20.  
  21. jmp_buf here;
  22.  
  23. /*
  24.  * System-independent replacement for index() and strchr().
  25.  */
  26. char *
  27. sindex(s, ch)
  28. char *s;
  29. int ch;
  30. {
  31.     if (s != NULL) {
  32.         for (; *s != '\0'; ++s) {
  33.             if (*s == (char)ch) {
  34.                 return (s);
  35.             }
  36.         }
  37.     }
  38.     return (NULL);
  39. }
  40.  
  41. int
  42. errorHandler(error, message, file, line, func)
  43. int error;
  44. char *message;
  45. char *file;
  46. int line;
  47. char *func;
  48. {
  49.     fprintf(stderr, "\"%s\": line %d: %s: %s %d: %s\n",
  50.         file, line, func, error < 0 ? "error" : "warning",
  51.         error, message);
  52.     if (error < 0) {
  53.         longjmp(here, 1);
  54.     }
  55.     else {
  56.         return (error);
  57.     }
  58. }
  59.  
  60. void
  61. usage()
  62. {
  63.     fprintf(stderr, "usage: apm [base]\n");
  64.     exit(1);
  65. }
  66.  
  67. main(argc, argv)
  68. int argc;
  69. char **argv;
  70. {
  71.     APM apm;
  72.     APM other;
  73.     APM result;
  74.     APM remainder;
  75.     char *cp;
  76.     char *x;
  77.     int ercode;
  78.     int scaleFactor;
  79.     int n;
  80.     int outLen;
  81.     int outDp;
  82.     int leftjust = 0;
  83.     int columnlist = 0;
  84.     short base = -1;
  85.  
  86.     for (--argc, ++argv; argc > 0; --argc, ++argv) {
  87.         if (base > -1) {
  88.             usage();
  89.         }
  90.         base = (short)strtol(*argv, &cp, 10);
  91.     }
  92.  
  93.     if (argc > 0) {
  94.         usage();
  95.     }
  96.  
  97.     if (base == -1) {
  98.         base = 0;
  99.     }
  100.  
  101.     apmDebugFile(stderr);
  102.     (void)apmErrorFunc(errorHandler);
  103.  
  104.     if (setjmp(here) != 0) {
  105.         exit (1);
  106.     }
  107.  
  108.     x = malloc(STRING_SIZE + 1);
  109.  
  110.     if (x == NULL) {
  111.         fprintf(stderr, "not enough memory\n");
  112.         exit(1);
  113.     }
  114.  
  115.     apm = apmNew(0);
  116.     other = apmNew(0);
  117.     result = apmNew(0);
  118.     remainder = apmNew(0);
  119.  
  120.     for (;;) {
  121.         setjmp(here);
  122. /***
  123.         apmGarbageCollect();
  124. ***/
  125.         fprintf(stderr, "\nEnter first number:\t");
  126.         fflush(stderr);
  127.         if (fgets(x, STRING_SIZE, stdin) == NULL) {
  128.             break;
  129.         }
  130.         cp = sindex(x, '\n');
  131.         if (cp != NULL) {
  132.             *cp = '\0';
  133.         }
  134.         if (x[0] == '\0') {
  135.             apmAssign(apm, result);
  136.         }
  137.         else {
  138.             apmAssignString(apm, x, base);
  139.         }
  140.  
  141.         fprintf(stderr, "Enter second number:\t");
  142.         fflush(stderr);
  143.         if (fgets(x, STRING_SIZE, stdin) == NULL) {
  144.             break;
  145.         }
  146.         cp = sindex(x, '\n');
  147.         if (cp != NULL) {
  148.             *cp = '\0';
  149.         }
  150.         if (x[0] == '\0') {
  151.             apmAssign(other, result);
  152.         }
  153.         else {
  154.             apmAssignString(other, x, base);
  155.         }
  156.  
  157.         fprintf(stderr, "Enter scale factor:\t");
  158.         fflush(stderr);
  159.         if(fgets(x, STRING_SIZE, stdin) == NULL) {
  160.             break;
  161.         }
  162.         cp = sindex(x, '\n');
  163.         if (cp != NULL) {
  164.             *cp = '\0';
  165.         }
  166.         scaleFactor = (int)strtol(x, &cp, 10);
  167.         if (cp == x) {
  168.             scaleFactor = 0;
  169.         }
  170.  
  171.         fprintf(stderr, "Enter field width:\t");
  172.         fflush(stderr);
  173.         if(fgets(x, STRING_SIZE, stdin) == NULL) {
  174.             break;
  175.         }
  176.         cp = sindex(x, '\n');
  177.         if (cp != NULL) {
  178.             *cp = '\0';
  179.         }
  180.         outLen = (int)strtol(x, &cp, 10);
  181.         if (cp == x) {
  182.             outLen = 32;
  183.         }
  184.  
  185.         fprintf(stderr, "Enter number of DP's:\t");
  186.         fflush(stderr);
  187.         if(fgets(x, STRING_SIZE, stdin) == NULL) {
  188.             break;
  189.         }
  190.         cp = sindex(x, '\n');
  191.         if (cp != NULL) {
  192.             *cp = '\0';
  193.         }
  194.         outDp = (int)strtol(x, &cp, 10);
  195.         if (cp == x) {
  196.             outDp = 10;
  197.         }
  198.  
  199.         n = apmCompare(apm, other);
  200.         if (n < 0) {
  201.             cp = "<";
  202.         }
  203.         else if (n > 0) {
  204.             cp = ">";
  205.         }
  206.         else {
  207.             cp = "==";
  208.         }
  209.         fprintf(stderr, "  Comparison:\t\tfirst %s second\n", cp);
  210.  
  211. /*
  212.         apmNegate(result, apm);
  213. */
  214.         apmCalc(result, apm, APM_NEG, NULL);
  215.         apmConvert(x, outLen + 1, outDp, 0, leftjust, result);
  216.         if (columnlist) {
  217.             fprintf(stderr, "\t\t\t12345678901234567890123456789012345678901234567890123456789012345678901234567890\n");
  218.         }
  219.         fprintf(stderr, "  Negation of first:\t%s\n", x);
  220.  
  221.         cp = NULL;
  222. /*
  223.         apmReciprocal(result, outDp, other);
  224. */
  225.         apmCalc(result, other, APM_RECIP(outDp), NULL);
  226.         if (apm_errno == APM_WDIVBYZERO) {
  227.             cp = " (division by zero)";
  228.         }
  229.         apmConvert(x, outLen + 1, outDp, 0, leftjust, result);
  230.         fprintf(stderr, "  Reciprocal of second:\t%s%s\n", x,
  231.             cp == NULL ? "" : cp);
  232.  
  233. /*
  234.         apmAdd(result, apm, other);
  235. */
  236.         apmCalc(result, apm, other, APM_ADD, NULL);
  237.         apmConvert(x, outLen + 1, outDp, 0, leftjust, result);
  238.         fprintf(stderr, "  Sum:\t\t\t%s\n", x);
  239.  
  240. /*
  241.         apmSubtract(result, apm, other);
  242. */
  243.         apmCalc(result, apm, other, APM_SUB, NULL);
  244.         apmConvert(x, outLen + 1, outDp, 0, leftjust, result);
  245.         fprintf(stderr, "  Difference:\t\t%s\n", x);
  246.  
  247. /*
  248.         apmMultiply(result, apm, other);
  249. */
  250.         apmCalc(result, apm, other, APM_MUL, NULL);
  251.         apmConvert(x, outLen + 1, outDp, 0, leftjust, result);
  252.         fprintf(stderr, "  Product:\t\t%s\n", x);
  253.  
  254.         cp = NULL;
  255. /*
  256.         apmDivide(result, outDp, remainder, apm, other);
  257. */
  258.         apmCalc(result, apm, other, APM_DIV(outDp), NULL);
  259.         apmGetRegister(remainder, 0);
  260.         if (apm_errno == APM_WDIVBYZERO) {
  261.             cp = " (division by zero)";
  262.         }
  263.         apmConvert(x, outLen + 1, outDp, 0, leftjust, result);
  264.         fprintf(stderr, "  Quotient:\t\t%s%s\n", x,
  265.             cp ? cp : "");
  266.         apmConvert(x, outLen + 1, outDp, 0, leftjust, remainder);
  267.         fprintf(stderr, "  Remainder:\t\t%s\n", x);
  268.  
  269. /*
  270.         apmScale(apm, result, scaleFactor);
  271. */
  272.         apmCalc(apm, apm, APM_POP(1), result, APM_SCALE(scaleFactor),
  273.             NULL);
  274.         apmConvert(x, outLen + 1, outDp, 0, leftjust, apm);
  275.         fprintf(stderr, "  Scaled quotient:\t%s\n", x);
  276.  
  277.         apmCalc(result, APM_PUSH(1), APM_DUP, APM_ADD, APM_DUP,
  278.             other, APM_MUL, APM_SUB, NULL);
  279.         apmConvert(x, outLen + 1, outDp, 0, leftjust, result);
  280.         fprintf(stderr, "  Calc test:\t\t%s\n", x);
  281.  
  282.     }
  283.     putchar('\n');
  284. }
  285.