Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Expression evaluator
#30
It was for LISP. I did it iteratively...

Goto innermost parentheses, solve, replace, go back.

Code:
(defun reduce.string () (let
((read.loc 1) (start.p 0) (end.loc 0) (op "") (string2 "") (string3 "") (op.exists 0) (num1 "") (num2 "") (num3 "") (st "") (i 0) (j 0) (string1a "") (string1b ""))

;; get the location of the innermost parentheses
(loop (let()
(setq start.p (instr string1 "(" read.loc))
(if (= start.p 0) (return))
(setq read.loc (+ start.p 1))
)) ;loop

;; find the end of the parentheses-enclosed statement.
(setq end.loc (instr string1 ")" read.loc))

;; removing the innermost ( ), save everything before as string2, everything after as string3, and extract the parentheses-enclosed statement as string1.

(setq string2 (mid$ string1 1 (- read.loc 2)))
(if (> (- (length string1) end.loc) 0) (setq string3 (mid$ string1 (+ end.loc 1) (- (length string1) end.loc))) (setq string3 ""))
(setq string1 (mid$ string1 read.loc (- end.loc read.loc)))

;; find an operand, starting from the left and going in the order: /, *, -, +
(loop (let()
(setq op.exists (instr string1 "/ "))
(if (= op.exists 0) (let()
(setq op.exists (instr string1 "* "))
(if (= op.exists 0) (let()
(setq op.exists (instr string1 "- "))
(if (= op.exists 0) (let()
(setq op.exists (instr string1 "+ "))
(if (= op.exists 0) (return) (setq op "+"))
) ;else
(setq op "-")
) ;end if
) ;else
(setq op "*")
) ;end if
) ;else
(setq op "/")
) ;end if

;; operate on the two numbers left and right of the operand.
(pprint (+str string2 "(" string1 ")" string3))

(setq num1 "" num2 "")
(setq i (- op.exists 2))
(loop (let()
(setq st (mid$ string1 i 1))
(if (<> (instr "-0123456789/" st) 0)
(setq num2 (+str st num2))
;else
(return)
) ;end if
(setq i (- i 1))
(if (= i 0) (return))
)) ;loop

(setq j i) (setq i (+ op.exists 2))
(loop (let()
(setq st (mid$ string1 i 1))
(if (<> (instr "-0123456789/" st) 0)
(setq num1 (+str num1 st))
(let() ;else
(setq i (- i 1))
(return))
) ;end if
(if (= i (len string1)) (return))
(setq i (+ i 1))
)) ;loop

(if (equalp op "+") (setq num3 (str$ (+ (val num1) (val num2))))
(if (equalp op "-") (setq num3 (str$ (- (val num2) (val num1))))
(if (equalp op "*") (setq num3 (str$ (* (val num1) (val num2))))
(setq num3 (str$ (/ (val num2) (val num1))))
))) ;end ifs

(setq string1a (mid$ string1 1 j))
(setq string1b (mid$ string1 (+ i 1) (- (length string1) i)) )
(setq string1 (+str string1a num3 string1b))
)) ;loop
;; now set the value of the function
(setq string1 (+str string2 string1 string3))
)) ;end reduce.string
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Reply


Messages In This Thread
Expression evaluator - by Nexinarus - 07-10-2003, 10:48 AM
Expression evaluator - by Agamemnus - 07-10-2003, 05:46 PM
Expression evaluator - by toonski84 - 07-10-2003, 07:05 PM
Expression evaluator - by Moneo - 07-10-2003, 11:36 PM
Expression evaluator - by Antoni Gual - 07-10-2003, 11:48 PM
Re: Expression evaluator - by Mango - 07-11-2003, 12:29 AM
Expression evaluator - by toonski84 - 07-11-2003, 12:38 AM
Expression evaluator - by whitetiger0990 - 07-11-2003, 01:39 AM
Expression evaluator - by Moneo - 07-11-2003, 03:44 AM
Expression evaluator - by Nexinarus - 07-11-2003, 03:55 AM
Expression evaluator - by Moneo - 07-11-2003, 06:30 AM
Expression evaluator - by oracle - 07-11-2003, 07:09 AM
Expression evaluator - by Moneo - 07-11-2003, 09:02 AM
Expression evaluator - by na_th_an - 07-12-2003, 06:48 PM
Expression evaluator - by oracle - 07-14-2003, 03:37 AM
Expression evaluator - by Moneo - 07-14-2003, 04:18 AM
Expression evaluator - by Agamemnus - 07-14-2003, 05:06 AM
Expression evaluator - by Moneo - 07-14-2003, 05:22 AM
Expression evaluator - by na_th_an - 07-14-2003, 05:53 AM
Expression evaluator - by Moneo - 07-14-2003, 05:59 AM
huh? - by Agamemnus - 07-14-2003, 06:36 AM
Expression evaluator - by Moneo - 07-14-2003, 07:09 AM
Expression evaluator - by oracle - 07-14-2003, 07:12 AM
Expression evaluator - by Moneo - 07-14-2003, 07:35 AM
Expression evaluator - by relsoft - 07-14-2003, 10:30 AM
Expression evaluator - by Nexinarus - 07-14-2003, 04:12 PM
Expression evaluator - by na_th_an - 07-14-2003, 04:48 PM
Expression evaluator - by Agamemnus - 07-14-2003, 05:53 PM
Expression evaluator - by na_th_an - 07-14-2003, 06:55 PM
Expression evaluator - by Agamemnus - 07-14-2003, 08:26 PM
Expression evaluator - by na_th_an - 07-14-2003, 09:07 PM
Expression evaluator - by Agamemnus - 07-14-2003, 09:14 PM
Expression evaluator - by Moneo - 07-15-2003, 03:54 AM
Expression evaluator - by na_th_an - 07-15-2003, 04:37 AM
Expression evaluator - by Moneo - 07-15-2003, 06:06 AM
Expression evaluator - by Nexinarus - 07-16-2003, 03:34 AM
Expression evaluator - by Agamemnus - 07-16-2003, 04:03 AM
Expression evaluator - by na_th_an - 07-16-2003, 04:14 AM
Expression evaluator - by Nexinarus - 07-16-2003, 04:52 AM
Expression evaluator - by Agamemnus - 07-16-2003, 05:57 AM
Expression evaluator - by Nexinarus - 07-16-2003, 06:29 AM
Expression evaluator - by Moneo - 07-20-2003, 11:39 PM
Expression evaluator - by Nexinarus - 07-21-2003, 09:49 AM
Expression evaluator - by oracle - 07-21-2003, 09:51 AM
Expression evaluator - by Nexinarus - 07-21-2003, 10:00 AM
Expression evaluator - by Blitz - 07-22-2003, 07:38 AM
Expression evaluator - by Agamemnus - 07-22-2003, 07:12 PM
Expression evaluator - by Nexinarus - 07-23-2003, 09:40 AM
Expression evaluator - by na_th_an - 07-23-2003, 03:04 PM
Expression evaluator - by Agamemnus - 07-23-2003, 05:03 PM
Expression evaluator - by na_th_an - 07-23-2003, 05:16 PM
Expression evaluator - by Nexinarus - 07-24-2003, 12:41 AM
Expression evaluator - by Agamemnus - 07-24-2003, 12:57 AM
Expression evaluator - by Nexinarus - 07-24-2003, 09:46 AM
Expression evaluator - by toonski84 - 07-24-2003, 10:03 AM
Expression evaluator - by Nexinarus - 07-24-2003, 10:06 AM
Expression evaluator - by toonski84 - 07-24-2003, 10:14 AM
Expression evaluator - by Nexinarus - 07-24-2003, 02:16 PM

Forum Jump:


Users browsing this thread: 2 Guest(s)