# Interesting technique

May 20 2011 at 11:24 AM

Response to chain, guess, test, and revise

Writing a program within a program. I haven't seen that one before. It did work for most of the things I tried. It does hang when there is a decimal point in a co-efficient, for example, 1.1x=11. As you mentioned, it is not always exact. For x+1.1=11, it gives an answer arbitrarily close to the correct 9.9.

Thanks for participating. Here is my version which probably contains it's own share of bugs.

CLS
DIM t(20)
alf\$ = "abcdefghijklmnopqrstuvwxyz"
INPUT "Equation: "; e\$

'find the variable
le = LEN(e\$)
FOR a = 1 TO le
temp\$ = MID\$(e\$, a, 1)
IF INSTR(alf\$, temp\$) THEN v\$ = temp\$: EXIT FOR
NEXT

'separate left and right sides of equation
equ = INSTR(e\$, "=")
s\$(1) = LEFT\$(e\$, equ - 1)
s\$(2) = MID\$(e\$, equ + 1)

'insure that each side begins with sign + or -
FOR b = 1 TO 2
temp\$ = LEFT\$(s\$(b), 1)
IF (temp\$ <> "+") AND (temp\$ <> "-") THEN s\$(b) = "+" + s\$(b)

'find each term separately
le = LEN(s\$(b))
FOR a = 1 TO le
temp\$ = MID\$(s\$(b), a, 1)
IF temp\$ = "+" OR temp\$ = "-" THEN p = p + 1: t(p) = a
NEXT
p = p + 1
t(p) = le + 1
FOR a = 1 TO p - 1
t\$ = MID\$(s\$(b), t(a), t(a + 1) - t(a))

'combine variable co-efficients and also combine constants
IF INSTR(t\$, v\$) THEN
IF VAL(t\$) = 0 THEN t\$ = LEFT\$(t\$, 1) + "1" + MID\$(t\$, 2)
r = INSTR(t\$, v\$)
t\$ = LEFT\$(t\$, r - 1) + MID\$(t\$, r + 1)
q = INSTR(t\$, "/")
IF q THEN t = VAL(t\$) / VAL(MID\$(t\$, q + 1)) ELSE t = VAL(t\$)
IF b = 1 THEN var = var + t ELSE var = var - t
ELSE
q = INSTR(t\$, "/")
IF q THEN t = VAL(t\$) / VAL(MID\$(t\$, q + 1)) ELSE t = VAL(t\$)
IF b = 2 THEN cons = cons + t ELSE cons = cons - t
END IF
NEXT
p = 0
NEXT
PRINT v\$; " ="; cons / var
SYSTEM

 Respond to this message
Responses