Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.63 KB | None | 0 0
  1. --------------------------
  2. --Dubs's Text Calculator--
  3. -------Version: 1.9-------
  4. --------------------------
  5. --[[notes
  6. lua function
  7. ]]--
  8. --options
  9. local usecolor = true
  10. local docutscenes = true
  11. local autoclear = false
  12. local autoclearinterval = 20
  13. local cutsceneinterval = 0.25
  14. local using_touchlua_app = true
  15. local createfiles = true
  16. --code
  17. require "draw"
  18. function print2(input)
  19.     io.write(input.."\n")
  20. end
  21. function print(input)
  22.     if input==nil then input="" end
  23.     if input==true then input="true" end
  24.     if input==false then input="false" end
  25.     local file=io.open("calculator logs.txt","a+")
  26.     if file:read()=="" then file:write(input)
  27. else do file:write("\n"..input) end end
  28.     file:close()
  29.     print2(input)
  30. end
  31. function clearlog()
  32.     file=io.open("calculator logs.txt","w")
  33.     file:write()
  34.     file:close()
  35. end
  36. crashcount=0
  37. local match="[1234567890/-*^+().#]"
  38. local notmatch=[=[[qwertyuiopasdfghjklzxcvbnm:;$&@?!'"{}_\|~<>€£¥]]=]
  39. local x=0
  40. if using_touchlua_app and docutscenes then cutsceneinterval=cutsceneinterval*1000 end
  41. if not io.open("calculator logs.txt","r") and createfiles then
  42.    file=io.open("calculator memory.txt","w")
  43.    file:close()
  44. end
  45. if not io.open("calculator memory.txt","r") and createfiles then
  46.    file=io.open("calculator memory.txt","w")
  47.    file:write("-----------------\nCalculator Recall\n-----------------\n")
  48.    file:close()
  49. end
  50. ::cls::
  51. os.execute("cls") clearlog()
  52. prev=nil
  53. function callable(f)
  54.    return function(...)
  55.        error, result = pcall(f, ...)
  56.        if error then
  57.            return true
  58.        else
  59.            return false
  60.        end
  61.    end
  62. end
  63. function get(getFile)
  64.    local file=io.open(getFile,"r")
  65.    local getOut=file:read("*a")
  66.    file:close()
  67.    print("[[[start get]]]")
  68.    print()
  69.    print(getOut)
  70.    print()
  71.    print("[[[end get]]]")
  72. end
  73. function slowprint(text,delay)
  74.    delay=delay or 125
  75.    for x=1,string.len(text) do
  76.        char=string.sub(text,x,x)
  77.        io.write(char)
  78.        sleep(delay)
  79.    end
  80. end
  81. function factorial(n)
  82.    n=tonumber(n)
  83.    return n > 0 and n * factorial(n-1) or 1
  84. end
  85. function percent(perc,tot)
  86.    return (perc/100)*tot
  87. end
  88. function ee(zero,to)
  89.    return zero*(10^to)
  90. end
  91. function simplify(calcin)
  92.    a,b=calcin:match"(.+)/(.+)"u,v=a,b while v+0>0 do t=u u=v v=t%v end
  93.    return a+0==a/u and a.."/"..b or a/u.."/"..b/u
  94. end
  95. function split(inputstr, sep)
  96.    if sep == nil then
  97.        sep = "%%"
  98.    end
  99.    local t={} ; i=1
  100.    for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  101.        t[i] = str
  102.        i = i + 1
  103.    end
  104.    return t
  105. end
  106. function IsPrime(n)
  107.    for i = 2, n^(1/2) do
  108.        if (n % i) == 0 then
  109.            return false
  110.        end
  111.    end
  112.    return true
  113. end
  114. function GetFileSize( filename )
  115.    local fp = io.open( filename )
  116.    if fp == nil then
  117.        return nil
  118.    end
  119.    local filesize = fp:seek( "end" )
  120.    fp:close()
  121.    return filesize
  122. end
  123. function PrimeDecomp( n )
  124.    local f = {}
  125.    if IsPrime( n ) then
  126.        f[1] = n
  127.        return f
  128.    end
  129.    local i = 2
  130.    repeat
  131.        while n % i == 0 do
  132.            f[#f+1] = i
  133.            n = n / i
  134.        end
  135.        repeat
  136.            i = i + 1
  137.        until IsPrime( i )
  138.    until n == 1
  139.    return f
  140. end
  141. function Factors( n )
  142.    local f = {}
  143.    for i = 1, n/2 do
  144.        if n % i == 0 then
  145.            f[#f+1] = i
  146.        end
  147.    end
  148.    f[#f+1] = n
  149.    return f
  150. end
  151. if not using_touchlua_app or not callable(sleep) then
  152.    local clock = os.clock
  153.    function sleep(n)  -- seconds
  154.        local t0 = clock()
  155.        while clock() - t0 <= n do end
  156.    end
  157. end
  158. function print_r ( t )
  159.    local print_r_cache={}
  160.    local function sub_print_r(t,indent)
  161.        if (print_r_cache[tostring(t)]) then
  162.            print(indent.."*"..tostring(t))
  163.        else
  164.            print_r_cache[tostring(t)]=true
  165.            if (type(t)=="table") then
  166.                for pos,val in pairs(t) do
  167.                    if (type(val)=="table") then
  168.                        print(indent.."["..pos.."] => "..tostring(t).." {")
  169.                        sub_print_r(val,indent..string.rep(" ",string.len(pos)+8))
  170.                        print(indent..string.rep(" ",string.len(pos)+6).."}")
  171.                    else
  172.                        print(indent.."["..pos.."] => "..tostring(val))
  173.                    end
  174.                end
  175.            else
  176.                print(indent..tostring(t))
  177.            end
  178.        end
  179.    end
  180.    sub_print_r(t,"  ")
  181. end
  182. if usecolor and using_touchlua_app then
  183.    sys.setbgcolor(draw.black)
  184.    sys.setcolor(draw.red)
  185. end
  186. if usecolor and using_touchlua_app then
  187.    sys.setbgcolor(draw.black)
  188.    sys.setcolor(draw.green)
  189. end
  190. if docutscenes then
  191.    print("Booting") print("- - -")
  192.    sleep(cutsceneinterval)
  193.    os.execute("cls") clearlog()
  194.    print("Booting") print("| - -")
  195.    sleep(cutsceneinterval)
  196.    os.execute("cls") clearlog()
  197.    print("Booting") print("| | -")
  198.    sleep(cutsceneinterval)
  199.    os.execute("cls") clearlog()
  200.    print("Booting") print("| | |")
  201.    sleep(cutsceneinterval)
  202. end
  203. ::reclc::
  204. os.execute("cls")
  205. 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
  206. 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
  207. clearlog()
  208. ::recalc::
  209. print("-------------")
  210. ::redocalc::
  211. calcin = io.read()
  212. if prevcalcin==calcin then crashcount=crashcount+1 end
  213. if prevcalcin~=calcin then crashcount=0 end
  214. if crashcount>=10 then error("keyspam detected.") end
  215. prevcalcin = calcin
  216. if string.match(string.lower(calcin),"slowprint ") then slowp=true goto reclc end
  217. local file=io.open("calculator logs.txt","a+")
  218. file:write("\n"..calcin)
  219. file:close()
  220. calcin=string.lower(calcin)
  221. if string.match(calcin,"panic") and createfiles==true then
  222.    file=io.open("calculator memory.txt","w")
  223.    file:write("-----------------\nCalculator Recall\n-----------------\n")
  224.    file:close()
  225.    clearlog()
  226.    goto cls
  227. 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
  228. elseif string.match(calcin,"#") and not prev then goto recalc
  229. elseif calcin=="" or calcin==" " then x=x+1 if x==20 and autoclear then os.execute("cls") clearlog()  goto recalc else end  goto recalc
  230. elseif string.match(calcin,",") then
  231.    calcin=string.gsub(calcin,",","")
  232. elseif string.match(calcin,"=") then
  233.    calcin=string.gsub(calcin,"=","")
  234. elseif calcin=="time" then
  235.    os.execute("cls")
  236.    local file=io.open("calculator logs.txt","r")
  237.    log=file:read("*a")
  238.    file:close()
  239.    io.write("Enter an equation. * is multiply, / is divide, ^ followed by a number is an exponent. Type help to get advanced functions.")
  240.    local log=string.gsub(log,"\ntime","")
  241.    print(log)
  242.    time=os.date("%I:%M:%S %p on %A, %B %d, %Y")
  243.    print(time)
  244.    goto recalc
  245. elseif calcin=="help" then print("-------------")print([=[time = get the date and time
  246. sqrt = type this, followed by a number, to get that
  247.       number's square root (i.e. sqrt10)
  248. factors = type this, followed by a number, to get that
  249.           number's factors (i.e. factors10)
  250. simplify = type this, followed by a fraction, to get the
  251.           simplified version of that fraction (i.e.  
  252.           simplify2/4)
  253. factorial = type this, followed by a number, to get the
  254.            result of multiplying a series of descending
  255.            numbers, starting with the given number (i.e.
  256.            factorial5)
  257. cmtoin = type this, followed by a number in centimeters,
  258.         to convert that number to inches (i.e. cmtoin42)
  259. intocm = type this, followed by a number in inches, to
  260.         convert that number to centimeters (i.e. intocm8)
  261. mtoft = type this, followed by a number in meters, to
  262.        convert that number to feet (i.e. mtoft5)
  263. fttom = type this, followed by a number in feet, to
  264.        convert that number to meters (i.e. fttom16)
  265. # = substitute for the previous result
  266. slowprint = type this, followed by a space and some
  267.            text, to print out that text after 5 seconds
  268.            (i.e. slowprint hello)
  269. % = type a number, followed by %, followed by another
  270.    number, to get the number that is the first number's
  271.     percentage of the second number (i.e. 10%20)
  272. pi = substitute for the first 14 digits of pi (i.e. pi*pi)
  273. ee = substitute for [first number]*10^[second number]
  274.      (i.e. 2ee4)
  275. sq = substitute for ^2 (i.e. 2sq)
  276. cu = substitute for ^3 (i.e. 3cu)
  277. clear = type this to clear the screen
  278. cls = alias for clear
  279. primefacs = type this, followed by a number, to get the
  280.             prime factors of that number (i.e. primefacs10)
  281. clearmem = clear the "calculator memory.txt" file]=])
  282. end
  283. if not string.match(calcin,match) or string.match(calcin,"=") then goto recalc
  284. elseif string.match(calcin,match) then
  285.     if not string.match(calcin,"sqrt") then
  286.         calcin=string.gsub(calcin,"sq","^2")
  287.     end
  288.     calcin=string.gsub(calcin,"pi",math.pi)
  289.     if prev then
  290.         calcin = string.gsub(calcin,"#",prev)
  291.     end
  292.     --start
  293.     if string.match(calcin,"sqrt") and string.match(calcin,"[0123456789]") and not string.match(calcin," ") then
  294.         calcin=string.lower(calcin)
  295.         input=calcin
  296.         calcin = string.gsub(calcin,"sqrt","")
  297.         if string.match(calcin,notmatch) or string.match(calcin,"[/-*^+()#]") then goto recalc end
  298.         pc,calcin=pcall(math.sqrt,calcin)
  299.         if not pc then goto recalc end
  300.         print("="..calcin)
  301.         glrb=calcin
  302.         calcin=input
  303.         goto skipcalcu
  304.     elseif string.match(calcin,"factors") and string.match(calcin,"[0123456789]") and not string.match(calcin," ") then
  305.         calcin=string.lower(calcin)
  306.         glrb=calcin
  307.         calcin = string.gsub(calcin,"factors","")
  308.         if string.match(calcin,notmatch) or string.match(calcin,"[/-*^+()#]") then goto recalc end
  309.         pc,calcin=pcall(Factors,calcin)
  310.         if not pc then goto recalc end
  311.         print_r(calcin)
  312.         goto noprev
  313.     elseif string.match(calcin,"primefacs") and string.match(calcin,"[0123456789]") and not string.match(calcin," ") then
  314.         calcin=string.lower(calcin)
  315.         glrb=calcin
  316.         calcin = string.gsub(calcin,"primefacs","")
  317.         if string.match(calcin,notmatch) or string.match(calcin,"[/-*^+()#]") then goto recalc end
  318.         calcin=tonumber(calcin)
  319.         pc,calcin=pcall(PrimeDecomp,calcin)
  320.         if not pc then goto recalc
  321.             print_r(calcin)
  322.             goto noprev
  323.         elseif string.match(calcin,"%%") and string.match(calcin,match) and not string.match(calcin," ") then
  324.             input=calcin
  325.             tab=split(calcin)
  326.             if string.match(tab[1],notmatch) or string.match(tab[1],"[/-*^+()#]") then goto recalc end
  327.             if string.match(tab[2],notmatch) or string.match(tab[2],"[/-*^+()#]") then goto recalc end
  328.             perc=tab[1]
  329.             tot=tab[2]
  330.             pc,glrb=pcall(percent,perc,tot)
  331.             if not pc then goto recalc end
  332.             print("="..glrb)
  333.             calcin=input
  334.             goto skipcalcu
  335.         elseif string.match(calcin,"ee") and string.match(calcin,match) and not string.match(calcin," ") then
  336.             input=calcin
  337.             tab=split(calcin,"ee")
  338.             if string.match(tab[1],notmatch) or string.match(tab[1],"[/-*^+()#]") then goto recalc end
  339.             if string.match(tab[2],notmatch) or string.match(tab[2],"[/-*^+()#]") then goto recalc end
  340.             zero=tab[1]
  341.             to=tab[2]
  342.             pc,glrb=pcall(ee,zero,to)
  343.             if not pc then goto recalc end
  344.             print("="..glrb)
  345.             calcin=input
  346.             goto skipcalcu
  347.         elseif string.match(calcin,"simplify") and string.match(calcin,match) and string.match(calcin,"/") and not string.match(calcin," ") then
  348.             input=calcin
  349.             calcin=string.gsub(calcin,"simplify","")
  350.             if string.match(calcin,notmatch) or string.match(calcin,"[-*^+()#]") then goto recalc end
  351.             pc,glrb=pcall(simplify,calcin)
  352.             if not pc then goto recalc end
  353.             print("="..glrb)
  354.             calcin=input
  355.             goto skipcalcu
  356.         elseif string.match(calcin,"factorial") and string.match(calcin,match) and not string.match(calcin," ") then
  357.             input=calcin
  358.             calcin=string.gsub(calcin,"factorial","")
  359.             calcin=string.lower(calcin)
  360.             if string.match(calcin,notmatch) or string.match(calcin,"[-*^+()#]") then goto recalc end
  361.             pc,glrb=pcall(factorial,calcin)
  362.             if not pc then goto recalc end
  363.             print("="..glrb)
  364.             calcin=input
  365.             goto skipcalcu
  366.         elseif string.match(calcin,"intocm") and string.match(calcin,match) and not string.match(calcin," ") then
  367.             input=calcin
  368.             calcin=string.gsub(calcin,"intocm","")
  369.             if string.match(calcin,notmatch) or string.match(calcin,"[-*^+()#]") then goto recalc end
  370.             glrb=calcin*2.54
  371.             print("="..glrb)
  372.             calcin=input
  373.             goto skipcalcu
  374.         elseif string.match(calcin,"cm>in") and string.match(calcin,match) and not string.match(calcin," ") then
  375.             input=calcin
  376.             calcin=string.gsub(calcin,"cm>in","")
  377.             if string.match(calcin,notmatch) or string.match(calcin,"[-*^+()#]") then goto recalc end
  378.             glrb=calcin/2.54
  379.             print("="..glrb)
  380.             calcin=input
  381.             goto skipcalcu
  382.         elseif string.match(calcin,"ft>m") and string.match(calcin,match) and not string.match(calcin," ") then
  383.             input=calcin
  384.             calcin=string.gsub(calcin,"ft>m","")
  385.             if string.match(calcin,notmatch) or string.match(calcin,"[-*^+()#]") then goto recalc end
  386.             glrb=calcin*3.28
  387.             print("="..glrb)
  388.             calcin=input
  389.             goto skipcalcu
  390.             --end
  391.         elseif string.match(calcin,"sq") and string.match(calcin,match) then calcin=string.gsub(calcin,"sq","^2")
  392.         elseif string.match(calcin,"cu") and string.match(calcin,match) then calcin=string.gsub(calcin,"cu","^3")
  393.         elseif string.match(calcin,"pi") and string.match(calcin,match) then calcin=string.gsub(calcin,"pi",math.pi)
  394.         elseif string.match(calcin,"/0") then goto recalc
  395.         elseif string.match(calcin,"#") and string.match(calcin,match) and prev~=nil then
  396.             calcin = string.gsub(calcin,"#",prev)
  397.         end
  398.     end
  399. end
  400. if string.match(calcin,notmatch) or string.match(calcin,"]") then goto recalc end
  401. if unexpected_condition then goto recalc end
  402. pc,func = pcall(assert,load("return " .. calcin))
  403. if not pc then goto recalc end
  404. glrb = func()
  405. if tostring(glrb)==tostring(calcin) then goto recalc end
  406. if string.match(glrb,"-") then
  407.     print("=("..glrb..")")
  408. else do
  409.         print("="..glrb)
  410.     end
  411. end
  412. ::skipcalcu::
  413. prev=glrb
  414. calcin=tostring(calcin)
  415. date=os.date()
  416. if createfiles then
  417.     file=io.open("calculator memory.txt","a+")
  418.     if string.match(glrb,"-") then
  419.         file:write("\n"..date.."\n"..calcin.." = ("..glrb..")\n")
  420.     else do
  421.             file:write("\n"..date.."\n"..calcin.." = "..glrb.."\n")
  422.         end
  423.     end
  424.     file:close()
  425. end
  426. ::noprev::
  427. x=x+1 if x==20 and autoclear then os.execute("cls") clearlog()  goto recalc else end
  428. goto recalc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement