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

Convert "+" into a math symbol?

Started by andrewverry49000, 08 October 2012 - 08:29 AM
andrewverry49000 #1
Posted 08 October 2012 - 10:29 AM
print("Calculator:)
write"Number 1"
input = read()
num1 = input

write"Number 2"
input = read()
num2 = input

write"Action"
input = read()
act = input

string.format("%f %f %f, num1, act, num2)


I am trying to make an easy calculator, however, I am having trouble converting the "act" variable into
a usable math character I get this error: "Calc:14: bad argument: number expected, got string"

any help or ideas would be greatly appreciated.
Thanks
Orwell #2
Posted 08 October 2012 - 10:37 AM
It's not clear to me what your endgoal is here. But as far as the formatting goes, use '%s' for strings. So that would be:

string.format("%f %s %f, num1, act, num2)
KaoS #3
Posted 08 October 2012 - 10:54 AM
well my way of doing this is a table, I am not great with string formatting but I know you can separate it (as demonstrated above), then all you do is pass the 3 parts to a function


local function mathMaker(num1,operator,num2)
  local tOperators={}
  tOperators['-']=function(num1,num2) return num1-num2 end
  tOperators['+']=function(num1,num2) return num1+num2 end --complete the operators you will be using
  return tOperators[operator](num1,num2)
end
andrewverry49000 #4
Posted 08 October 2012 - 10:11 PM
Sorry, for the action, you input + - * etc.
Convert them to a math symbol, then run the calculation
Laserman34170 #5
Posted 09 October 2012 - 12:29 AM

local mathfunctions = {
  ["+"] = function(in1, in2) return in1 + in2 end,
  ["-"] = function in1, in2) return in1 - in2 end
}

print("4 + 3 is "..mathfunctions["+"](4, 3))
print("7 - 6 is "..mathfunctions["-"](7, 6))