Posts: 3,368
Threads: 195
Joined: Jan 2003
i didn't implement subtraction.
Anyways, subtraction is just the same as adding a negative number. It's easier for computers. Maybe.
Anyways, why is one method harder than the second?
10000 - 999 = 10000 - 1000 + 1 = 9001.
However, my silly program says:
(10000 - 999)
(10000 - (990 + 9))
(10000 - (990 + 3 * 3))
(10000 - (165 * 6 + 3 * 3))
(10000 - (165 * 6 + 9))
(10000 - (990 + 9))
(10000 - 999)
9001
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.
Posts: 1,956
Threads: 65
Joined: Jun 2003
AGA, Now I'm thoroughly confused.
*****
Posts: 3,343
Threads: 83
Joined: Mar 2003
Quote:ORACLE,
I figured you were adding/subtracting strings.
You didn't answer my question of what method you're using to subtract. Is it that you're using Neo's routines and you don't really care?
*****
Yep
Though it's coded in only 20 lines or so, and I made an addition one as well as Neo's one.
The HARD part is the decimal places, currently it's giving me a lot of grief.
Posts: 1,956
Threads: 65
Joined: Jun 2003
ORACLE,
Regarding trouble with decimals. There must be a maximum number of decimal places that you can handle, right. Well then take any input number and normalize to the maximum number of places.
Example: Let's say the maximum decimal places is 4.
a) If the input is 1234, you normalze it to 12340000.
b) If the input is 56.78, you normalize it to 567800.
That way you arithmetic routines don't have to deal with decimales. The decimal point is implied. For output, you put the decimal point back in.
*****
Posts: 3,288
Threads: 167
Joined: Nov 2001
Nex: Just don't forget tto credit these guys in QB++. ;*)
j/k
Yeah, Adding the negative of a number is the same as subtraction. In fact I have yet to encounter a book which says...
Subtranction rule of equality or division rule of equality. ;*)
Posts: 294
Threads: 9
Joined: Oct 2002
@na_th_an:
Yeah your idea is really nice and small, no brackets/commands like SIN but real cool in 30 lines
.
Posts: 6,419
Threads: 74
Joined: Mar 2002
Well, it was just an idea on how to build the evaluator.. To add SINs and other unary expressions you only have to switch in the 1st IF branch, and to add parentheses wouldn't be very hard. Maybe I come out with something more complex.
Posts: 3,368
Threads: 195
Joined: Jan 2003
Parentheses are VERY hard.
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.
Posts: 6,419
Threads: 74
Joined: Mar 2002
Not really. Preprocess: For each parentheses make a recursive call with its contents, and replace the parentheses bit with the result.
Posts: 3,368
Threads: 195
Joined: Jan 2003
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.