home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / mutt / calc.mut < prev    next >
Text File  |  1988-08-11  |  4KB  |  119 lines

  1.   ;; calc.mut : a popup programmers calculator for ME
  2.  
  3. (include popup.mut)    ; for the doc window
  4. (include tobase.mut)
  5.  
  6. (INT RV TV mem)
  7. (int base)
  8.  
  9. (defun
  10.   doc HIDDEN    ; popup a window with documentation
  11.   {
  12.     (popup-window 0 0 40 16)
  13.     (wputs "Mutt CALC - an RPN calculator")
  14.     (wputs "+ - * (or x) /")
  15.     (wputs "Enter or Return : move x to total")
  16.     (wputs "m : Negate x")
  17.     (wputs "| & \^ : bitwise OR, AND, XOR total and x")
  18.     (wputs "> < : shift right or left")
  19.     (wputs "% : total mod x")
  20.     (wputs "s : Store total in memory")
  21.     (wputs "r : Recall memory to x")
  22.     (wputs "= : Insert total at dot")
  23.     (wputs "\^H or BACKSPACE : Erase last digit of x")
  24.     (wputs "\^L : Redraw the screen")
  25.     (wputs "# : Toggle between decimal and hex")
  26.     (wputs "B : Change the radix")
  27.     (wputs "k : Put the next key pressed into total")
  28.     (wputs 'q ^G : Quit')
  29.   }
  30.   num (array byte s 1) HIDDEN { (s 0) }    ; convert character to number
  31.   inc (int n) HIDDEN        ; increment TV by n
  32.   {
  33.     (if (< n base) (TV (+ (* TV base) n)) )
  34.   }
  35.   MAIN { (base 10) }        ; initialize base to decimal
  36.   vert (int n)    HIDDEN    ; convert n to proper base for display
  37.   {
  38.     (if (== base 10) { n (done) } )
  39.     (if (< n 0) (concat "-" (tobase (- 0 n) base)) (tobase n base))
  40.   }
  41.   odd (int n) HIDDEN { (!= n (* (/ n 2) 2)) }    ; TRUE if n is odd
  42.   bitwise (pointer defun op)(int x y) HIDDEN    ; (bitwise-op x y)
  43.   {
  44.     (INT bit result a b)
  45.  
  46.     (result 0)(bit 1)(a x)(b y)
  47.     (while (or (!= 0 a)(!= 0 b))
  48.     {
  49.       (if (op a b) (+= result bit))
  50.       (*= bit 2)    ; next bit
  51.       (/= a 2)(/= b 2)
  52.     })
  53.     result
  54.   }
  55.   bor  (int a b) HIDDEN { (or (odd a)(odd b)) }  ;TRUE if ((a&1) OR (b&1))==1
  56.   band (int a b) HIDDEN { (and (odd a)(odd b)) } ;TRUE if ((a&1) AND (b&1))==1
  57.   bxor (int a b) HIDDEN { (odd (+ a b)) }     ;TRUE if ((a&1) XOR (b&1))==1
  58.   bitwise-or  (int x y) HIDDEN { (bitwise (floc bor)  x y) }
  59.   bitwise-and (int x y) HIDDEN { (bitwise (floc band) x y) }
  60.   bitwise-xor (int x y) HIDDEN { (bitwise (floc bxor) x y) }
  61.   calculator
  62.   {
  63.     (int n)
  64.  
  65.     (while TRUE
  66.     {
  67.       (msg "RPN CALC>" base " Memory: " (vert mem base)
  68.     " Total: " (vert RV) "  x: " (vert TV) )
  69.       (switch (getchar)
  70.     "0" (inc 0)
  71.     "1" (inc 1)
  72.     "2" (inc 2)
  73.     "3" (inc 3)
  74.     "4" (inc 4)
  75.     "5" (inc 5)
  76.     "6" (inc 6)
  77.     "7" (inc 7)
  78.     "8" (inc 8)
  79.     "9" (inc 9)
  80.         "+" { (+= RV TV)(TV 0) }
  81.         "-" { (-= RV TV)(TV 0) }
  82.         "*" { (*= RV TV)(TV 0) }
  83.         "x" { (*= RV TV)(TV 0) }
  84.         "/" { (if (== 0 TV)(RV 0)(/= RV TV)) (TV 0) } 
  85.     "a" (inc 10)
  86.     "b" (inc 11)
  87.     "c" (inc 12)
  88.     "d" (inc 13)
  89.     "e" (inc 14)
  90.     "f" (inc 15)
  91.     "=" { (insert-text (vert RV))(update) }
  92.     "^M" { (RV TV)(TV 0) }            ; enter
  93.     "m" (*= TV -1)                ; change sign
  94.     "|" { (RV (bitwise-or RV TV)) (TV 0) }
  95.     "&" { (RV (bitwise-and RV TV)) (TV 0) }
  96.     '^' { (RV (bitwise-xor RV TV)) (TV 0) }
  97.     '%' { (if (== 0 TV)(RV 0)(RV (mod RV TV))) (TV 0) }
  98.     "^H" (/= TV base)
  99.     "s" (mem RV)                ; store
  100.     "r" (TV mem)                ; recall
  101.     "^L" { (refresh-screen)(update) }    ; refresh screnn
  102.     ">" (/= RV 2)                ; shift right
  103.     "<" (*= RV 2)                ; shift left
  104.     "#" (if (== base 10)(base 16)(base 10))    ; toggle radix
  105.     "B"                    ; change radix
  106.       {
  107.         (n (atoi (ask "base = ")))
  108.         (if (and (<= 2 n)(<= n 16)) (base n))
  109.       }
  110.     "k" { (msg "Press key to convert")(RV (num (getchar))) }
  111.     "K" { (msg "Press ME key to convert")(RV (get-key)) }
  112.     "?" (doc)
  113.     "q" (break)            ; quit
  114.     "^G" (break)            ; quit
  115.       )
  116.     })
  117.   }
  118. )
  119.