Advertisement
Guest User

diamonddigger13's text calculator

a guest
Apr 15th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.34 KB | None | 0 0
  1. --------------------------
  2. --Dubs's Text Calculator--
  3. -------Version: 1.2-------
  4. --------------------------
  5.  
  6. --[[notes
  7.  
  8. ]]--
  9. function print2(input)
  10. io.write(input.."\n")
  11. end
  12. --
  13. function print(input)
  14. local file=io.open("calculator logs.txt","a+")
  15. if file:read()=="" then file:write(input)
  16. else do file:write("\n"..input) end end
  17. file:close()
  18. print2(input)
  19. end
  20.  
  21. function clearlog()
  22. file=io.open("calculator logs.txt","w")
  23. file:write()
  24. file:close()
  25. end
  26.  
  27. --options
  28. local usecolor = true
  29. local docutscenes = true
  30. local autoclear = false
  31. local autoclearinterval = 20
  32. local cutsceneinterval = 0.25
  33. local using_touchlua_app = true
  34. local createfiles = true
  35.  
  36. --code
  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. if createfiles then
  51. file=io.open("clear memory.lua","w")
  52. file:write([==[
  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. if not callable(sleep) then
  64. local clock = os.clock
  65. function sleep(n) -- seconds
  66. local t0 = clock()
  67. while clock() - t0 <= n do end
  68. end
  69. end
  70. os.execute("cls") clearlog()
  71. print("Would you like to clear the calculator's memory file? \n(yes/no)")
  72. if string.lower(io.read())~="yes" then r=true goto reclc end
  73. os.execute("cls") clearlog()
  74. print("Are you absolutely sure that you want to clear the calculator's memory file? \n(yes/no)")
  75. if string.lower(io.read())~="yes" then r=true goto reclc end
  76. os.execute("cls") clearlog()
  77. print("Last chance to stop. Are you sure that you would like to clear the calculator's memory file? \n(yes/no)")
  78. if string.lower(io.read())~="yes" then r=true goto reclc end
  79. os.execute("cls") clearlog()
  80. print("Alright. Clearing calculator's memory file...") print("- - -") sleep(]==].. cutsceneinterval .. [==[) os.execute("cls") clearlog()
  81. print("Alright. Clearing calculator's memory file...") print("| - -") sleep(]==].. cutsceneinterval .. [==[) os.execute("cls") clearlog()
  82. print("Alright. Clearing calculator's memory file...") print("| | -") sleep(]==].. cutsceneinterval .. [==[) os.execute("cls") clearlog()
  83. print("Alright. Clearing calculator's memory file...") print("| | |") sleep(]==].. cutsceneinterval .. [==[) os.execute("cls") clearlog()
  84. print([=[Calculator's memory file "calculator memory.txt" has been cleared. Have a good day!]=])
  85. ::reclc::
  86. if not r then goto skip end
  87. os.execute("cls") clearlog()
  88. print([=[Calculator's memory file "calculator memory.txt" has not been cleared, as the answer "yes" was not given. Have a good day!]=])
  89. ::skip::
  90. ]==])
  91. file:close()
  92. end
  93.  
  94. ::cls::
  95. os.execute("cls") clearlog()
  96. prev=nil
  97. function callable(f)
  98. return function(...)
  99. error, result = pcall(f, ...)
  100. if error then
  101. return true
  102. else
  103. return false
  104. end
  105. end
  106. end
  107.  
  108.  
  109. function slowprint(text,delay)
  110.  
  111. delay=delay or 125
  112.  
  113. for x=1,string.len(text) do
  114. char=string.sub(text,x,x)
  115. io.write(char)
  116. sleep(delay)
  117. end
  118.  
  119. end
  120.  
  121. function split(inputstr, sep)
  122. if sep == nil then
  123. sep = "%%"
  124. end
  125. local t={} ; i=1
  126. for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  127. t[i] = str
  128. i = i + 1
  129. end
  130. return t
  131. end
  132.  
  133. function IsPrime(n)
  134. for i = 2, n^(1/2) do
  135. if (n % i) == 0 then
  136. return false
  137. end
  138. end
  139. return true
  140. end
  141.  
  142. function GetFileSize( filename )
  143. local fp = io.open( filename )
  144. if fp == nil then
  145. return nil
  146. end
  147. local filesize = fp:seek( "end" )
  148. fp:close()
  149. return filesize
  150. end
  151.  
  152. function PrimeDecomp( n )
  153. local f = {}
  154. if IsPrime( n ) then
  155. f[1] = n
  156. return f
  157. end
  158. local i = 2
  159. repeat
  160. while n % i == 0 do
  161. f[#f+1] = i
  162. n = n / i
  163. end
  164. repeat
  165. i = i + 1
  166. until IsPrime( i )
  167. until n == 1
  168.  
  169. return f
  170. end
  171.  
  172. function Factors( n )
  173. local f = {}
  174. for i = 1, n/2 do
  175. if n % i == 0 then
  176. f[#f+1] = i
  177. end
  178. end
  179. f[#f+1] = n
  180.  
  181. return f
  182. end
  183.  
  184. if not using_touchlua_app or not callable(sleep) then
  185. local clock = os.clock
  186. function sleep(n) -- seconds
  187. local t0 = clock()
  188. while clock() - t0 <= n do end
  189. end
  190. end
  191.  
  192. function print_r ( t )
  193. local print_r_cache={}
  194. local function sub_print_r(t,indent)
  195. if (print_r_cache[tostring(t)]) then
  196. print(indent.."*"..tostring(t))
  197. else
  198. print_r_cache[tostring(t)]=true
  199. if (type(t)=="table") then
  200. for pos,val in pairs(t) do
  201. if (type(val)=="table") then
  202. print(indent.."["..pos.."] => "..tostring(t).." {")
  203. sub_print_r(val,indent..string.rep(" ",string.len(pos)+8))
  204. print(indent..string.rep(" ",string.len(pos)+6).."}")
  205. else
  206. print(indent.."["..pos.."] => "..tostring(val))
  207. end
  208. end
  209. else
  210. print(indent..tostring(t))
  211. end
  212. end
  213. end
  214. sub_print_r(t," ")
  215. end
  216.  
  217. if usecolor and using_touchlua_app then
  218. sys.setbgcolor(colors.black)
  219. sys.setcolor(colors.red)
  220. end
  221.  
  222. if usecolor and using_touchlua_app then
  223. sys.setbgcolor(colors.black)
  224. sys.setcolor(colors.lime)
  225. end
  226.  
  227. if docutscenes then
  228. print("Booting") print("- - -")
  229. sleep(cutsceneinterval)
  230. os.execute("cls") clearlog()
  231. print("Booting") print("| - -")
  232. sleep(cutsceneinterval)
  233. os.execute("cls") clearlog()
  234. print("Booting") print("| | -")
  235. sleep(cutsceneinterval)
  236. os.execute("cls") clearlog()
  237. print("Booting") print("| | |")
  238. sleep(cutsceneinterval)
  239. end
  240. ::reclc::
  241. os.execute("cls")
  242. if not slowp then print("Enter an equation. Don't use =. * is multiply, / is divide, ^ is power. Type help to get advanced functions.") end
  243. 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
  244. clearlog()
  245. ::recalc::
  246. print("-------------")
  247. ::redocalc::
  248. calcin = io.read()
  249. local file=io.open("calculator logs.txt","a+")
  250. file:write("\n"..calcin)
  251. file:close()
  252. if string.match(string.lower(calcin),"slowprint ") then slowp=true goto reclc end
  253. calcin=string.lower(calcin)
  254. 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
  255. elseif string.match(calcin,"#") and not prev then goto recalc
  256. elseif calcin=="clearmem" and createfiles==true then
  257. os.execute("cls") clearlog()
  258. print("Would you like to clear the calculator's memory file? \n(yes/no)")
  259. if string.lower(io.read())~="yes" then goto reclc end
  260. os.execute("cls") clearlog()
  261. print("Are you absolutely sure that you want to clear the calculator's memory file? \n(yes/no)")
  262. if string.lower(io.read())~="yes" then goto reclc end
  263. os.execute("cls") clearlog()
  264. print("Last chance to stop. Are you sure that you would like to clear the calculator's memory file? \n(yes/no)")
  265. if string.lower(io.read())~="yes" then goto reclc end
  266. os.execute("cls") clearlog()
  267. print("Alright. Clearing calculator's memory file...") print("- - -") sleep(cutsceneinterval) os.execute("cls") clearlog()
  268. print("Alright. Clearing calculator's memory file...") print("| - -") sleep(cutsceneinterval) os.execute("cls") clearlog()
  269. print("Alright. Clearing calculator's memory file...") print("| | -") sleep(cutsceneinterval) os.execute("cls") clearlog()
  270. print("Alright. Clearing calculator's memory file...") print("| | |") sleep(cutsceneinterval) os.execute("cls") clearlog()
  271. print([=[Calculator's memory file "calculator memory.txt" has been cleared.]=])
  272. print("Press any button to continue.")
  273. io.read()
  274. goto reclc
  275.  
  276. elseif calcin=="" or calcin==" " then x=x+1 if x==20 and autoclear then os.execute("cls") clearlog() goto recalc else end goto recalc
  277. elseif string.match(calcin,",") then
  278. calcin=string.gsub(calcin,",","")
  279. elseif string.match(calcin,"=") then
  280. calcin=string.gsub(calcin,"=","")
  281. elseif calcin=="help" then print("-------------")print([=[help = list advanced commands
  282. pi = substitute for the first 14 digits of pi
  283. sqrt = type this, followed by a number, to get that
  284. number's square root (i.e. sqrt10)
  285. factors = type this, followed by a number, to get that
  286. number's factors (i.e. factors10)
  287. # = substitute for the previous result
  288. slowprint = type slowprint, followed by a space and some
  289. text, to print out that text after 5 seconds
  290. (i.e. slowprint hello)
  291. % = type a number, followed by %, followed by another
  292. number, to get the number that is the first number's
  293. percentage of the second number (i.e. 10%20)
  294. ee = substitute for [first number]*10^[second number]
  295. (i.e. 2ee4)
  296. sq = substitute for ^2
  297. cu = substitute for ^3
  298. clear = type this to clear the screen
  299. cls = alias for clear
  300. primefacs = type this, followed by a number, to get the
  301. prime factors of that number (i.e. primefacs10)
  302. clearmem = clear the "calculator memory.txt" file]=])
  303. end
  304. if not string.match(calcin,match) or string.match(calcin,"=") then goto recalc
  305. elseif string.match(calcin,match) then
  306.  
  307. if not string.match(calcin,"sqrt") then
  308. calcin=string.gsub(calcin,"sq","^2")
  309. end
  310.  
  311. calcin=string.gsub(calcin,"pi",math.pi)
  312.  
  313. if prev then
  314. calcin = string.gsub(calcin,"#",prev)
  315. end
  316.  
  317. if string.match(calcin,"sqrt") and string.match(calcin,"[0123456789]") and not string.match(calcin," ") then
  318. calcin=string.lower(calcin)
  319. calcin = string.gsub(calcin,"sqrt","")
  320. calcin=math.sqrt(calcin) print("="..calcin)
  321. glrb=calcin
  322. goto skipcalcu
  323.  
  324. elseif string.match(calcin,"factors") and string.match(calcin,"[0123456789]") and not string.match(calcin," ") then
  325. calcin=string.lower(calcin)
  326. calcin = string.gsub(calcin,"factors","")
  327. calcin=Factors(calcin)
  328. print_r(calcin)
  329. glrb=calcin
  330. goto noprev
  331.  
  332. elseif string.match(calcin,"primefacs") and string.match(calcin,"[0123456789]") and not string.match(calcin," ") then
  333. calcin=string.lower(calcin)
  334. calcin = string.gsub(calcin,"primefacs","")
  335. calcin=tonumber(calcin)
  336. calcin=PrimeDecomp(calcin)
  337. print_r(calcin)
  338. glrb=calcin
  339. goto noprev
  340.  
  341. elseif string.match(calcin,"%%") and string.match(calcin,match) and not string.match(calcin," ") then
  342. tab=split(calcin)
  343. perc=tab[1]
  344. tot=tab[2]
  345. glrb=(perc/100)*tot
  346. print("="..glrb)
  347. goto skipcalcu
  348.  
  349. elseif string.match(calcin,"ee") and string.match(calcin,match) and not string.match(calcin," ") then
  350. tab=split(calcin,"ee")
  351. zero=tab[1]
  352. to=tab[2]
  353. glrb=zero*(10^to)
  354. print("="..glrb)
  355. goto skipcalcu
  356.  
  357. elseif string.match(calcin,"sq") and string.match(calcin,match) then calcin=string.gsub(calcin,"sq","^2")
  358.  
  359. elseif string.match(calcin,"cu") and string.match(calcin,match) then calcin=string.gsub(calcin,"cu","^2")
  360.  
  361. elseif string.match(calcin,"pi") and string.match(calcin,match) then calcin=string.gsub(calcin,"pi",math.pi)
  362.  
  363. elseif string.match(calcin,"#") and string.match(calcin,match) and prev~=nil then
  364. calcin = string.gsub(calcin,"#",prev)
  365. end
  366. end
  367. if string.match(calcin,notmatch) or string.match(calcin,"]") then goto recalc end
  368. func = assert(load("return " .. calcin))
  369. glrb = func()
  370. print("="..glrb)
  371. ::skipcalcu::
  372. prev=glrb
  373. calcin=tostring(calcin)
  374. date=os.date()
  375. if createfiles then
  376. file=io.open("calculator memory.txt","a+")
  377. file:write("\n"..date.."\n"..calcin.."="..glrb.."\n")
  378. file:close()
  379. end
  380. ::noprev::
  381. x=x+1 if x==20 and autoclear then os.execute("cls") clearlog() goto recalc else end
  382. goto recalc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement