number = io.read()
x=(number)
print(x)
Isnt lua supposed to have some sort of built in math thing?
Probably a silly question, but still.
number = tonumber(io.read())
local function isValid(char)
if not char then return false end
if tonumber(char) ~= nil then return true end
char = tostring(char)
if char == "+" or char == "-" or char == "*" or char == "/"
or char == "(" or char == ")" or char == "^" or char == "."
or char == "%"
then
return true
else
return false
end
end
local function evalString( str )
if not str then return nil end
local filtStr = ""
for i=1, #str do
if isValid(str:sub(i,i)) then
filtStr = filtStr..str:sub(i,i)
end
end
local fCalc = loadstring("return "..filtStr)
setfenv(fCalc, getfenv())
return fCalc()
end
print(evalString("2.2*2")) --Outputs 4.4
print(evalString("(2+2-1)^2")) --Outputs 9
--And so on
while true do
write("> ")
string=read()
assert(loadstring("result="..string))()
print(result)
end
i have a better way of doing it, it supports complex functions too!
not sure if this will work, havent tried it in gamewhile true do write("> ") string=read() assert(loadstring("result="..string))() print(result) end
calculator virus anyone? :)/>/>i have a better way of doing it, it supports complex functions too!
not sure if this will work, havent tried it in gamewhile true do write("> ") string=read() assert(loadstring("result="..string))() print(result) end
While yes this would work it is not really a calculator per say it is effectively just a lua interpreter,
Allowing people to input whatever the hell they like and it will run that code no matter what.