Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --------------------------
- --Dubs's Text Calculator--
- -------Version: 1.2-------
- --------------------------
- --[[notes
- ]]--
- function print2(input)
- io.write(input.."\n")
- end
- --
- function print(input)
- 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
- --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
- 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
- if createfiles then
- file=io.open("clear memory.lua","w")
- file:write([==[
- function callable(f)
- return function(...)
- error, result = pcall(f, ...)
- if error then
- return true
- else
- return false
- end
- end
- end
- if not callable(sleep) then
- local clock = os.clock
- function sleep(n) -- seconds
- local t0 = clock()
- while clock() - t0 <= n do end
- end
- end
- 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 r=true 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 r=true 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 r=true 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()
- print([=[Calculator's memory file "calculator memory.txt" has been cleared. Have a good day!]=])
- ::reclc::
- if not r then goto skip end
- os.execute("cls") clearlog()
- print([=[Calculator's memory file "calculator memory.txt" has not been cleared, as the answer "yes" was not given. Have a good day!]=])
- ::skip::
- ]==])
- 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 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 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(colors.black)
- sys.setcolor(colors.red)
- end
- if usecolor and using_touchlua_app then
- sys.setbgcolor(colors.black)
- sys.setcolor(colors.lime)
- 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. Don't use =. * is multiply, / is divide, ^ is power. 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. Don't use =. * is multiply, / is divide, ^ is power. Type help to get advanced functions.") print(log) print("-------------") sleep(5000) slowprint(calcin,200) io.write("\n") slowp=false end
- clearlog()
- ::recalc::
- print("-------------")
- ::redocalc::
- calcin = io.read()
- local file=io.open("calculator logs.txt","a+")
- file:write("\n"..calcin)
- file:close()
- if string.match(string.lower(calcin),"slowprint ") then slowp=true goto reclc end
- calcin=string.lower(calcin)
- if 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()
- print([=[Calculator's memory file "calculator memory.txt" has been cleared.]=])
- print("Press any button to continue.")
- io.read()
- goto reclc
- 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=="help" then print("-------------")print([=[help = list advanced commands
- pi = substitute for the first 14 digits of pi
- 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)
- # = substitute for the previous result
- slowprint = type slowprint, 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)
- ee = substitute for [first number]*10^[second number]
- (i.e. 2ee4)
- sq = substitute for ^2
- cu = substitute for ^3
- 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
- if string.match(calcin,"sqrt") and string.match(calcin,"[0123456789]") and not string.match(calcin," ") then
- calcin=string.lower(calcin)
- calcin = string.gsub(calcin,"sqrt","")
- calcin=math.sqrt(calcin) print("="..calcin)
- glrb=calcin
- goto skipcalcu
- elseif string.match(calcin,"factors") and string.match(calcin,"[0123456789]") and not string.match(calcin," ") then
- calcin=string.lower(calcin)
- calcin = string.gsub(calcin,"factors","")
- calcin=Factors(calcin)
- print_r(calcin)
- glrb=calcin
- goto noprev
- elseif string.match(calcin,"primefacs") and string.match(calcin,"[0123456789]") and not string.match(calcin," ") then
- calcin=string.lower(calcin)
- calcin = string.gsub(calcin,"primefacs","")
- calcin=tonumber(calcin)
- calcin=PrimeDecomp(calcin)
- print_r(calcin)
- glrb=calcin
- goto noprev
- elseif string.match(calcin,"%%") and string.match(calcin,match) and not string.match(calcin," ") then
- tab=split(calcin)
- perc=tab[1]
- tot=tab[2]
- glrb=(perc/100)*tot
- print("="..glrb)
- goto skipcalcu
- elseif string.match(calcin,"ee") and string.match(calcin,match) and not string.match(calcin," ") then
- tab=split(calcin,"ee")
- zero=tab[1]
- to=tab[2]
- glrb=zero*(10^to)
- print("="..glrb)
- goto skipcalcu
- 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","^2")
- elseif string.match(calcin,"pi") and string.match(calcin,match) then calcin=string.gsub(calcin,"pi",math.pi)
- 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
- func = assert(load("return " .. calcin))
- glrb = func()
- print("="..glrb)
- ::skipcalcu::
- prev=glrb
- calcin=tostring(calcin)
- date=os.date()
- if createfiles then
- file=io.open("calculator memory.txt","a+")
- file:write("\n"..date.."\n"..calcin.."="..glrb.."\n")
- 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