Advertisement
BobMe

string algo DO ALL MATH

Sep 26th, 2019
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. -- 3rd attempt in string algo math thing
  2.  
  3. -- the DO ALL MATH algorithm actually works, however it thinks all decimals are numbers which will be fixed later.
  4.  
  5. str = "10000/333333333 is probably a much larger number than 0.9999999999+0.0000000001, but I'm not really too sure. Also 23*3 is a cool number."
  6.  
  7. function isnumber(x)
  8. x = tostring(x)
  9. local numbs = {"1","2","3","4","5","6","7","8","9","0","."}
  10. for i=1,#numbs do
  11. if numbs[i] == x then
  12. return true
  13. end
  14. end
  15. return false
  16. end
  17.  
  18. function isoperator(x)
  19. local numbs = {"*","/","+","-"}
  20. for i=1,#numbs do
  21. if numbs[i] == x then
  22. return {true,numbs[i]}
  23. end
  24. end
  25. return {false,nil}
  26. end
  27.  
  28. function calculate(x,y,z)
  29. if y == "+" then
  30. return x+z
  31. elseif y == "-" then
  32. return x-z
  33. elseif y == "*" then
  34. return x*z
  35. elseif y == "/" then
  36. return x/z
  37. end
  38. end
  39.  
  40.  
  41. local num = false
  42. local firstfound = false
  43. local sub1
  44. local sub2
  45. local sub3
  46. local op
  47. local strr = #str
  48.  
  49. function resetvars()
  50. num = false
  51. firstfound = false
  52. sub1 = nil
  53. sub2 = nil
  54. sub3 = nil
  55. op = nil
  56. end
  57.  
  58. for i=1,strr do
  59. local x = string.sub(str,i,i)
  60. if isnumber(x) == true and num == false then
  61. if x == "." then
  62. if isnumber(string.sub(str,i+1,i+1)) == true then
  63. sub1 = i
  64. firstfound = true
  65. num = true
  66. end
  67. else
  68. sub1 = i
  69. firstfound = true
  70. num = true
  71. end
  72. end
  73. if isnumber(x) == false and num == true and sub1 ~= nil and sub2 == nil and sub3 == nil and isoperator(x)[1] == true then
  74. sub2 = i
  75. op = string.sub(str,sub2,sub2)
  76. elseif isnumber(x) == false and num == true and sub1 ~= nil and sub2 == nil and sub3 == nil and isoperator(x)[1] == false then
  77. resetvars()
  78. end
  79. if isnumber(x) == false and num == true and sub1 ~= nil and sub2 ~= nil and sub3 == nil then
  80. if i-sub2 > 0 then
  81. if x == "." then
  82. if isnumber(string.sub(str,i+1,i+1)) == true then
  83. sub3 = i
  84. end
  85. else
  86. sub3 = i
  87. end
  88. end
  89. end
  90. if (num == true and sub1 ~= nil and sub2 ~= nil and sub3 ~= nil) then
  91. local number1 = tonumber(string.sub(str,sub1,sub2-1))
  92. local number2 = tonumber(string.sub(str,sub2+1,sub3-1))
  93. local sum = calculate(number1,op,number2)
  94. local k = #str
  95. str = string.sub(str,1,sub1-1)..tostring(sum)..string.sub(str,sub3)
  96. local k2 = k-#str
  97. i = i-k2
  98. strr = #str
  99. resetvars()
  100. end
  101. end
  102.  
  103. print(str)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement