以下是用 Aardio 写的计算器代码:

function main()
    sys.splash('欢迎使用计算器!')

    while true do
        local expr = sys.input('请输入表达式(例如 1 + 2):')
        if not expr then break end

        local num1, op, num2 = string.match(expr, '(\d+%.?\d*)%s*([%+%-*/])%s*(\d+%.?\d*)')
        if not num1 or not op or not num2 then
            sys.alert('表达式不合法,请重新输入!')
        else
            num1, num2 = tonumber(num1), tonumber(num2)
            local result
            if op == '+' then
                result = num1 + num2
            elseif op == '-' then
                result = num1 - num2
            elseif op == '*' then
                result = num1 * num2
            elseif op == '/' then
                if num2 == 0 then
                    sys.alert('除数不能为0,请重新输入!')
                    goto continue
                end
                result = num1 / num2
            end
            sys.alert(string.format('结果为:%.2f', result))
        end

        ::continue::
    end
    sys.splash('再见!')
end

运行效果:

计算器运行效果

Aardio 计算器:精确到小数点两位,支持加减乘除

原文地址: https://www.cveoy.top/t/topic/nqSB 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录