Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- w, h = term.getSize()
- local stringW = ""
- for i = 1, w - 2 do
- stringW = stringW .. " "
- end
- function initializeCalc()
- term.setBackgroundColor(colors.green)
- term.clear()
- term.setCursorPos(w / 2 - 4, 1)
- term.write("Calculator")
- term.setCursorPos(w / 2 - 9, 2)
- term.write("+ - * / ** ^ %")
- term.setCursorPos(2, 4)
- term.setBackgroundColor(colors.lime)
- term.write(stringW)
- term.setCursorPos(1, h)
- term.setBackgroundColor(colors.blue)
- term.write("Graph")
- term.setCursorPos(w - 3, h)
- term.setBackgroundColor(colors.red)
- term.write("Back")
- end
- local emphasis = {}
- emphasis["+"] = true
- emphasis["-"] = true
- emphasis["/"] = true
- emphasis["*"] = true
- emphasis["^"] = true
- emphasis["%"] = true
- emphasis["("] = true
- emphasis[")"] = true
- emphasis["="] = true
- emphasis["<"] = true
- emphasis[">"] = true
- emphasis["!"] = true
- local letters = {}
- letters["a"] = true
- letters["b"] = true
- letters["c"] = true
- letters["d"] = true
- letters["e"] = true
- letters["f"] = true
- letters["g"] = true
- letters["h"] = true
- letters["i"] = true
- letters["j"] = true
- letters["k"] = true
- letters["l"] = true
- letters["m"] = true
- letters["n"] = true
- letters["o"] = true
- letters["p"] = true
- letters["q"] = true
- letters["r"] = true
- letters["s"] = true
- letters["t"] = true
- letters["u"] = true
- letters["v"] = true
- letters["w"] = true
- letters["x"] = true
- letters["y"] = true
- letters["z"] = true
- local dontuse = {}
- dontuse["~"] = true
- dontuse["@"] = true
- dontuse["`"] = true
- dontuse["$"] = true
- dontuse["#"] = true
- dontuse["&"] = true
- dontuse[";"] = true
- dontuse[":"] = true
- dontuse["\""] = true
- dontuse["'"] = true
- dontuse["_"] = true
- dontuse["?"] = true
- dontuse[","] = true
- dontuse["|"] = true
- dontuse["\\"] = true
- dontuse["["] = true
- dontuse["]"] = true
- dontuse["{"] = true
- dontuse["}"] = true
- local printtype = nil
- local globalvars = {}
- local calcdone = false
- while not calcdone do
- local gInput = nil
- local fInput = nil
- local calculate = nil
- initializeCalc()
- local currentY = 1
- local input = {}
- input[1] = ""
- local inputDone = false
- while not inputDone do
- pressed = nil
- term.setCursorPos(2, #input + 4)
- term.setBackgroundColor(colors.green)
- term.write(stringW)
- term.setBackgroundColor(colors.lime)
- for i = 1, #input do
- term.setCursorPos(2, i + 3)
- term.write(stringW)
- term.setCursorPos(2, i + 3)
- for n = 1, #input[i] do
- letter = string.sub(input[i], n, n)
- if emphasis[letter] == true then
- term.setTextColor(colors.yellow)
- elseif letters[letter] == true then
- term.setTextColor(colors.pink)
- else
- term.setTextColor(colors.white)
- end
- term.write(letter)
- end
- end
- pressed = {os.pullEvent()} -----------------------start---V
- if pressed[1] == "mouse_click" and pressed[2] == 1 and pressed[3] >= w - 3 and pressed[3] <= w and pressed[4] == h then
- calcdone = true
- break
- elseif pressed[1] == "mouse_click" and pressed[2] == 1 and pressed[3] >= 1 and pressed[3] <= 5 and pressed[4] == h then
- while true do
- local graphPosX = 0
- local graphPosY = 0
- w, h = term.getSize()
- term.setBackgroundColor(colors.blue)
- term.clear()
- term.setCursorPos(w/2 - 4, 1)
- term.write("Grapher")
- term.setCursorPos(1, 3)
- term.write("y=")
- term.setCursorPos(w - 3, h)
- term.setBackgroundColor(colors.blue)
- term.write("Back")
- pressed = {os.pullEvent()} -------start graph --v
- gInput = nil
- if pressed[1] == "mouse_click" and pressed[2] == 1 and pressed[4] == 3 then
- term.setCursorPos(3, 3)
- gInput = read()
- elseif pressed[1] == "mouse_click" and pressed[2] == 1 and pressed[3] >= w - 3 and pressed[3] <= w and pressed[4] == h then
- initializeCalc()
- break
- end
- if gInput then
- oTemp = fs.open("_runGraph", "w")
- oTemp.writeLine("graphtab = {}")
- oTemp.writeLine("for x = -100, 100 do")
- oTemp.writeLine(" graphtab[x] = " .. tostring(gInput))
- oTemp.writeLine("end")
- oTemp.flush()
- oTemp.close()
- os.loadAPI("_runGraph")
- local donegraphing = false
- while not donegraphing do
- term.setBackgroundColor(colors.black)
- term.clear()
- for i = graphPosX, w - graphPosX do
- paintutils.drawLine(i - graphPosX, h - _runGraph.graphtab[i] + 1 + graphPosY, i + 1 - graphPosX, h - _runGraph.graphtab[i + 1] + 1 + graphPosY, colors.green)
- term.setCursorPos(1, 1)
- end
- --[[for i, v in pairs(_runGraph.graphtab) do
- paintutils.drawPixel(i, h - v + 1, colors.blue)
- term.setCursorPos(1, 1)
- end]]--
- shell.run("delete _runGraph")
- pressed = {os.pullEvent()}
- if pressed[1] == "key" then
- if pressed[2] == 200 then --up
- graphPosY = graphPosY - 1
- elseif pressed[2] == 208 then --down
- graphPosY = graphPosY + 1
- elseif pressed[2] == 203 then --left
- graphPosX = graphPosX + 1
- elseif pressed[2] == 205 then --right
- graphPosX = graphPosX - 1
- end
- end
- end
- end
- end
- end
- if #input[currentY] == w - 2 then
- currentY = currentY + 1
- input[currentY] = ""
- end
- if pressed[1] == "char" and not dontuse[pressed[2]] then
- input[currentY] = input[currentY] .. pressed[2]
- elseif pressed[1] == "key" then
- if pressed[2] == 14 then --delete
- if input[currentY] == "" and #input ~= 1 then
- input[currentY] = nil
- currentY = currentY - 1
- end
- input[currentY] = string.sub(input[currentY], 0, #input[currentY] - 1)
- elseif pressed[2] == 28 then --enter
- inputDone = true
- end
- end
- end
- if calcdone then break end
- local fInput = ""
- for i = 1, #input do
- fInput = fInput .. input[i]
- end
- local var = nil
- local varnum = nil
- var = string.find(fInput, "=")
- if var then
- printtype = "var"
- var = nil
- varnum = 0
- for i, v in pairs(letters) do
- local stringstart = 0
- while string.find(fInput, i, stringstart) do
- var = string.find(fInput, i, stringstart)
- if var then
- stringstart = var + 1
- varnum = varnum + 1
- end
- end
- end
- for i = 1, #tostring(fInput) do
- for k, e in pairs(letters) do
- if string.sub(fInput, i, i) == k then
- var = i
- break
- end
- end
- if var then
- break
- end
- end
- varnum = string.sub(fInput, var - 1, varnum)
- --term.setBackgroundColor(colors.blue)
- --print(varnum)
- globalvars[#globalvars + 1] = varnum
- --fInput = string.sub(#tostring(fInput), 1, var - 1) .. " local " .. string.sub(tostring(fInput), var, #tostring(fInput))
- else
- fInput = "answer = " .. tostring(fInput)
- printtype = "math"
- end
- answer = nil
- calculate = nil
- calculate = loadstring(fInput)
- calculate()
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- if printtype == "math" then
- term.setCursorPos(w / 2 - #tostring(answer) / 2, h - 1)
- term.write(answer)
- elseif printtype == "var" then
- term.setCursorPos(w / 2 - #tostring(varnum) / 2, h - 1)
- term.write(fInput)
- end
- sleep(2)
- end
- local vnil = nil
- for i, v in pairs(globalvars) do
- --print(v .. " = nil")
- vnil = loadstring(v .. " = nil")
- vnil()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement