Advertisement
XoXFaby

calc

Nov 1st, 2012
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.70 KB | None | 0 0
  1. while true do
  2. term.clear()
  3. term.setCursorPos(1,1)
  4. print("Type any simple equation. Type \"exit\" to exit. ")
  5. res = ""
  6. equ = read()
  7. equs = {}
  8. if equ == "exit" then return end
  9. for i in string.gmatch(equ, "[^%s]+") do
  10. equs[#equs + 1] = i
  11. end
  12. if tonumber(equs[1]) == nil or tonumber(equs[3]) == nil then
  13. res = "Values are not numbers"
  14. else
  15. if equs[2] == "+" then
  16. res = tonumber(equs[1]) + tonumber(equs[3])
  17. elseif equs[2] == "-" then
  18. res = tonumber(equs[1]) - tonumber(equs[3])
  19. elseif equs[2] == "*" then
  20. res = tonumber(equs[1]) * tonumber(equs[3])
  21. elseif equs[2] == "/" then
  22. res = tonumber(equs[1]) / tonumber(equs[3])
  23. else
  24. res = "Invalid Operator"
  25. end
  26. end
  27.  
  28. print(res)
  29. print("Any key to continue")
  30. os.pullEvent()
  31. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement