¸®½À °­Á - ¸®½ÀÀ¸·Î ¸¸µç °è»ê±â ÇÔ¼ö.


º» °­Á´ ¾ÆÅ°¿ÀÇǽº(±èÈñÅÂ)¿¡ ÀÇÇØ ¸¸µé¾î Á³À¸¸ç ¾î¶°ÇÑ À¥À̳ª
ÃâÆÇ¹°¿¡ ¾ÆÅ°¿ÀÇǽº(±èÈñÅÂ)ÀÇ µ¿ÀǾøÀÌ ¿Ã¸®´Â °ÍÀ» Çã¶ôÇÏÁö ¾Ê½À´Ï´Ù.

 

¸®½ÀÀ¸·Î cal °ú ºñ½ÁÇÑ ÇÔ¼ö¸¦ ±¸ÇöÇØ º¸¾Ò½À´Ï´Ù.
¾µ¼öÀÖ´Â ¿¬»êÀÚ´Â ´õÇϱâ "+", »©±â "-", °öÇϱâ "x or X or *", ³ª´©±â "/" ÀÔ´Ï´Ù.
Áßø °ýÈ£°¡ °¡´ÉÇÕ´Ï´Ù.



(vl-kht-cal »ê¼ú½Ä¹®ÀÚ¿­)

=> »ê¼ú½ÄÀÌ ¿Ã¹Ù¸£¸é °è»êµÈ °ªÀ» ¸®ÅÏ.

(vl-kht-cal "600 - (70 x (20 x (5 + 4 - 7)) X 6 / 2 + 3)")

=> -7803.0

(vl-kht-cal "((25 + (70 / 20 / (5 + 4) + 7) * 6 - 200 + 500 ) - 1000) * 3" )

=> -1892.0

(vl-kht-cal "2*3*100.53+3/2+23.63/4/4-200")

=> 406.157

 

ÄÚµå

  
(defun str-subst (newstr paten stri / n)
  (setq n 0)
  (while (and (setq n (vl-string-position (ascii paten) stri n)) (/= stri (setq stri (vl-string-subst newstr paten stri n ))))
    (setq n (+ n (strlen newstr)))
  )
  stri
)


(defun str-count (paten stri / n)
  (setq n 0)
  (while (/= stri (setq stri (vl-string-subst "" paten stri))) (setq n (1+ n)) )
  n
)
	    


(defun vl-kht-cal (a / vl-kht-cal-sub  chk1 chk2 exchk1 exchk2 ex)

  (defun vl-kht-cal-sub (a / app+- app*/ ex)
    (mapcar '(lambda (x)
               (if (/= 'SYM (type x)) (setq x (if (listp x) (vl-kht-cal-sub x) (float x))))
               (cond
                 ((or (= '* x) (= '/ x))
                  (setq app*/ (list x ex))
                 )
                 ((or (= '+ x) (= '- x))
                  (if (and app*/ (= 2 (length app+-)) (or (= '+ (car app+-)) (= '- (car app+-))))
                    (setq app+- (list x (append app+- (list app*/))) app*/ nil)
                    (setq app+- (list x (if app+- (append app+- (list ex)) ex)) app*/ nil)
                  )
                 )
                 ((and (= 2 (length app*/)) (or (= '* (car app*/)) (= '/ (car app*/))))
                  (setq app*/ (append app*/ (list x)) ex app*/)
                 )
                 (t (setq ex x))
               )
             )
           a
    )
    (if app+-
      (append app+- (list ex))
      app*/
    )
  )
  

  (if (or (/= "(" (substr a 1 1)) (/= ")" (substr a (strlen a))))
    (setq a (strcat "(" a ")"))
  )
  (mapcar '(lambda (x y) (setq a (str-subst x y a)))
          '(""  "*" "*" " * " " (" ") " " + " " - " " / ")
          '("," "x" "X" "*"   "("  ")"  "+"   "-"   "/"  )
  )
  (setq chk1   (str-count "(" a)
        chk2   (str-count ")" a)
        ex     (vl-princ-to-string (read a))
        exchk1 (str-count "(" ex)
        exchk2 (str-count ")" ex)
  )
  (cond
    ((or (/= chk1 exchk1) (/= chk2 exchk2))
     (princ "\n °ýÈ£ÀÇ °¹¼ö°¡ ¸ÂÁö¾Ê°Å³ª À߸øµÇ¾ú½À´Ï´Ù.")
     nil
    )
    ((not (vl-every '(lambda (x) (or (= '- x) (= '+ x) (= '* x) (= '/ x) (= 'INT (type x)) (= 'REAL (type x))))
                    (read (strcat "(" (str-subst "" ")" (str-subst "" "(" a)) ")") )
     ))
     (princ "\nÀ߸øµÈ ¿¬»êÀÚ³ª ¼ýÀÚ¸¦ ÀÔ·ÂÇß½À´Ï´Ù.")
     nil
    )
    (t (eval (vl-kht-cal-sub (read a))))
  )
)