How to resolve the algorithm Arithmetic evaluation step by step in the Lua programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Arithmetic evaluation step by step in the Lua programming language

Table of Contents

Problem Statement

For those who don't remember, mathematical precedence is as follows:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Arithmetic evaluation step by step in the Lua programming language

Source code in the lua programming language

require"lpeg"

P, R, C, S, V = lpeg.P, lpeg.R, lpeg.C, lpeg.S, lpeg.V

--matches arithmetic expressions and returns a syntax tree
expression = P{"expr";
ws = P" "^0,
number = C(R"09"^1) * V"ws",
lp = "(" * V"ws",
rp = ")" * V"ws",
sym = C(S"+-*/") * V"ws",
more = (V"sym" * V"expr")^0,
expr = V"number" * V"more" + V"lp" * lpeg.Ct(V"expr" * V"more") * V"rp" * V"more"}

--evaluates a tree
function eval(expr)
  --empty
  if type(expr) == "string" or type(expr) == "number" then return expr + 0 end
  
  --arithmetic functions
  tb = {["+"] = function(a,b) return eval(a) + eval(b) end,
		["-"] = function(a,b) return eval(a) - eval(b) end,
		["*"] = function(a,b) return eval(a) * eval(b) end,
		["/"] = function(a,b) return eval(a) / eval(b) end}
  
  --you could add ^ or other operators to this pretty easily
  for i, v in ipairs{"*/", "+-"} do
    for s, u in ipairs(expr) do
	  local k = type(u) == "string" and C(S(v)):match(u)
	  if k then
	    expr[s-1] = tb[k](expr[s-1],expr[s+1])
	    table.remove(expr, s)
	    table.remove(expr, s)
	  end
	end
  end
  return expr[1]
end

print(eval{expression:match(io.read())})


  

You may also check:How to resolve the algorithm Run-length encoding step by step in the 11l programming language
You may also check:How to resolve the algorithm 99 bottles of beer step by step in the DIBOL-11 programming language
You may also check:How to resolve the algorithm Compare sorting algorithms' performance step by step in the C programming language
You may also check:How to resolve the algorithm I before E except after C step by step in the Batch File programming language
You may also check:How to resolve the algorithm Maximum triangle path sum step by step in the Python programming language