home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 418.lha / calc.rexx < prev    next >
OS/2 REXX Batch file  |  1990-08-28  |  3KB  |  64 lines

  1. /*
  2. ** REXX exec to evaluate an expression of decimal, hexadecimal, or both
  3. ** numbers and return a decimal or hexadecimal result
  4. ** Written becaus I could never find my calculator when I needed it. :-)
  5. **
  6. ** Format CALC expression  -  where expression is an algebraic expression
  7. ** 
  8. ** Written 9/29/88 by Rick Blaha
  9. **
  10. ** Adapted for AREXX 9/9/90
  11. **
  12. ** This program illustrates some nice features of REXX.
  13. ** - The INTERPRET instruction. In my opnion one of the cutest around
  14. ** - The SIGNAL instruction. To handle potential error conditions
  15. **                           Notice the error handling routines at the end
  16. ** - several functions
  17. ** - The structured nature of REXX. This is my method of indentation, 
  18. **   capitalization and commenting. It is not the necessarily the best and by
  19. **   no means is it the only way. I would suggest that you develop a style that
  20. **   is easy use and understand.
  21. */
  22.  
  23. signal on error                        /* Handle error conditions      */
  24. signal on syntax
  25.  
  26. numeric digits 15                      /* 15 digits of precision       */
  27.  
  28. HEX_RESULT = 'NO'                      /* Initialize and get input args*/
  29. parse upper arg IN_LINE
  30.                                        /* Hexadecimal result desired ??*/
  31. if abbrev('HEX',word(IN_LINE,1),1) = 1 then do
  32.    IN_LINE = delword(IN_LINE,1,1)      /* Delete first word            */
  33.    HEX_RESULT = 'YES'                  /* Raise the flag.              */
  34.    end
  35.  
  36. CALC_LINE = IN_LINE                    /* copy the input arguments     */
  37.  
  38. HEX_LOC = index(CALC_LINE,'X')         /* Any Hexadecimal arguments ?  */
  39. do while HEX_LOC > 0                   /* Convert to decimal for math  */
  40.    HEX_END = verify(CALC_LINE,'+-/*()',match,HEX_LOC+1)
  41.    if HEX_END = 0 then HEX_END = length(CALC_LINE) + 1
  42.    HEX_2_DEC = strip(substr(CALC_LINE,HEX_LOC+1,(HEX_END-HEX_LOC)-1))
  43.    HEX_2_DEC = x2d(strip(strip(HEX_2_DEC,,'''')))
  44.    CALC_LINE = delstr(CALC_LINE,HEX_LOC,(HEX_END-HEX_LOC))
  45.    CALC_LINE = insert(HEX_2_DEC,CALC_LINE,HEX_LOC-1)
  46.    HEX_LOC = index(CALC_LINE,'X')
  47.    end
  48.  
  49. interpret 'ANSWER =' CALC_LINE         /* calculate the answer         */
  50. if HEX_RESULT = 'YES'                  /* hexadecimal output if needed */
  51.    then ANSWER = 'X'''||d2x(trunc(ANSWER))||''''
  52.  
  53. RETURN:                                /* show result and terminate    */
  54. say IN_LINE '=' ANSWER
  55. return
  56.  
  57. ERROR:                                 /* Show error to user           */
  58. ANSWER = 'EXEC error at line' SIGL '. Return code =' RC '!!!'
  59. signal RETURN
  60.  
  61. SYNTAX:                                /* Bad expression               */
  62. ANSWER = 'Invalid expression :' errortext(RC)
  63. signal RETURN
  64.