SHOW:
|
|
- or go back to the newest paste.
| 1 | --[[ | |
| 2 | Math HW helper thingy. Features: | |
| 3 | -distance formula (1D or 2D) | |
| 4 | -pythagorean theorem | |
| 5 | -midpoint formula | |
| 6 | ||
| 7 | MODES: | |
| 8 | 1 : P. Theorem | |
| 9 | 2 : Distance Formula | |
| 10 | 3 : Midpoint Formula | |
| 11 | ]] | |
| 12 | ||
| 13 | local maxMode = 3 | |
| 14 | local mode = 0 | |
| 15 | ||
| 16 | function comma(amount) | |
| 17 | local formatted = tostring(amount) | |
| 18 | while true do | |
| 19 | formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2') | |
| 20 | - | if k == 0 then |
| 20 | + | if k == 0 then |
| 21 | - | break |
| 21 | + | break |
| 22 | - | end |
| 22 | + | end |
| 23 | end | |
| 24 | return formatted | |
| 25 | end | |
| 26 | ||
| 27 | function round(number) | |
| 28 | return tostring(number):find("%.") and tostring(number):sub(1, tostring(number):find("%.") + 3) or tostring(number)
| |
| 29 | end | |
| 30 | ||
| 31 | function promptReturn() | |
| 32 | io.write("return to menu? YES/NO -> ")
| |
| 33 | local reply = io.read() | |
| 34 | if reply:lower():find("yes") then
| |
| 35 | io.write("\n")
| |
| 36 | mode = 0 | |
| 37 | end | |
| 38 | end | |
| 39 | ||
| 40 | function unpackPar(par) | |
| 41 | local isolate = {}
| |
| 42 | local row = "" | |
| 43 | local lastChar; | |
| 44 | par = par:gsub("%s+", "")
| |
| 45 | for char = 1, par:len() do | |
| 46 | if lastChar then | |
| 47 | if tonumber(par:sub(char, char)) then | |
| 48 | row = row .. par:sub(char, char) | |
| 49 | elseif row:len() > 0 then | |
| 50 | table.insert(isolate, row) | |
| 51 | row = "" | |
| 52 | end | |
| 53 | end | |
| 54 | lastChar = par:sub(char, char) | |
| 55 | end | |
| 56 | return unpack(isolate) | |
| 57 | end | |
| 58 | ||
| 59 | function handleMath(mode, input) | |
| 60 | return ( | |
| 61 | mode == 1 and | |
| 62 | function() | |
| 63 | local a, b, c; | |
| 64 | io.write("a -> ") a = io.read()
| |
| 65 | io.write("b -> ") b = io.read()
| |
| 66 | io.write("c -> ") c = io.read()
| |
| 67 | print ( | |
| 68 | a == "?" and | |
| 69 | "\na = " .. comma(round(math.sqrt((c ^ 2) - (b ^ 2)))) .. " || root( " .. comma((c ^ 2) - (b ^ 2)) .. " )" | |
| 70 | or b == "?" and | |
| 71 | "\nb = " .. comma(round(math.sqrt((c ^ 2) - (a ^ 2)))) .. " || root( " .. comma((c ^ 2) - (a ^ 2)) .. " )" | |
| 72 | or c == "?" and | |
| 73 | "\nc = " .. comma(round(math.sqrt((a ^ 2) + (b ^ 2)))) .. " || root( " .. comma((a ^ 2) + (b ^ 2)) .. " )" | |
| 74 | or "\n---> syntax error <---" | |
| 75 | ) | |
| 76 | promptReturn() | |
| 77 | end | |
| 78 | or mode == 2 and | |
| 79 | function() | |
| 80 | local coord1, coord2, answer, x1, y1, x2, y2; | |
| 81 | io.write("point 1 -> ") coord1 = io.read()
| |
| 82 | io.write("point 2 -> ") coord2 = io.read()
| |
| 83 | x1, y1 = unpackPar(coord1) | |
| 84 | x2, y2 = unpackPar(coord2) | |
| 85 | answer = ((x2 - x1) ^ 2) + ((y2 - y1) ^ 2) | |
| 86 | print ( | |
| 87 | not (x1 and y1 and x2 and y2) and | |
| 88 | "\n---> syntax error <---" | |
| 89 | or | |
| 90 | "\ndistance = " .. comma(round(math.sqrt(answer))) .. " || root( " .. comma(answer) .. " ) " | |
| 91 | ) | |
| 92 | promptReturn() | |
| 93 | end | |
| 94 | or mode == 3 and | |
| 95 | function() | |
| 96 | local coord1, coord2, answer, type, x1, y1, x2, y2; | |
| 97 | io.write("Type of midpoint? (1D or 2D) -> ") type = io.read()
| |
| 98 | if type:lower():find("1d") then
| |
| 99 | io.write("position 1 -> ") coord1 = io.read()
| |
| 100 | io.write("position 2 -> ") coord2 = io.read()
| |
| 101 | print ( | |
| 102 | not (tonumber(coord1) and tonumber(coord2)) and | |
| 103 | "\n---> syntax error <---" | |
| 104 | or | |
| 105 | "\nmidpoint = " .. comma(round(0.5 * (tonumber(coord1) + tonumber(coord2)))) | |
| 106 | ) | |
| 107 | promptReturn() | |
| 108 | else | |
| 109 | io.write("point 1 -> ") coord1 = io.read()
| |
| 110 | io.write("point 2 -> ") coord2 = io.read()
| |
| 111 | x1, y1 = unpackPar(coord1) | |
| 112 | x2, y2 = unpackPar(coord2) | |
| 113 | print ( | |
| 114 | not (x1 and y1 and x2 and y2) and | |
| 115 | "\n---> syntax error <---" | |
| 116 | or | |
| 117 | "\nmidpoint = (" .. comma(round((x1 + x2) / 2)) .. ", " .. comma(round((y1 + y2) / 2)) .. ")"
| |
| 118 | ) | |
| 119 | promptReturn() | |
| 120 | end | |
| 121 | end | |
| 122 | )() | |
| 123 | end | |
| 124 | ||
| 125 | function changeMode(mode) | |
| 126 | return (tonumber(mode) > -1 and tonumber(mode) <= maxMode) and tonumber(mode) or 0 | |
| 127 | end | |
| 128 | ||
| 129 | while true do | |
| 130 | if mode == 0 then | |
| 131 | print( | |
| 132 | "Math HW Helper by David. Choose a mode:\n" .. | |
| 133 | "\t1) Pythagorean Theorem\n" .. | |
| 134 | "\t2) Distance Formula\n" .. | |
| 135 | "\t3) Midpoint Formula\n" | |
| 136 | ) | |
| 137 | mode = changeMode(io.read()) | |
| 138 | else | |
| 139 | handleMath(mode) | |
| 140 | end | |
| 141 | end |