Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Math HW helper thingy. Features:
- -distance formula (1D or 2D)
- -pythagorean theorem
- -midpoint formula
- MODES:
- 1 : P. Theorem
- 2 : Distance Formula
- 3 : Midpoint Formula
- ]]
- local maxMode = 3
- local mode = 0
- function comma(amount)
- local formatted = tostring(amount)
- while true do
- formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
- if k == 0 then
- break
- end
- end
- return formatted
- end
- function round(number)
- return tostring(number):find("%.") and tostring(number):sub(1, tostring(number):find("%.") + 3) or tostring(number)
- end
- function promptReturn()
- io.write("return to menu? YES/NO -> ")
- local reply = io.read()
- if reply:lower():find("yes") then
- io.write("\n")
- mode = 0
- end
- end
- function unpackPar(par)
- local isolate = {}
- local row = ""
- local lastChar;
- par = par:gsub("%s+", "")
- for char = 1, par:len() do
- if lastChar then
- if tonumber(par:sub(char, char)) then
- row = row .. par:sub(char, char)
- elseif row:len() > 0 then
- table.insert(isolate, row)
- row = ""
- end
- end
- lastChar = par:sub(char, char)
- end
- return unpack(isolate)
- end
- function handleMath(mode, input)
- return (
- mode == 1 and
- function()
- local a, b, c;
- io.write("a -> ") a = io.read()
- io.write("b -> ") b = io.read()
- io.write("c -> ") c = io.read()
- print (
- a == "?" and
- "\na = " .. comma(round(math.sqrt((c ^ 2) - (b ^ 2)))) .. " || root( " .. comma((c ^ 2) - (b ^ 2)) .. " )"
- or b == "?" and
- "\nb = " .. comma(round(math.sqrt((c ^ 2) - (a ^ 2)))) .. " || root( " .. comma((c ^ 2) - (a ^ 2)) .. " )"
- or c == "?" and
- "\nc = " .. comma(round(math.sqrt((a ^ 2) + (b ^ 2)))) .. " || root( " .. comma((a ^ 2) + (b ^ 2)) .. " )"
- or "\n---> syntax error <---"
- )
- promptReturn()
- end
- or mode == 2 and
- function()
- local coord1, coord2, answer, x1, y1, x2, y2;
- io.write("point 1 -> ") coord1 = io.read()
- io.write("point 2 -> ") coord2 = io.read()
- x1, y1 = unpackPar(coord1)
- x2, y2 = unpackPar(coord2)
- answer = ((x2 - x1) ^ 2) + ((y2 - y1) ^ 2)
- print (
- not (x1 and y1 and x2 and y2) and
- "\n---> syntax error <---"
- or
- "\ndistance = " .. comma(round(math.sqrt(answer))) .. " || root( " .. comma(answer) .. " ) "
- )
- promptReturn()
- end
- or mode == 3 and
- function()
- local coord1, coord2, answer, type, x1, y1, x2, y2;
- io.write("Type of midpoint? (1D or 2D) -> ") type = io.read()
- if type:lower():find("1d") then
- io.write("position 1 -> ") coord1 = io.read()
- io.write("position 2 -> ") coord2 = io.read()
- print (
- not (tonumber(coord1) and tonumber(coord2)) and
- "\n---> syntax error <---"
- or
- "\nmidpoint = " .. comma(round(0.5 * (tonumber(coord1) + tonumber(coord2))))
- )
- promptReturn()
- else
- io.write("point 1 -> ") coord1 = io.read()
- io.write("point 2 -> ") coord2 = io.read()
- x1, y1 = unpackPar(coord1)
- x2, y2 = unpackPar(coord2)
- print (
- not (x1 and y1 and x2 and y2) and
- "\n---> syntax error <---"
- or
- "\nmidpoint = (" .. comma(round((x1 + x2) / 2)) .. ", " .. comma(round((y1 + y2) / 2)) .. ")"
- )
- promptReturn()
- end
- end
- )()
- end
- function changeMode(mode)
- return (tonumber(mode) > -1 and tonumber(mode) <= maxMode) and tonumber(mode) or 0
- end
- while true do
- if mode == 0 then
- print(
- "Math HW Helper by David. Choose a mode:\n" ..
- "\t1) Pythagorean Theorem\n" ..
- "\t2) Distance Formula\n" ..
- "\t3) Midpoint Formula\n"
- )
- mode = changeMode(io.read())
- else
- handleMath(mode)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment