Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Execute Lua Online

a = "7n5m4m2p1d6"

function trenne(s, i, j)
    b = string.sub(s,i,j) 
    return b
end
i = 1
c = 0

while i < string.len(a) do
    x1 = tonumber(trenne(a, i, i))
    x2 = tonumber(trenne(a, i+2, i+2))
    if i < 3 then
        
        if trenne(a, i+1, i+1) == "p" then
          c = x1 + x2
        elseif trenne(a, i+1, i+1) == "m" then
          c = x1 - x2
        elseif trenne(a, i+1, i+1) == "n" then
          c = x1 * x2
        elseif trenne(a, i+1, i+1) == "d" then
          c = x1 / x2
        else
          print("invalid operation")
        end
        i = i +3
    else
        x1 = c
        x2 = tonumber(trenne(a,i+1,i+1))
        
        if trenne(a, i, i) == "p" then
          c = x1 + x2
        elseif trenne(a, i, i) == "m" then
          c = x1 - x2
        elseif trenne(a, i, i) == "n" then
          c = x1 * x2
        elseif trenne(a, i, i) == "d" then
          c = x1 / x2
        else
          print("invalid operation")
        end
        i = i +2
    end
end
print(c)

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.