home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 5 Edit
/
05-Edit.zip
/
me34src.zip
/
me3
/
mutt
/
package
/
calc.mut
< prev
next >
Wrap
Text File
|
1995-01-14
|
4KB
|
140 lines
;; calc.mut : a popup programmers calculator for ME
;; C Durland Public Domain
(include me.mh)
(include tobase.mut)
(int RV TV mem)
(small-int base)
(defun
doc HIDDEN ; popup a window with documentation
{
(xmenu-box
">Mutt CALC - RPN Calculator"
"Enter : Move x to total"
'Backspace : Erase last digit of x'
" "
"+ - * (or x) /"
'| & ^ : Bitwise OR, AND, XOR total and x'
"~ : 1's compliment x"
"< > : Shift total left or right"
"% : Total mod x"
"m : Negate x"
" "
"= : Insert total at dot"
"' (quote) : Insert x at dot (as a character)"
" "
"# : Toggle between decimal and hex"
"B : Change the radix"
" "
"k : Put next key pressed into total"
"K : Put a ME key into total"
" "
"s : Store total in memory"
"r : Recall memory to x"
" "
'C-l (ell) : Redraw the screen'
'q C-g : Quit')
}
MAIN
{
(base 10) ;; initialize base to decimal
(require "menu-box" "popup.mut") ;; for (doc)
}
inc (int n) HIDDEN ; increment TV by n
{
(if (< n base) (TV (+ (* TV base) n)) )
}
vert (int n) HIDDEN ; convert n to proper base for display
{
(if (== base 10) { n (done) } )
(if (< n 0) (concat "-" (tobase (- 0 n) base)) (tobase n base))
}
; odd (int n) HIDDEN { (!= n (* (/ n 2) 2)) } ; TRUE if n is odd
; bitwise (pointer defun op)(int x y) HIDDEN ; (bitwise-op x y)
; {
; (int bit result a b)
;
; (result 0)(bit 1)(a x)(b y)
; (while (or (!= 0 a)(!= 0 b))
; {
; (if (op a b) (+= result bit))
; (*= bit 2) ; next bit
; (/= a 2)(/= b 2)
; })
; result
; }
; bor (int a b) HIDDEN { (or (odd a)(odd b)) } ;TRUE if ((a&1) OR (b&1))==1
; band (int a b) HIDDEN { (and (odd a)(odd b)) } ;TRUE if ((a&1) AND (b&1))==1
; bxor (int a b) HIDDEN { (odd (+ a b)) } ;TRUE if ((a&1) XOR (b&1))==1
; bit-or (int x y) HIDDEN { (bitwise (floc bor) x y) }
; bit-and (int x y) HIDDEN { (bitwise (floc band) x y) }
; bit-xor (int x y) HIDDEN { (bitwise (floc bxor) x y) }
calculator
{
(int n)
(while TRUE
{
(msg "RPN CALC>" base " Memory: " (vert mem base)
" Total: " (vert RV) " x: " (vert TV) )
(switch (getchar)
"0" (inc 0)
"1" (inc 1)
"2" (inc 2)
"3" (inc 3)
"4" (inc 4)
"5" (inc 5)
"6" (inc 6)
"7" (inc 7)
"8" (inc 8)
"9" (inc 9)
"+" { (+= RV TV)(TV 0) }
"-" { (-= RV TV)(TV 0) }
"*" { (*= RV TV)(TV 0) }
"x" { (*= RV TV)(TV 0) }
"/" { (if (== 0 TV)(RV 0)(/= RV TV)) (TV 0) }
"a" (inc 10)
"b" (inc 11)
"c" (inc 12)
"d" (inc 13)
"e" (inc 14)
"f" (inc 15)
"=" { (insert-text (vert RV))(update) }
"^M" { (RV TV)(TV 0) } ; enter
"m" (*= TV -1) ; change sign
"|" { (RV (bit-or RV TV)) (TV 0) }
"&" { (RV (bit-and RV TV)) (TV 0) }
'^' { (RV (bit-xor RV TV)) (TV 0) }
'~' (TV (bit-not TV))
'%' { (if (== 0 TV)(RV 0)(RV (mod RV TV))) (TV 0) }
"^H" (/= TV base)
"s" (mem RV) ; store
"r" (TV mem) ; recall
; "^L" { (refresh-screen)(update) } ; refresh screen
"^L" (update) ; refresh screen
">" (/= RV 2) ; shift right
"<" (*= RV 2) ; shift left
"#" (if (== base 10)(base 16)(base 10)) ; toggle radix
"B" ; change radix
{
(n (convert-to NUMBER (ask "base = ")))
(if (and (<= 2 n)(<= n 16)) (base n))
}
"k"
{
(msg "Press key to convert")
(RV (convert-to CHARACTER (getchar)))
}
"K" { (msg "Press ME key to convert")(RV (get-key)) }
"'" { (insert-text (convert-to CHARACTER TV))(update) }
"?" (doc)
"q" (break) ; quit
"^G" (abort) ; quit
)
})
}
)