AndreSoYeah

Untitled

Feb 16th, 2015
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.09 KB | None | 0 0
  1. cb=peripheral.wrap("top")
  2. factorial=nil
  3. blacklist = {
  4. "tha_cre8er"
  5. }
  6. temp={}
  7. fibs={1,1}
  8. for i=1,186 do
  9. fibs[#fibs+1] = fibs[#fibs]+fibs[#fibs-1]
  10. end
  11. function checkNumb(tab)
  12. ok=true
  13. toR={}
  14. for k,v in pairs(tab) do
  15. s=tonumber(v)
  16. if not s then
  17. ok=false
  18. else
  19. toR[#toR+1] = tonumber(v)
  20. end
  21. end
  22. if ok then
  23. return toR
  24. else
  25. return false
  26. end
  27. end
  28. checknumb=checkNumb
  29. function factorial(n)
  30. n=tonumber(n)
  31. r=math.abs(n)
  32. d=1
  33. if r>34 then
  34. return "Infinity"
  35. else
  36. for y=1,r do
  37. d=d*y
  38. end
  39. if r==n then
  40. return d
  41. else
  42. return tonumber("-"..d)
  43. end
  44. end
  45. end
  46. function corParam(g,l)
  47. if type(g)==type({"a"}) then
  48. if#g==l then
  49. if checkNumb(g) then
  50. return true
  51. else
  52. return false
  53. end
  54. else
  55. return false
  56. end
  57. end
  58. end
  59. operations = {
  60. ["+"] = function(a)
  61. --print("+")
  62. if corParam(a,2) then
  63. ans = a[1]+a[2]
  64. return a[1].."+"..a[2].."="..ans
  65. else
  66. return "Incorrect parameters"
  67. end
  68. end,
  69. ["-"] = function(a)
  70. if corParam(a,2) then
  71. ans = a[1]-a[2]
  72. return a[1].."-"..a[2].."="..ans
  73. else
  74. return "Incorrect parameters"
  75. end
  76. end,
  77. ["/"] = function(a)
  78. if corParam(a,2) then
  79. ans = a[1]/a[2]
  80. return a[1].."/"..a[2].."="..ans
  81. else
  82. return "Incorrect parameters"
  83. end
  84. end,
  85. ["*"] = function(a)
  86. if corParam(a,2) then
  87. ans = a[1]*a[2]
  88. return a[1].."*"..a[2].."="..ans
  89. else
  90. return "Incorrect parameters"
  91. end
  92. end,
  93. ["sqrt"] = function(a)
  94. if corParam(a,1) then
  95. ans = math.sqrt(a[1])
  96. return "root("..a[1]..") = "..ans
  97. else
  98. return "Incorrect parameters"
  99. end
  100. end,
  101. ["root"] = function(a)
  102. if true then
  103. return operations["sqrt"](a)
  104. endfor i=1,10 do
  105. print(i)
  106. end
  107. end,
  108. ["!"] = function(a)
  109. if corParam(a,1) then
  110. ans=factorial(a[1])
  111. return a[1].."!".."="..ans
  112. else
  113. return "Incorrect parameters"
  114. end
  115. end,
  116. ["avg"] = function(a)
  117. if checkNumb(a) then
  118. if #a>0 then
  119. sum=0
  120. ou="avg("
  121. for ir=1,#a do
  122. sum=sum+a[ir]
  123. ou=ou..a[ir]..","
  124. end
  125. ou=string.sub(ou,1,#ou-1)..")"
  126. ans=sum/#a
  127. return ou.." = "..ans
  128. else
  129. return "Incorrect parameters"
  130. end
  131. else
  132. return "Incorrect parameters"
  133. end
  134. end,
  135. ["pi"] = function(a)
  136. pi="3.141592653789793238462643383279502884197169"
  137. if #a==0 then
  138. return "PI = "..pi
  139. elseif #a==1 and checkNumb(a) then
  140. return a[1].."PI = "..(tonumber(pi)*a[1])
  141. else
  142. return "Incorrect parameters"
  143. end
  144. end,
  145. ["sin"] = function(a)
  146. if corParam(a,1)then
  147. ans=math.sin(a[1])
  148. return "sin("..a[1]..") = "..ans
  149. else
  150. return "Incorrect parameters"
  151. end
  152. end,
  153. ["sine"] = function(a)
  154. if true then
  155. return operations["sin"](a)
  156. end
  157. end,
  158. ["triangle"] = function(a)
  159. if corParam(a,3) then
  160. temp.a=tonumber(a[1])
  161. temp.b=tonumber(a[2])
  162. temp.c=tonumber(a[3])
  163. if ((temp.a+temp.b>temp.c) and (temp.a+temp.c>temp.b) and (temp.b+temp.c>temp.a)) then
  164. temp.c1 = "True"
  165. else
  166. temp.c1 = "False"
  167. end
  168. return "tri("..temp.a..", "..temp.b..", "..temp.c..") = "..temp.c1
  169. else
  170. return "Incorrect parameters"
  171. end
  172. end,
  173. ["cos"] = function(a)
  174. if corParam(a,1) then
  175. ans=math.cos(a[1])
  176. return "cos("..a[1]..") = "..ans
  177. else
  178. return "Incorrect parameters"
  179. end
  180. end,
  181. ["fibonacci"] = function(a)
  182. if corParam(a,1) then
  183. a[1] = tonumber(a[1])
  184. if a[1]>0 and a[1]<189 then
  185. ans=fibs[tonumber(a[1])]
  186. return "fib("..a[1]..") = "..ans
  187. elseif a[1]>188 then
  188. return "Infinity"
  189. else
  190. return "Must input positive integer"
  191. end
  192. else
  193. return "Incorrect parameters"
  194. end
  195. end,
  196. ["rand"] = function(a)
  197. if corParam(a,2) then
  198. if a[1]<a[2] then
  199. math.randomseed(os.time()*1000)
  200. ans=math.random(a[1],a[2])
  201. return "rand("..a[1]..","..a[2]..") = "..ans
  202. else
  203. return "Interval is empty"
  204. end
  205. else
  206. return "Incorrect parameters"
  207. end
  208. end,
  209. ["random"] = function(a)
  210. if true then
  211. return operations["rand"](a)
  212. end
  213. end,
  214. ["degrees"] = function(a)
  215. if corParam(a,1) then
  216. ans=math.deg(a[1])
  217. return "rad("..a[1]..") = deg("..ans..")"
  218. else
  219. return "Incorrect parameters"
  220. end
  221. end,
  222. ["radians"] = function(a)
  223. if corParam(a,1) then
  224. ans=math.rad(a[1])
  225. return "deg("..a[1]..") = rad("..ans..")"
  226. else
  227. return "Incorrect parameters"
  228. end
  229. end,
  230. ["log"] = function(a)
  231. if corParam(a,2) then
  232. ans=math.log(a[2])/math.log(a[1])
  233. return "log("..a[2]..") in base "..a[1].." = "..ans
  234. else
  235. return "Incorrect parameters"
  236. end
  237. end,
  238. ["ln"] = function(a)
  239. if corParam(a,1) then
  240. ans=math.log(a[1])
  241. return "log("..a[1]..") in base e = "..ans
  242. end
  243. end,
  244. ["syntax"] = function(a)
  245. if true then
  246. return " Syntax: math:func:arg1:arg2..."
  247. end
  248. end,
  249. ["list"] = function(a)
  250. --print("list")
  251. --print(#a)
  252. if #a == 0 then
  253. ls=""
  254. --print("ls")
  255. for k,v in pairs(operations) do
  256. --print(#operations)
  257. if ls=="" then
  258. ls = k
  259. else
  260. ls=ls..", "..k
  261. end
  262. --print(ls)
  263. end
  264. return "Commands: "..ls
  265. else
  266. return "Incorrect parameters"
  267. end
  268. end
  269. }
  270. function parse(txt)
  271. colons = {}
  272. args={}
  273. for i=1,#txt do
  274. if string.sub(txt,i,i) == ":" then
  275. colons[#colons+1] = i
  276. end
  277. end
  278. for i=1,#colons-1 do
  279. args[#args+1] = string.sub(txt,colons[i]+1,colons[i+1]-1)
  280. end
  281. return args
  282. end
  283. while true do
  284. gf=true
  285. while gf do
  286. gf=false
  287. _,_,user,msg = os.pullEvent("chat_message")
  288. for k,v in pairs(blacklist) do
  289. if v==user then
  290. gf=true
  291. if #msg>4 then
  292. if string.sub(string.lower(msg),1,5) == ":math" then
  293. cb.tell(v,"You have been blacklisted")
  294. end
  295. end
  296. end
  297. end
  298. end
  299. msg=string.lower(msg)
  300. if string.sub(msg,1,1) == ":" then
  301. if string.sub(msg,#msg,#msg) == ":" then
  302. --nothing
  303. else
  304. msg=msg..":"
  305. end
  306. returned=parse(msg)
  307. if returned then
  308. if returned[1] == "math" then
  309. toGive={}
  310. for h=3,#returned do
  311. toGive[#toGive+1]=returned[h]
  312. end
  313. if operations[returned[2]] then
  314. if true then
  315. output=operations[returned[2]](toGive)
  316. cb.tell(user,output)
  317. end
  318. else
  319. if msg == ":math:" then
  320. cb.tell(user,operations["list"]({}))
  321. else
  322. cb.tell(user,"Incorrect function")
  323. end
  324. end
  325. end
  326. end
  327. end
  328. end
Advertisement
Add Comment
Please, Sign In to add comment