Advertisement
BobMe

my pc battery is low help

Sep 25th, 2019
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. -- time to remake the strange do all math algo.
  2.  
  3.  
  4. str = "8*3 is my favorite number, but it's not as cool as 3*42. 827 Is a good one too."
  5.  
  6. function isnumber(x)
  7. local numbers = {"1","2","3","4","5","6","7","8","9","0","."}
  8. for i=1,#numbers do
  9. if tonumber(x) == tonumber(numbers[i]) then
  10. return true
  11. end
  12. end
  13. return false
  14. end
  15.  
  16. function isoperator(x)
  17. local operators = {"*","-","+","/"}
  18. for i=1,#operators do
  19. if x == operator[i] then
  20. return {true,x}
  21. end
  22. end
  23. return {false,nil}
  24. end
  25.  
  26. function calculate(x,sign,y)
  27. if sign == "+" then
  28. local sum = tonumber(x)+tonumber(y)
  29. return sum
  30. elseif sign == "-" then
  31. local sum = tonumber(x)-tonumber(y)
  32. return sum
  33. elseif sign == "/" then
  34. local sum = tonumber(x)/tonumber(y)
  35. return sum
  36. elseif sign == "*" then
  37. local sum = tonumber(x)*tonumber(y)
  38. return sum
  39. end
  40. end
  41.  
  42. local sub1
  43. local sub2
  44. local sub3
  45. local op
  46.  
  47. for i=1,#str do
  48. if isnumber(string.sub(str,i,i)) == true and sub1 == nil and op == nil then
  49. print('hi')
  50. sub1 = i
  51. elseif isnumber(string.sub(str,i,i)) == false and sub1 == true and isoperator(string.sub(str,i,i))[1] == true and op == nil then
  52. print('hi again')
  53. op = isoperator(string.sub(str,i,i))[2]
  54. sub3 = i
  55. elseif op ~= nil and isnumber(string.sub(str,i,i)) == false then
  56. sub2 = i
  57. local number1 = string.sub(sub1,sub3-1)
  58. local number2 = string.sub(sub3+1,sub2)
  59. local math = calculate(number1,op,number2)
  60. local idiff = #str
  61. str = string.sub(str,1,sub1-1)..math..(string.sub(str,sub3+1))
  62. idiff = idiff - #str
  63. i = i - idiff
  64. else
  65. --sub1 = nil
  66. --sub2 = nil
  67. --op = nil
  68. end
  69. end
  70.  
  71. print(str)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement