Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --------------------------------------------------------------
- ----------------- EQUATIONS D'OXYDOREDUCTION -----------------
- --------------------------------------------------------------
- -- De Antoine Appia, TI-Planet/Omnimaga : AnToX98, antoineappia@hotmail.fr
- -- © Copyright 2014
- --------------------------------------------------------------
- local w, h, opt, input, Chembox, ww, hh, P, choice, firstequ, secequ, equ
- function on.construction()
- w, h = 318, 212
- input = true
- Chembox = { D2Editor.newRichText(), D2Editor.newRichText(), D2Editor.newRichText(), D2Editor.newRichText() }
- ww, hh = 60, 23
- Chembox[1]:resize(ww, hh) Chembox[2]:resize(ww, hh) Chembox[3]:resize(ww, hh) Chembox[4]:resize(ww, hh)
- P = { 10, 40, 85, 40, 10, 83, 85, 83 }
- for i = 1, #Chembox do
- Chembox[i]:move(P[2 * i - 1], P[(2 * i)])
- Chembox[i]:createChemBox()
- Chembox[i]:setFontSize(10)
- Chembox[i]:registerFilter{
- charIn = function(char)
- local t, c, s = Chembox[i]:getExpressionSelection()
- return (c == 0 or c == t:ulen())
- end,
- backspaceKey = function()
- local t, c, s = Chembox[i]:getExpressionSelection()
- return (c == 7)
- end,
- enterKey = function()
- Chembox[i]:setFocus(false)
- on.enterKey()
- end,
- arrowKey = function(key)
- local t, c, s = Chembox[i]:getExpressionSelection()
- if key == "right" and c == t:ulen() then
- if i == 1 then Chembox[2]:setFocus(true)
- elseif i == 3 then Chembox[4]:setFocus(true)
- elseif i == 2 then Chembox[2]:setFocus(false) opt = 1
- elseif i == 4 then Chembox[4]:setFocus(false) opt = 2
- end
- elseif key == "left" and c == 0 then
- if i == 2 then Chembox[1]:setFocus(true)
- elseif i == 4 then Chembox[3]:setFocus(true)
- end
- elseif key == "up" then
- if i == 3 then Chembox[1]:setFocus(true)
- elseif i == 4 then Chembox[2]:setFocus(true)
- end
- elseif key == "down" then
- if i == 1 then Chembox[3]:setFocus(true)
- elseif i == 2 then Chembox[4]:setFocus(true)
- end
- end
- return false
- end,
- tabKey = function()
- if i <= 3 then
- Chembox[i + 1]:setFocus(true)
- else
- Chembox[4]:setFocus(false)
- opt = 1
- end
- end,
- escapeKey = function()
- local t, c, s = Chembox[i]:getExpressionSelection()
- return (c == 7)
- end
- }
- end
- choice = { 1, 1 }
- opt = nil
- Chembox[1]:setFocus(true)
- end
- --------------------------------------------------------------
- function on.timer()
- end
- --------------------------------------------------------------
- function on.arrowKey(key)
- if key == "right" then
- elseif key == "left" then
- if opt == 1 then Chembox[2]:setFocus(true) opt = nil end
- if opt == 2 then Chembox[4]:setFocus(true) opt = nil end
- elseif key == "up" and opt == 2 then
- opt = 1
- elseif key == "down" and opt == 1 then
- opt = 2
- end
- platform.window:invalidate()
- end
- --------------------------------------------------------------
- function on.enterKey()
- if input then
- input = false
- local table = {}
- for i = 1, #Chembox do
- table[#table + 1] = Chembox[i]:getExpressionSelection()
- end
- firstequ = Balance(table[1], table[2])
- secequ = Balance(table[3], table[4])
- platform.window:invalidate()
- end
- end
- function Balance(e1, e2)
- local a
- if choice[2] == 1 then a = "+ H^1" elseif choice[2] == 2 then a = "+ HO^-1" end
- local str = e1:sub(8, #e1 - 1) .. "+^-1" .. "=" .. e2:sub(8, #e2 - 1)
- str = replace(str)
- str = math.evalStr('balance("' .. str .. '")')
- if not str or str:sub(2, 3) == "Er" then
- if e1:find("H₂O") or e2:find("H₂O") then
- equ = e1:sub(8, #e1 - 1) .. a .. "+^-1" .. "=" .. e2:sub(8, #e2 - 1)
- else
- equ = e1:sub(8, #e1 - 1) .. a .. "+^-1" .. "=" .. e2:sub(8, #e2 - 1) .. "+H2O"
- end
- equ = replace(equ)
- equ = math.evalStr('balance("' .. equ .. '")')
- else
- equ = str
- end
- equ = equ:gsub("\"", "")
- equ = avoidNegatifCoeffs(equ)
- equ = addElectron(equ)
- return equ
- end
- function replace(equ)
- local tn = { "¹", "²", "³", "⁴", "⁵", "⁶", "⁷", "⁸", "⁹", "¹⁰" }
- for i = 1, 10 do
- equ = equ:gsub(tn[i] .. "⁻", "^-" .. tostring(i))
- equ = equ:gsub(tn[i] .. "⁺", "^" .. tostring(i))
- end
- equ = equ:gsub("⁻", "^-1")
- equ = equ:gsub("⁺", "^1")
- local ti = { "₁", "₂", "₃", "₄", "₅", "₆", "₇", "₈", "₉" }
- equ = equ:gsub("₀", "0")
- for i = 1, 9 do
- equ = equ:gsub(ti[i], i)
- end
- return equ
- end
- function avoidNegatifCoeffs(equ)
- local m = equ:ufind("→")
- local left = equ:usub(1, m - 2)
- local right = equ:usub(m + 2, equ:ulen())
- local tleft, tright
- for i = 1, left:ulen() do
- if left:usub(i, i) == "−" then
- for k = i, left:ulen() do
- if left:usub(k, k) == "+" or k == #left then
- tleft = { i, k }
- right = right .. " " .. left:usub(i, k - 1):gsub("−", "+ ")
- end
- end
- end
- end
- if tleft then
- left = left:usub(1, tleft[1] - 1) .. left:usub(tleft[2] + 1, left:ulen())
- end
- for i = 1, right:ulen() do
- if right:usub(i, i) == "−" then
- for k = i, left:ulen() do
- if right:usub(k, k) == "+" or k == #right then
- tright = { i, k }
- left = left .. " " .. right:usub(i, k - 1):gsub("−", "+ ")
- end
- end
- end
- end
- if tright then
- right = right:usub(1, tright[1] - 1) .. right:usub(tright[2] + 1, right:ulen())
- end
- return left .. " = " .. right
- end
- --[[
- function addElectron(equ)
- local m = equ:ufind("=")
- local left = equ:usub(1, m - 2)
- local right = equ:usub(m + 2, equ:ulen())
- for i = 1, left:ulen() do
- if isCharge(left:usub(i, i)) == true then
- for j = i, 1, -1 do
- if string.byte(left:usub(j, j)) >= 65 and string.byte(left:usub(j, j)) <= 90 or string.byte(left:usub(j, j)) >= 97 and string.byte(left:usub(j, j)) <= 122 or isNumb(left:usub(j, j)) == true then
- break
- end
- if isCharge(left:usub(j, j)) == false then
- electronpos = j
- break
- end
- if left:usub(j, j) == "+" then
- break
- end
- end
- end
- if electronpos then
- break
- end
- end
- print(electronpos)
- left = left:usub(1, electronpos - 1) .. "e" .. left:usub(electronpos + 1, left:ulen())
- equ = left .. " = " .. right
- return equ
- end
- --]]
- function isCoeff(str)
- local ti = { "1", "2", "3", "4", "5", "6", "7", "8", "9", " " }
- for k = 1, #ti do
- if ti[k] == str then
- return true
- end
- end
- return false
- end
- function addElectron(equ)
- for i = 1, equ:ulen() - 1 do
- if isCoeff(equ:usub(i, i)) then
- if equ:usub(i + 1, i + 1) == "⁻" then
- equ = equ:usub(1, i) .. "e" .. equ:usub(i + 1, equ:ulen())
- end
- end
- end
- return equ
- end
- function isCharge(str)
- local tn = { "¹", "²", "³", "⁴", "⁵", "⁶", "⁷", "⁸", "⁹", "⁻", "⁺" }
- for k = 1, #tn do
- if tn[k] == str then
- return true
- end
- end
- return false
- end
- function isNumb(str)
- local ti = { "₁", "₂", "₃", "₄", "₅", "₆", "₇", "₈", "₉", "₀" }
- for k = 1, #ti do
- if ti[k] == str then
- return true
- end
- end
- return false
- end
- function string.ulen(s) -- count the number of non-continuing bytes
- return select(2, s:gsub('[^\128-\193]', ''))
- end
- function string.ufind(s)
- for i = 1, str:ulen() do
- if str:usub(i, i) == s then
- return i
- end
- end
- return nil
- end
- --------------------------------------------------------------
- function on.tabKey()
- if opt == 1 then opt = 2
- elseif opt == 2 then Chembox[1]:setFocus(true) opt = nil
- end
- end
- --------------------------------------------------------------
- function on.escapeKey()
- if choice[opt] == 1 then choice[opt] = 2
- elseif choice[opt] == 2 then choice[opt] = 1
- end
- platform.window:invalidate()
- end
- --------------------------------------------------------------
- function on.paint(gc)
- gc:drawLine(0, 20, 318, 20)
- gc:drawLine(73, 58, 80, 45)
- gc:drawLine(73, 101, 80, 88)
- gc:setColorRGB(0, 0, 200)
- gc:setFont("serif", "b", 11)
- gc:drawString("ÉQUATION D'OXYDORÉDUCTION", w / 2 - gc:getStringWidth("ÉQUATION D'OXYDORÉDUCTION") / 2, 0)
- gc:setColorRGB(0, 150, 0)
- for i = 1, 4 do
- gc:drawRect(P[2 * i - 1] - 1, P[(2 * i)] - 1, ww + 1, hh + 1)
- end
- gc:setFont("sansserif", "b", 8)
- gc:drawString("COUPLE 1", 10, 24)
- gc:drawString("COUPLE 2", 10, 67)
- gc:setColorRGB(0, 0, 0)
- gc:setFont("sansserif", "b", 8)
- gc:drawString("Reactifs", 210, 25)
- gc:drawString("Milieu de la réaction", 188, 69)
- gc:setColorRGB(200, 200, 200)
- if opt == 1 then
- gc:setColorRGB(200, 200, 255)
- end
- gc:fillRoundRect(230, 52, 100, 25, 10)
- gc:setColorRGB(200, 200, 200)
- if opt == 2 then
- gc:setColorRGB(200, 200, 255)
- end
- gc:fillRoundRect(230, 96, 100, 25, 10)
- gc:setColorRGB(0, 0, 0)
- gc:drawRoundRect(230, 52, 100, 25, 10)
- gc:drawRoundRect(230, 96, 100, 25, 10)
- gc:setFont("serif", "b", 10)
- gc:drawString("O₁/R₂", 195, 43)
- gc:drawString("O₂/R₁", 236, 43)
- gc:drawString("ACI", 197, 87)
- gc:drawString("BAS", 238, 87)
- gc:setColorRGB(150, 0, 0)
- if choice[1] == 1 then
- gc:drawRect(190, 44, 40, 16, 10)
- else
- gc:drawRect(230, 44, 40, 16, 10)
- end
- if choice[2] == 1 then
- gc:drawRect(190, 88, 40, 16, 10)
- else
- gc:drawRect(230, 88, 40, 16, 10)
- end
- if input then
- gc:setFont("sansserif", "r", 9)
- gc:setColorRGB(0, 0, 0)
- gc:drawString([[Appuyez sur [enter] pour valider vos entrées ou sur
- [esc] pour changer le type/mileu de réaction]], 20, 140)
- else
- gc:setFont("serif", "b", 10)
- gc:setColorRGB(200, 0, 0)
- gc:drawString("Réaction 1 : " .. firstequ, 10, 115)
- gc:drawString("Réaction 2 : " .. secequ, 10, 135)
- gc:setFont("serif", "b", 6)
- gc:setColorRGB(0, 100, 0)
- local str = [[Au cours d'une équation d'oxydoréduction, il y a autant d'électrons
- captés par l'oxydant que d'électrons cédés par le réducteur. ]]
- gc:drawString(str, 18, 155)
- gc:setColorRGB(0, 0, 100)
- gc:drawLine(32, 183, w - 32, 183)
- end
- end
- --------------------------------------------
- -- BetterLuaAPI, By Adriweb
- --------------------------------------------
- local function drawRoundRect(gc, x, y, wd, ht, rd) -- wd = width, ht = height, rd = radius of the rounded corner
- local x = x - wd / 2 -- let the center of the square be the origin (x coord)
- local y = y - ht / 2 -- same for y coord
- if rd > ht / 2 then rd = ht / 2 end -- avoid drawing cool but unexpected shapes. This will draw a circle (max rd)
- gc:drawLine(x + rd, y, x + wd - (rd), y)
- gc:drawArc(x + wd - (rd * 2), y + ht - (rd * 2), rd * 2, rd * 2, 270, 90)
- gc:drawLine(x + wd, y + rd, x + wd, y + ht - (rd))
- gc:drawArc(x + wd - (rd * 2), y, rd * 2, rd * 2, 0, 90)
- gc:drawLine(x + wd - (rd), y + ht, x + rd, y + ht)
- gc:drawArc(x, y, rd * 2, rd * 2, 90, 90)
- gc:drawLine(x, y + ht - (rd), x, y + rd)
- gc:drawArc(x, y + ht - (rd * 2), rd * 2, rd * 2, 180, 90)
- end
- local function fillRoundRect(gc, x, y, wd, ht, rd) -- wd = width, ht = height, rd = radius
- if rd > ht / 2 then rd = ht / 2 end -- avoid drawing cool but unexpected shapes. This will draw a circle (max rd)
- gc:fillPolygon({ (x - wd / 2), (y - ht / 2 + rd), (x + wd / 2), (y - ht / 2 + rd), (x + wd / 2), (y + ht / 2 - rd), (x - wd / 2), (y + ht / 2 - rd), (x - wd / 2), (y - ht / 2 + rd) })
- gc:fillPolygon({ (x - wd / 2 - rd + 1), (y - ht / 2), (x + wd / 2 - rd + 1), (y - ht / 2), (x + wd / 2 - rd + 1), (y + ht / 2), (x - wd / 2 + rd), (y + ht / 2), (x - wd / 2 + rd), (y - ht / 2) })
- local x = x - wd / 2 -- let the center of the square be the origin (x coord)
- local y = y - ht / 2 -- same
- gc:fillArc(x + wd - (rd * 2), y + ht - (rd * 2), rd * 2, rd * 2, 1, -91)
- gc:fillArc(x + wd - (rd * 2), y, rd * 2, rd * 2, -2, 91)
- gc:fillArc(x, y, rd * 2, rd * 2, 85, 95)
- gc:fillArc(x, y + ht - (rd * 2), rd * 2, rd * 2, 180, 95)
- end
- -- Credits to Jim Bauwens :)
- -- See http://inspired-lua.org/index.php/2013/05/how-to-add-your-own-functions-to-gc/
- function AddToGC(key, func)
- local gcMetatable = platform.withGC(getmetatable)
- gcMetatable[key] = func
- end
- AddToGC("drawRoundRect", drawRoundRect)
- AddToGC("fillRoundRect", fillRoundRect)
- --------------------------------------------
- function myErrorHandler(line, errMsg, callStack, locals)
- print("Error handled ! ", errMsg)
- return true -- let the script continue
- end
- platform.registerErrorHandler(myErrorHandler)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement