This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Slyth's profile picture

[Lua]Why wont my Calculator work?

Started by Slyth, 08 May 2012 - 12:56 AM
Slyth #1
Posted 08 May 2012 - 02:56 AM
Ok, just randomly thought of this and I was wondering why it only output the problem I put into it?


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.
Luanub #2
Posted 08 May 2012 - 03:02 AM
You probably need to convert it to numbers to work, io.read()'s output will be a string. To convert to a number do

number = tonumber(io.read())
Grim Reaper #3
Posted 08 May 2012 - 03:39 AM
Sorry, but LUA doesn't have an automatic string to expression converter, at least as far as I know. You'd have to build one from the ground up which I would imagine wouldn't be very difficult at all depending on your experience level.

The easiest way to do this for simple expressions would be to ask for a first number, then an operation, then a second number, and finally to print the result.
Xtansia #4
Posted 08 May 2012 - 05:20 AM
This is an extremely quick and hackish way of doing it,
It does not support sin/cos/tan and the such only the basic operators(+,-./,*,^,%) and ignores all other characters,


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
Slyth #5
Posted 08 May 2012 - 12:12 PM
Ok cool, I didnt know about the tonumber thing. First time coding with Lua
Tomass: Thank you, explained lots of things to me.
PixelToast #6
Posted 11 May 2012 - 05:32 AM
i have a better way of doing it, it supports complex functions too!
not sure if this will work, havent tried it in game


while true do
  write("> ")
  string=read()
  assert(loadstring("result="..string))()
  print(result)
end
Xtansia #7
Posted 11 May 2012 - 06:21 AM
i have a better way of doing it, it supports complex functions too!
not sure if this will work, havent tried it in game


while 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.
PixelToast #8
Posted 11 May 2012 - 07:30 AM
i have a better way of doing it, it supports complex functions too!
not sure if this will work, havent tried it in game


while 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.
calculator virus anyone? :)/>/>