Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Warn All, OutputDebug
- SetBatchLines -1
- equation := "3 + x * 1 = 2 + y"
- r := parseEq(equation)
- OutputDebug % equation "`n" r[1] " = " r[2] "`n`n"
- equation := "3 + x * 1 = 3 * 3 + 2 - y * 2 - 8 "
- r := parseEq(equation)
- OutputDebug % equation "`n" r[1] " = " r[2] "`n`n"
- equation := "3 + x * 1 = 5 - 4 + 3y"
- r := parseEq(equation)
- OutputDebug % equation "`n" r[1] " = " r[2] "`n`n"
- equation := "3 + x * 1 = 10 * 5 - 4 - 8 + y/2"
- r := parseEq(equation)
- OutputDebug % equation "`n" r[1] " = " r[2] "`n`n"
- parseEq(equation)
- {
- ; Remove spaces
- equation := StrReplace(equation, " ")
- ; Split by equality
- equation := StrSplit(equation, "=")
- eq1 := equation[1], eq2 := equation[2]
- ; Get precedent operations (mult/div)
- precedent := [], p := 1
- while p := RegExMatch(eq2, "(-?\w+[*|\/]-?\w+)", match, p)
- {
- precedent.Push(match1)
- p += StrLen(match)
- }
- ; Replace in the equation
- for i,op in precedent
- {
- parts := StrSplit(op, ["/", "*"])
- fn := InStr(op, "/") ? "div" : "mult"
- res := fn.Call(parts[1], parts[2])
- if res ; If we have a result, means no variable
- {
- if !InStr(res, "-")
- res := "+" res
- ; We need to remove. If we do now, the lops misbehaves
- precedent[i] := "" ; Emptying, then delete the empties.
- }
- eq2 := StrReplace(eq2, op, res)
- }
- ; Remove the empty ones
- for i,val in precedent
- if !val
- precedent.Delete(i)
- ; Get parts for eq2
- parts2 := [], p := 1
- while p := RegExMatch(eq2, "([+|-]?\w+)", match, p)
- parts2.Push(match1)
- , p += StrLen(match)
- ; Arithmetical inversion
- for i,part in parts2
- {
- if (part ~= "i)[a-z]") ; Skip variables
- continue
- rem := part
- part := StrReplace(part, "+") * -1
- if !InStr(part, "-")
- part := "+" part
- eq1 .= part
- eq2 := StrReplace(eq2, rem,,, 1)
- }
- ; Restore precedent operations
- for i,op in precedent
- eq2 .= op
- ; Return each equation
- return [eq1, eq2]
- }
- div(a, b)
- {
- return a / b
- }
- mult(a, b)
- {
- return a * b
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement