home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Club Amiga de Montreal - CAM
/
CAM_CD_1.iso
/
files
/
418.lha
/
calc.rexx
< prev
next >
Wrap
OS/2 REXX Batch file
|
1990-08-28
|
3KB
|
64 lines
/*
** REXX exec to evaluate an expression of decimal, hexadecimal, or both
** numbers and return a decimal or hexadecimal result
** Written becaus I could never find my calculator when I needed it. :-)
**
** Format CALC expression - where expression is an algebraic expression
**
** Written 9/29/88 by Rick Blaha
**
** Adapted for AREXX 9/9/90
**
** This program illustrates some nice features of REXX.
** - The INTERPRET instruction. In my opnion one of the cutest around
** - The SIGNAL instruction. To handle potential error conditions
** Notice the error handling routines at the end
** - several functions
** - The structured nature of REXX. This is my method of indentation,
** capitalization and commenting. It is not the necessarily the best and by
** no means is it the only way. I would suggest that you develop a style that
** is easy use and understand.
*/
signal on error /* Handle error conditions */
signal on syntax
numeric digits 15 /* 15 digits of precision */
HEX_RESULT = 'NO' /* Initialize and get input args*/
parse upper arg IN_LINE
/* Hexadecimal result desired ??*/
if abbrev('HEX',word(IN_LINE,1),1) = 1 then do
IN_LINE = delword(IN_LINE,1,1) /* Delete first word */
HEX_RESULT = 'YES' /* Raise the flag. */
end
CALC_LINE = IN_LINE /* copy the input arguments */
HEX_LOC = index(CALC_LINE,'X') /* Any Hexadecimal arguments ? */
do while HEX_LOC > 0 /* Convert to decimal for math */
HEX_END = verify(CALC_LINE,'+-/*()',match,HEX_LOC+1)
if HEX_END = 0 then HEX_END = length(CALC_LINE) + 1
HEX_2_DEC = strip(substr(CALC_LINE,HEX_LOC+1,(HEX_END-HEX_LOC)-1))
HEX_2_DEC = x2d(strip(strip(HEX_2_DEC,,'''')))
CALC_LINE = delstr(CALC_LINE,HEX_LOC,(HEX_END-HEX_LOC))
CALC_LINE = insert(HEX_2_DEC,CALC_LINE,HEX_LOC-1)
HEX_LOC = index(CALC_LINE,'X')
end
interpret 'ANSWER =' CALC_LINE /* calculate the answer */
if HEX_RESULT = 'YES' /* hexadecimal output if needed */
then ANSWER = 'X'''||d2x(trunc(ANSWER))||''''
RETURN: /* show result and terminate */
say IN_LINE '=' ANSWER
return
ERROR: /* Show error to user */
ANSWER = 'EXEC error at line' SIGL '. Return code =' RC '!!!'
signal RETURN
SYNTAX: /* Bad expression */
ANSWER = 'Invalid expression :' errortext(RC)
signal RETURN