Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --------------------------
- --Dubs's Text Calculator--
- ------Version: 1.9.1------
- --------------------------
- --[[notes
- lua function
- ]]--
- --options
- local usecolor = true
- local docutscenes = true
- local autoclear = false
- local autoclearinterval = 20
- local cutsceneinterval = 0.25
- local using_touchlua_app = true
- local createfiles = true
- --code
- require "draw"
- function print2(input)
- io.write(input.."\n")
- end
- function print(input)
- if input==nil then input="" end
- if input==true then input="true" end
- if input==false then input="false" end
- local file=io.open("calculator logs.txt","a+")
- if file:read()=="" then file:write(input)
- else do file:write("\n"..input) end end
- file:close()
- print2(input)
- end
- function clearlog()
- file=io.open("calculator logs.txt","w")
- file:write()
- file:close()
- end
- crashcount=0
- local match="[1234567890/-*^+().#%%]"
- local notmatch=[=[[qwertyuiopasdfghjklzxcvbnm:;$&@?!'"{}_\|~<>€£¥]]=]
- local x=0
- if using_touchlua_app and docutscenes then cutsceneinterval=cutsceneinterval*1000 end
- if not io.open("calculator logs.txt","r") and createfiles then
- file=io.open("calculator memory.txt","w")
- file:close()
- end
- if not io.open("calculator memory.txt","r") and createfiles then
- file=io.open("calculator memory.txt","w")
- file:write("-----------------\nCalculator Recall\n-----------------\n")
- file:close()
- end
- ::cls::
- os.execute("cls") clearlog()
- prev=nil
- function callable(f)
- return function(...)
- error, result = pcall(f, ...)
- if error then
- return true
- else
- return false
- end
- end
- end
- function get(getFile)
- local file=io.open(getFile,"r")
- local getOut=file:read("*a")
- file:close()
- print("[[[start get]]]")
- print()
- print(getOut)
- print()
- print("[[[end get]]]")
- end
- function slowprint(text,delay)
- delay=delay or 125
- for x=1,string.len(text) do
- char=string.sub(text,x,x)
- io.write(char)
- sleep(delay)
- end
- end
- function factorial(n)
- n=tonumber(n)
- return n > 0 and n * factorial(n-1) or 1
- end
- function percent(perc,tot)
- return (perc/100)*tot
- end
- function ee(zero,to)
- return zero*(10^to)
- end
- function simplify(calcin)
- a,b=calcin:match"(.+)/(.+)"u,v=a,b while v+0>0 do t=u u=v v=t%v end
- return a+0==a/u and a.."/"..b or a/u.."/"..b/u
- end
- function split(inputstr, sep)
- if sep == nil then
- sep = "%%"
- end
- local t={} ; i=1
- for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
- t[i] = str
- i = i + 1
- end
- return t
- end
- function IsPrime(n)
- for i = 2, n^(1/2) do
- if (n % i) == 0 then
- return false
- end
- end
- return true
- end
- function GetFileSize( filename )
- local fp = io.open( filename )
- if fp == nil then
- return nil
- end
- local filesize = fp:seek( "end" )
- fp:close()
- return filesize
- end
- function PrimeDecomp( n )
- local f = {}
- if IsPrime( n ) then
- f[1] = n
- return f
- end
- local i = 2
- repeat
- while n % i == 0 do
- f[#f+1] = i
- n = n / i
- end
- repeat
- i = i + 1
- until IsPrime( i )
- until n == 1
- return f
- end
- function Factors( n )
- local f = {}
- for i = 1, n/2 do
- if n % i == 0 then
- f[#f+1] = i
- end
- end
- f[#f+1] = n
- return f
- end
- if not using_touchlua_app or not callable(sleep) then
- local clock = os.clock
- function sleep(n) -- seconds
- local t0 = clock()
- while clock() - t0 <= n do end
- end
- end
- function print_r ( t )
- local print_r_cache={}
- local function sub_print_r(t,indent)
- if (print_r_cache[tostring(t)]) then
- print(indent.."*"..tostring(t))
- else
- print_r_cache[tostring(t)]=true
- if (type(t)=="table") then
- for pos,val in pairs(t) do
- if (type(val)=="table") then
- print(indent.."["..pos.."] => "..tostring(t).." {")
- sub_print_r(val,indent..string.rep(" ",string.len(pos)+8))
- print(indent..string.rep(" ",string.len(pos)+6).."}")
- else
- print(indent.."["..pos.."] => "..tostring(val))
- end
- end
- else
- print(indent..tostring(t))
- end
- end
- end
- sub_print_r(t," ")
- end
- if usecolor and using_touchlua_app then
- sys.setbgcolor(draw.black)
- sys.setcolor(draw.red)
- end
- if usecolor and using_touchlua_app then
- sys.setbgcolor(draw.black)
- sys.setcolor(draw.green)
- end
- if docutscenes then
- print("Booting") print("- - -")
- sleep(cutsceneinterval)
- os.execute("cls") clearlog()
- print("Booting") print("| - -")
- sleep(cutsceneinterval)
- os.execute("cls") clearlog()
- print("Booting") print("| | -")
- sleep(cutsceneinterval)
- os.execute("cls") clearlog()
- print("Booting") print("| | |")
- sleep(cutsceneinterval)
- end
- ::reclc::
- os.execute("cls")
- if not slowp then print("Enter an equation. * is multiply, / is divide, ^ followed by a number is an exponent. Type help to get advanced functions.") end
- if slowp==true then calcin=string.gsub(string.lower(calcin),"slowprint ","") local file=io.open("calculator logs.txt","r") log=file:read("*a") file:close() io.write("Enter an equation. * is multiply, / is divide, ^ followed by a number is an exponent. Type help to get advanced functions.") print(log) sleep(5000) slowprint(calcin,200) io.write("\n") slowp=false end
- clearlog()
- ::recalc::
- print("-------------")
- ::redocalc::
- calcin = io.read()
- if string.match(string.lower(calcin),"debug") then
- ::drawdebug::
- if usecolor and using_touchlua_app then
- sys.setbgcolor(draw.black)
- sys.setcolor(draw.red)
- end
- os.execute("cls")
- for i=1,100 do
- print("boot")
- sleep(10)
- end
- os.execute("cls")
- sleep(1000)
- print("Debug mode enabled. Anti-crash safeguards disabled while in this mode. Debug commands enabled while in this mode. Scary red font enabled while in this mode. Type exit to leave.")
- ::redebug::
- print("=============")
- io.write("debug> ")
- local debugin=io.read()
- debugin=string.lower(debugin)
- if string.match(debugin,"panic") and createfiles==true then
- file=io.open("calculator memory.txt","w")
- file:write("-----------------\nCalculator Recall\n-----------------\n")
- file:close()
- clearlog()
- goto drawdebug
- end
- if debugin=="exit" then
- if usecolor and using_touchlua_app then
- sys.setbgcolor(draw.black)
- sys.setcolor(draw.green)
- end
- goto cls
- end
- if debugin=="cls" then goto drawdebug end
- if debugin=="console" or debugin=="lua" then
- if usecolor and using_touchlua_app then
- sys.setcolor(draw.blue)
- os.execute("cls")
- while true do
- io.write("Lua> ")
- local luaIn=io.read()
- if luaIn=="exit" then break end
- load(luaIn)()
- end
- goto drawdebug
- end
- end
- if string.match(debugin,"get") then
- if string.match(debugin,"history") then
- get("calculator memory.txt")
- goto redebug
- elseif string.match(debugin,"logs") then
- get("calculator logs.txt")
- goto redebug
- end
- end
- if string.match(debugin,"sqrt") then
- debugin=string.lower(debugin)
- input=debugin
- debugin = string.gsub(debugin,"sqrt","")
- debugin=math.sqrt(debugin)
- print("="..debugin)
- glrb=debugin
- debugin=input
- goto redebug
- elseif string.match(debugin,"factors") then
- debugin=string.lower(debugin)
- glrb=debugin
- debugin = string.gsub(debugin,"factors","")
- debugin=Factors(debugin)
- print_r(debugin)
- goto redebug
- elseif string.match(debugin,"primefacs") then
- debugin=string.lower(debugin)
- glrb=debugin
- debugin = string.gsub(debugin,"primefacs","")
- debugin=tonumber(debugin)
- debugin=PrimeDecomp(debugin)
- print_r(debugin)
- goto redebug
- elseif string.match(debugin,"%%") then
- input=debugin
- tab=split(debugin)
- perc=tab[1]
- tot=tab[2]
- glrb=percent(perc,tot)
- print("="..glrb)
- debugin=input
- goto redebug
- elseif string.match(debugin,"ee") then
- input=debugin
- tab=split(debugin,"ee")
- zero=tab[1]
- to=tab[2]
- glrb=ee(zero,to)
- print("="..glrb)
- debugin=input
- goto redebug
- elseif string.match(debugin,"simplify") then
- input=debugin
- debugin=string.gsub(debugin,"simplify","")
- glrb=simplify(debugin)
- print("="..glrb)
- debugin=input
- goto redebug
- elseif string.match(debugin,"factorial") then
- input=debugin
- debugin=string.gsub(debugin,"factorial","")
- debugin=string.lower(debugin)
- glrb=factorial(debugin)
- print("="..glrb)
- debugin=input
- goto redebug
- elseif string.match(debugin,"intocm") then
- input=debugin
- debugin=string.gsub(debugin,"intocm","")
- glrb=debugin*2.54
- print("="..glrb)
- debugin=input
- goto redebug
- elseif string.match(debugin,"cm>in") then
- input=debugin
- debugin=string.gsub(debugin,"cm>in","")
- glrb=debugin/2.54
- print("="..glrb)
- debugin=input
- goto redebug
- elseif string.match(debugin,"ft>m") then
- input=debugin
- debugin=string.gsub(debugin,"ft>m","")
- glrb=debugin*3.28
- print("="..glrb)
- debugin=input
- goto redebug
- end
- func = assert(load("return " .. debugin))
- glrb = func()
- if string.match(glrb,"-") then
- print("=("..glrb..")")
- else do
- print("="..glrb)
- end
- end
- goto redebug
- end
- if calcin=="an equation" then
- os.execute("cls") clearlog()
- slowprint("Good job.") sleep(3000) slowprint("\nYou win.") sleep(3000) slowprint("\nI'm out.") sleep(3000) slowprint("\nGG.") sleep(3000) os.exit()
- end
- if prevcalcin==calcin then crashcount=crashcount+1 end
- if prevcalcin~=calcin then crashcount=0 end
- if crashcount>=10 then error("keyspam detected.") end
- prevcalcin = calcin
- if string.match(string.lower(calcin),"slowprint ") then slowp=true goto reclc end
- local file=io.open("calculator logs.txt","a+")
- file:write("\n"..calcin)
- file:close()
- calcin=string.lower(calcin)
- if string.match(calcin,"panic") and createfiles==true then
- file=io.open("calculator memory.txt","w")
- file:write("-----------------\nCalculator Recall\n-----------------\n")
- file:close()
- clearlog()
- goto cls
- elseif calcin=="cls" or calcin=="clear" then x=x+1 if x==20 and autoclear then os.execute("cls") clearlog() goto recalc else end goto cls
- elseif string.match(calcin,"#") and not prev then goto recalc
- elseif calcin=="clearmem" and createfiles==true then
- os.execute("cls") clearlog()
- print("Would you like to clear the calculator's memory file? \n(yes/no)")
- if string.lower(io.read())~="yes" then goto reclc end
- os.execute("cls") clearlog()
- print("Are you absolutely sure that you want to clear the calculator's memory file? \n(yes/no)")
- if string.lower(io.read())~="yes" then goto reclc end
- os.execute("cls") clearlog()
- print("Last chance to stop. Are you sure that you would like to clear the calculator's memory file? \n(yes/no)")
- if string.lower(io.read())~="yes" then goto reclc end
- os.execute("cls") clearlog()
- print("Alright. Clearing calculator's memory file...") print("- - -") sleep(cutsceneinterval) os.execute("cls") clearlog()
- print("Alright. Clearing calculator's memory file...") print("| - -") sleep(cutsceneinterval) os.execute("cls") clearlog()
- print("Alright. Clearing calculator's memory file...") print("| | -") sleep(cutsceneinterval) os.execute("cls") clearlog()
- print("Alright. Clearing calculator's memory file...") print("| | |") sleep(cutsceneinterval) os.execute("cls") clearlog()
- file=io.open("calculator memory.txt","w")
- file:write("-----------------\nCalculator Recall\n-----------------\n")
- file:close()
- print([=[Calculator's memory file "calculator memory.txt" has been cleared.]=])
- print("Press any button to continue.")
- io.read()
- goto cls
- elseif calcin=="" or calcin==" " then x=x+1 if x==20 and autoclear then os.execute("cls") clearlog() goto recalc else end goto recalc
- elseif string.match(calcin,",") then
- calcin=string.gsub(calcin,",","")
- elseif string.match(calcin,"=") then
- calcin=string.gsub(calcin,"=","")
- elseif calcin=="time" then
- os.execute("cls")
- local file=io.open("calculator logs.txt","r")
- log=file:read("*a")
- file:close()
- io.write("Enter an equation. * is multiply, / is divide, ^ followed by a number is an exponent. Type help to get advanced functions.")
- local log=string.gsub(log,"\ntime","")
- print(log)
- time=os.date("%I:%M:%S %p on %A, %B %d, %Y")
- print(time)
- goto recalc
- elseif calcin=="help" then print("-------------")print([=[time = get the date and time
- sqrt = type this, followed by a number, to get that
- number's square root (i.e. sqrt10)
- factors = type this, followed by a number, to get that
- number's factors (i.e. factors10)
- simplify = type this, followed by a fraction, to get the
- simplified version of that fraction (i.e.
- simplify2/4)
- factorial = type this, followed by a number, to get the
- result of multiplying a series of descending
- numbers, starting with the given number (i.e.
- factorial5)
- cmtoin = type this, followed by a number in centimeters,
- to convert that number to inches (i.e. cmtoin42)
- intocm = type this, followed by a number in inches, to
- convert that number to centimeters (i.e. intocm8)
- mtoft = type this, followed by a number in meters, to
- convert that number to feet (i.e. mtoft5)
- fttom = type this, followed by a number in feet, to
- convert that number to meters (i.e. fttom16)
- # = substitute for the previous result
- slowprint = type this, followed by a space and some
- text, to print out that text after 5 seconds
- (i.e. slowprint hello)
- % = type a number, followed by %, followed by another
- number, to get the number that is the first number's
- percentage of the second number (i.e. 10%20)
- pi = substitute for the first 14 digits of pi (i.e. pi*pi)
- ee = substitute for [first number]*10^[second number]
- (i.e. 2ee4)
- sq = substitute for ^2 (i.e. 2sq)
- cu = substitute for ^3 (i.e. 3cu)
- clear = type this to clear the screen
- cls = alias for clear
- primefacs = type this, followed by a number, to get the
- prime factors of that number (i.e. primefacs10)
- clearmem = clear the "calculator memory.txt" file]=])
- end
- if not string.match(calcin,match) or string.match(calcin,"=") then goto recalc
- elseif string.match(calcin,match) then
- if not string.match(calcin,"sqrt") then
- calcin=string.gsub(calcin,"sq","^2")
- end
- calcin=string.gsub(calcin,"pi",math.pi)
- if prev then
- calcin = string.gsub(calcin,"#",prev)
- end
- --start
- if string.match(calcin,"sqrt") and string.match(calcin,"[0123456789]") and not string.match(calcin," ") then
- calcin=string.lower(calcin)
- input=calcin
- calcin = string.gsub(calcin,"sqrt","")
- if string.match(calcin,notmatch) or string.match(calcin,"[/-*^+()#]") then goto recalc end
- pc,calcin=pcall(math.sqrt,calcin)
- if not pc then goto recalc end
- print("="..calcin)
- glrb=calcin
- calcin=input
- goto skipcalcu
- elseif string.match(calcin,"factors") and string.match(calcin,"[0123456789]") and not string.match(calcin," ") then
- calcin=string.lower(calcin)
- glrb=calcin
- calcin = string.gsub(calcin,"factors","")
- if string.match(calcin,notmatch) or string.match(calcin,"[/-*^+()#]") then goto recalc end
- pc,calcin=pcall(Factors,calcin)
- if not pc then goto recalc end
- print_r(calcin)
- goto noprev
- elseif string.match(calcin,"primefacs") and string.match(calcin,"[0123456789]") and not string.match(calcin," ") then
- calcin=string.lower(calcin)
- glrb=calcin
- calcin = string.gsub(calcin,"primefacs","")
- if string.match(calcin,notmatch) or string.match(calcin,"[/-*^+()#]") then goto recalc end
- calcin=tonumber(calcin)
- pc,calcin=pcall(PrimeDecomp,calcin)
- if not pc then goto recalc end
- print_r(calcin)
- goto noprev
- elseif string.match(calcin,"%%") and string.match(calcin,match) and not string.match(calcin," ") then
- input=calcin
- tab=split(calcin)
- if string.match(tab[1],notmatch) or string.match(tab[1],"[/-*^+()#]") then goto recalc end
- if string.match(tab[2],notmatch) or string.match(tab[2],"[/-*^+()#]") then goto recalc end
- perc=tab[1]
- tot=tab[2]
- pc,glrb=pcall(percent,perc,tot)
- if not pc then goto recalc end
- print("="..glrb)
- calcin=input
- goto skipcalcu
- elseif string.match(calcin,"ee") and string.match(calcin,match) and not string.match(calcin," ") then
- input=calcin
- tab=split(calcin,"ee")
- if string.match(tab[1],notmatch) or string.match(tab[1],"[/-*^+()#]") then goto recalc end
- if string.match(tab[2],notmatch) or string.match(tab[2],"[/-*^+()#]") then goto recalc end
- zero=tab[1]
- to=tab[2]
- pc,glrb=pcall(ee,zero,to)
- if not pc then goto recalc end
- print("="..glrb)
- calcin=input
- goto skipcalcu
- elseif string.match(calcin,"simplify") and string.match(calcin,match) and string.match(calcin,"/") and not string.match(calcin," ") then
- input=calcin
- calcin=string.gsub(calcin,"simplify","")
- if string.match(calcin,notmatch) or string.match(calcin,"[-*^+()#]") then goto recalc end
- pc,glrb=pcall(simplify,calcin)
- if not pc then goto recalc end
- print("="..glrb)
- calcin=input
- goto skipcalcu
- elseif string.match(calcin,"factorial") and string.match(calcin,match) and not string.match(calcin," ") then
- input=calcin
- calcin=string.gsub(calcin,"factorial","")
- calcin=string.lower(calcin)
- if string.match(calcin,notmatch) or string.match(calcin,"[-*^+()#]") then goto recalc end
- pc,glrb=pcall(factorial,calcin)
- if not pc then goto recalc end
- print("="..glrb)
- calcin=input
- goto skipcalcu
- elseif string.match(calcin,"intocm") and string.match(calcin,match) and not string.match(calcin," ") then
- input=calcin
- calcin=string.gsub(calcin,"intocm","")
- if string.match(calcin,notmatch) or string.match(calcin,"[-*^+()#]") then goto recalc end
- glrb=calcin*2.54
- print("="..glrb)
- calcin=input
- goto skipcalcu
- elseif string.match(calcin,"cm>in") and string.match(calcin,match) and not string.match(calcin," ") then
- input=calcin
- calcin=string.gsub(calcin,"cm>in","")
- if string.match(calcin,notmatch) or string.match(calcin,"[-*^+()#]") then goto recalc end
- glrb=calcin/2.54
- print("="..glrb)
- calcin=input
- goto skipcalcu
- elseif string.match(calcin,"ft>m") and string.match(calcin,match) and not string.match(calcin," ") then
- input=calcin
- calcin=string.gsub(calcin,"ft>m","")
- if string.match(calcin,notmatch) or string.match(calcin,"[-*^+()#]") then goto recalc end
- glrb=calcin*3.28
- print("="..glrb)
- calcin=input
- goto skipcalcu
- --end
- elseif string.match(calcin,"sq") and string.match(calcin,match) then calcin=string.gsub(calcin,"sq","^2")
- elseif string.match(calcin,"cu") and string.match(calcin,match) then calcin=string.gsub(calcin,"cu","^3")
- elseif string.match(calcin,"pi") and string.match(calcin,match) then calcin=string.gsub(calcin,"pi",math.pi)
- elseif string.match(calcin,"/0") then goto recalc
- elseif string.match(calcin,"#") and string.match(calcin,match) and prev~=nil then
- calcin = string.gsub(calcin,"#",prev)
- end
- end
- if string.match(calcin,notmatch) or string.match(calcin,"]") then goto recalc end
- if unexpected_condition then goto recalc end
- pc,func = pcall(assert,load("return " .. calcin))
- if not pc then goto recalc end
- glrb = func()
- if glrb==9001 then print("=OVER 9000 (real answer: 9001)") goto skipcalcu end
- if tostring(glrb)==tostring(calcin) then goto recalc end
- if string.match(glrb,"-") then
- print("=("..glrb..")")
- else do
- print("="..glrb)
- end
- end
- ::skipcalcu::
- prev=glrb
- calcin=tostring(calcin)
- date=os.date()
- if createfiles then
- file=io.open("calculator memory.txt","a+")
- if string.match(glrb,"-") then
- file:write("\n"..date.."\n"..calcin.." = ("..glrb..")\n")
- else do
- file:write("\n"..date.."\n"..calcin.." = "..glrb.."\n")
- end
- end
- file:close()
- end
- ::noprev::
- x=x+1 if x==20 and autoclear then os.execute("cls") clearlog() goto recalc else end
- goto recalc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement