home *** CD-ROM | disk | FTP | other *** search
/ Share Gallery 1 / share_gal_1.zip / share_gal_1 / GR / GR505.ZIP / LSP.EXE / CALC.LSP < prev    next >
Text File  |  1988-07-08  |  5KB  |  238 lines

  1. ;
  2. ;    CALC.LSP
  3. ;
  4. ;        Written by Hugh S. Myers of:
  5. ;
  6. ;        Bazeghi-Myers
  7. ;        8414 Fairview Ave.
  8. ;        Boise, Idaho  83704
  9. ;
  10. ;        Or contact me through CompuServe,
  11. ;        User ID: 76314,3672
  12. ;        Not too surprisingly I hang out at !go adesk...
  13. ;
  14. ;        Cautions and such...I'm giving this stuff away
  15. ;        as is...I don't plan on real heavy support(i.e. none!)
  16. ;        but would like to hear of bugs or problems, certainly
  17. ;        would like to hear if you can improve, sort of give back
  18. ;        at least as much as you get plan(does that make this
  19. ;        golden rule ware???!)  At any rate, this is not a how
  20. ;        too for Autolisp, and the comments are not as thick as
  21. ;        they might be, but persevere(or at least persist) and
  22. ;        all should be made clear.
  23. ;
  24. ;        So, here goes.....
  25. ;
  26. ;
  27. ;    a few string handling functions
  28. ;
  29.  
  30. (DEFUN REDUCE(A)
  31.     (SUBSTR A 2)
  32. )
  33.  
  34. (DEFUN TRIMSTR(A)
  35.     (SUBSTR A 1 (1- (STRLEN A)))
  36. )
  37.  
  38. (DEFUN FIRSTCHAR(A)
  39.     (SUBSTR A 1 1)
  40. )
  41.  
  42. (DEFUN LASTCHAR(A)
  43.     (SUBSTR A (STRLEN A))
  44. )
  45.  
  46. (DEFUN NNUL(A)
  47.     (NOT (ZEROP (STRLEN A)))
  48. )
  49.  
  50. (DEFUN STRINGP(A)
  51.     (EQUAL 'STR (TYPE A))
  52. )
  53.  
  54. (DEFUN TEXPLODE(A)
  55.     (SETQ L (LIST (FIRSTCHAR A)))
  56.     (SETQ A (REDUCE A))
  57.     (WHILE (NNUL A)
  58.         (SETQ L (CONS (FIRSTCHAR A) L))
  59.         (SETQ A (REDUCE A))
  60.     )
  61.     (REVERSE L)
  62. )
  63.  
  64. (DEFUN SNUMBP(A)
  65.     (IF (MEMBER A '("0" "1" "2" "3" "4" "5" "6" "7" "8" "9"))
  66.         T
  67.         NIL
  68.     )
  69. )
  70.  
  71. ;
  72. ;    now a few necessary functions for a calculator, feet & inches...
  73. ;
  74.  
  75. (DEFUN GETSN(A)
  76.     (SETQ L "")
  77.     (WHILE (SNUMBP (FIRSTCHAR A))
  78.         (SETQ L (STRCAT L (FIRSTCHAR A)))
  79.         (SETQ A (REDUCE A))
  80.     )
  81.     (CONS L A)
  82. )
  83.  
  84. (DEFUN GETSFRAC(A)
  85.     (SETQ X1 (GETSN A))
  86.     (SETQ Y1 (STRCAT (REDUCE (CDR X1)) ".0"))
  87.     (SETQ X1 (STRCAT (CAR X1) ".0"))
  88.     (/ (ATOF X1) (ATOF Y1))
  89. )
  90.  
  91. (DEFUN GETSNUMB(A)
  92.     (IF (EQUAL "-" (FIRSTCHAR A))
  93.         (SETQ A (REDUCE A))
  94.     )
  95.     (SETQ X (GETSN A))
  96.     (SETQ Y (FIRSTCHAR (CDR X)))
  97.     (SETQ Z (REDUCE (CDR X)))
  98.     (SETQ X (CAR X))
  99.     (COND
  100.         ((EQUAL "." Y) 
  101.             (PROGN
  102.                 (SETQ A (ATOF A))
  103.                 (SETQ Z "")
  104.             )
  105.         )
  106.         ((EQUAL "/" Y)
  107.             (PROGN
  108.                 (SETQ A (GETSN Z))
  109.                 (SETQ Z (CAR A))
  110.                 (SETQ ZZ (FIRSTCHAR (CDR A)))
  111.                 (SETQ A (GETSFRAC (STRCAT X Y Z)))
  112.                 (IF (EQUAL "'" ZZ)
  113.                     (SETQ A (* A 12.0))
  114.                 )
  115.                 (SETQ Z "")
  116.             )
  117.         )
  118.         ((EQUAL "'" Y) (SETQ A (* 12.0 (ATOF X))))
  119.         (T (SETQ A (ATOF X)))
  120.     )
  121.     (CONS A Z)
  122. )
  123.  
  124. (DEFUN GETFT&IN(X)
  125.     (SETQ N 0)
  126.     (WHILE (NNUL X)
  127.         (SETQ X (GETSNUMB X))
  128.         (SETQ N (+ N (CAR X)))
  129.         (SETQ X (CDR X))
  130.     )
  131.     (EVAL N)
  132. )
  133.  
  134. (DEFUN PUSH_NUM(N)
  135.     (SETQ NUM_STACK (CONS N NUM_STACK))
  136. )
  137.  
  138. (DEFUN POP_NUM()
  139.     (SETQ N (CAR NUM_STACK))
  140.     (SETQ NUM_STACK (CDR NUM_STACK))
  141.     (EVAL N)
  142. )
  143.  
  144. (DEFUN CLR_NUM()
  145.     (SETQ NUM_STACK NIL)
  146. )
  147.  
  148. (DEFUN NUM_OK(N)
  149.     (> (LENGTH NUM_STACK) N)
  150. )
  151.  
  152. (DEFUN C:CALC()
  153.     (SETQ FLAG T)
  154.     (WHILE FLAG
  155.         (SETQ A (GETSTRING "CALC: "))
  156.         (COND
  157.             ((EQUAL A "CLR") 
  158.                 (CLR_NUM))
  159.             ((EQUAL A "STK") 
  160.                 (PRIN1 NUM_STACK))
  161.             ((EQUAL A "+") 
  162.                 (IF (NUM_OK 1) (DO_PLUS) 
  163.                     (CALC_ERR "need at least two numbers")))
  164.             ((EQUAL A "-") 
  165.                 (IF (NUM_OK 1) (DO_MINUS) 
  166.                     (CALC_ERR "need at least two numbers")))
  167.             ((EQUAL A "*") 
  168.                 (IF (NUM_OK 1) (DO_TIMES) 
  169.                     (CALC_ERR "need at least two numbers")))
  170.             ((EQUAL A "/") 
  171.                 (IF (NUM_OK 1) (DO_DIVIDE) 
  172.                     (CALC_ERR "need at least two numbers")))
  173.             ((EQUAL A "==") 
  174.                 (IF (NUM_OK 0) (DO_DISPLAY) 
  175.                     (CALC_ERR "need at least one number")))
  176.             ((EQUAL A "=D") 
  177.                 (IF (NUM_OK 0) (DO_DDISPLAY) 
  178.                     (CALC_ERR "need at least one number")))
  179.             ((EQUAL A "") 
  180.                 (SETQ FLAG NIL))
  181.             (T 
  182.                 (PUSH_NUM (GETFT&IN A)))
  183.         )
  184.         (TERPRI)
  185.     )
  186.     (CLR_NUM)
  187. )
  188.  
  189. (DEFUN DO_PLUS()
  190.     (PUSH_NUM (+ (POP_NUM) (POP_NUM)))
  191. )
  192.  
  193. (DEFUN DO_MINUS()
  194.     (SETQ A (POP_NUM))
  195.     (PUSH_NUM (- (POP_NUM) A))
  196. )
  197.  
  198. (DEFUN DO_TIMES()
  199.     (PUSH_NUM (* (POP_NUM) (POP_NUM)))
  200. )
  201.  
  202. (DEFUN DO_DIVIDE()
  203.     (SETQ A (POP_NUM))
  204.     (PUSH_NUM (/ (POP_NUM) A))
  205. )
  206.  
  207. (DEFUN DO_DISPLAY()
  208.     (PRIN1 (RTOS (CAR NUM_STACK) 4 8))
  209. )
  210.  
  211. (DEFUN DO_DDISPLAY()
  212.     (PRIN1 (RTOS (CAR NUM_STACK) 2 8))
  213. )
  214.  
  215. (DEFUN CALC_ERR(A)
  216.     (PRIN1 (STRCAT "***CALC ERROR, " A))
  217. )
  218. ;
  219. ;    I haven't taken the time to find out all the possible ways
  220. ;    to use the calculator.  The only thing that I have figured
  221. ;    out the first time through is a format like:
  222. ;    
  223. ;    CALC: 1'3+2'6 <return>
  224. ;    CALC: == <return>
  225. ;    "3'-9""  <will be the answer>
  226. ;
  227. ;    CALC: clr  <will clear the stack>
  228. ;    CALC: stk  <will display the stack>
  229. ;
  230. ;    I think mabye some changes are in order to get the routine
  231. ;    to do fractions.  After I learn more, I will update the 
  232. ;    commands.  If you figure it all out, you might add to the list.
  233. ;
  234. ;                        The ACADemy
  235. ;                           (512) 445-6000
  236. ;                          1200 baud
  237. ;                          
  238.