Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for clcalc.rex

(view source code of clcalc.rex as plain text)

  1. /* Command line calculator */
  2.  
  3. /* Check command line validity */
  4. Parse Arg params
  5. params = Translate( params, "/.", ":," )
  6. If params = "" Then Call Syntax
  7. If Pos( "?", params ) > 0 Then Call Syntax
  8.  
  9. /* Parse command line and check each individual parameter */
  10. parmstr = params
  11. param.0 = 0
  12. Do i = 1 by 1 Until parmstr = ""
  13. 	Parse Value parmstr With param.i parmstr
  14. 	If param.i <> "" Then Do
  15. 		param.0 = i
  16. 		param.i = Strip( param.i, "B", '"' )
  17. 	End
  18. End
  19. If param.0 = 0 Then Call Syntax
  20. Do i = 1 to param.0
  21. 	If DataType( param.i, "N" ) = 1 Then Iterate
  22. 	If Pos( param.i, "**//%+-:()" ) = 0 Then Call Syntax
  23. End
  24.  
  25. /* Perform the actual calculation, truncate to whole integer */
  26. Interpret "result = ("||params||") % 1"
  27.  
  28. /* Display the result on screen */
  29. Say result
  30.  
  31. /* Return code is the result, but limited to range 0..255 */
  32. EXIT Max( Min( result, 255 ), 0 )
  33.  
  34.  
  35. Syntax:
  36. 	Say
  37. 	Say "CLCalc,  Version 1.00"
  38. 	Say "Command Line Calculator for use in batch files"
  39. 	Say
  40. 	Say "Usage:  <REXX>  CLCALC.REX  <expression>"
  41. 	Say
  42. 	Say 'Where:    "<REXX>"       is your Rexx interpreter:'
  43. 	Say "                         - Windows:  REGINA.EXE or REXX.EXE,"
  44. 	Say "                                     whichever is installed"
  45. 	Say "                         - OS/2:     no need to specify, just"
  46. 	Say "                                     rename script to *.cmd"
  47. 	Say '          "<expression>" is a valid Rexx expression:'
  48. 	Say "                         Operators +, -, /, //, %, *, ** and : are allowed"
  49. 	Say "                         Both dots and commas are allowed as decimal delimiters"
  50. 	Say "                         Numbers and operators should be separated by spaces"
  51. 	Say
  52. 	Say "Returns:  Result of <expression> on screen, and as a return code"
  53. 	Say "          (minimum return code is 0, maximum is 255)"
  54. 	Say
  55. 	Say "Written by Rob van der Woude"
  56. 	Say "http://www.robvanderwoude.com"
  57. 	EXIT 0
  58. return
  59.  

page last uploaded: 2016-12-15, 11:20