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

Aufgabe 2 Grundlagen in LUA (PARSER) Lars Dietrich

function split(str)
    local table_temp = {}
    local i = 0
    for letter in str:gmatch"." do
        table_temp[i] = letter
        i = i + 1
    end
    return table_temp
end


function output_table(ausgabe)
    local i = 0
    while i <= #ausgabe do
        print(ausgabe[i])
        i = i + 1 
    end
end

function is_operation(str)
    for key, value in pairs({"p","m","n","d"}) do
        if value == str then 
            return true 
        end
    end
    return false
end



function parsing(str)
    local table_temp = split(str)
    local i = 0
    local result = tonumber(table_temp[0])
    
    while i <= #table_temp do
        element = table_temp[i]
        if is_operation(element) then
            
            if      element == "p" then
                result = result + tonumber(table_temp[i + 1])

                
            elseif  element == "m" then
                result = result - tonumber(table_temp[i + 1]) 

                
            elseif  element == "n" then
                result = result * tonumber(table_temp[i + 1])

                
            elseif  element == "d" then
                result = result / tonumber(table_temp[i + 1])

                
            else
                print("Wrong Input")
            end
            
        end
        
        
        i = i + 1 
    end
    print(result)
end

----------------------------------------------------------

operations = "2p3p3m6p2"


parsing(operations)

----------------------------------------------------------

Advertisements
Loading...

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