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

already another problem

Started by tonkku107, 18 May 2013 - 06:53 AM
tonkku107 #1
Posted 18 May 2013 - 08:53 AM

c = a m b
it says '=' exepted but i tried to add = everywhere!
a = a number
b = another number
m = + / - / * / :
What is wrong?
I'm attempting to do a simple caculator
Engineer #2
Posted 18 May 2013 - 09:16 AM
Can you show us the code you are using?
Because you can assign a value to a variable:

local n1 = 5
local n2 = 10
local calculation =  n1 + n2
-- If you print calculation 15 will come out of it

So, we really need your code. Nobody is going to steal it.. Or at least 99.9% of this community dont steal code. :)/>
H4X0RZ #3
Posted 18 May 2013 - 09:21 AM
I think you can't save math operators in variables :)/>
Engineer #4
Posted 18 May 2013 - 09:41 AM
I think you can't save math operators in variables :)/>/>
He is doing a calculation…
EDIT: At least, I think he is doing that


EDIT2: You are right, you cannot do this calculation: 2 / - / 5 etc.. At max you can only have two operators, but only a + or - in combination with a * or /. Thank you for correcting me!
Edited on 18 May 2013 - 07:44 AM
theoriginalbit #5
Posted 18 May 2013 - 09:55 AM
ok so this is 2 of the ways to get what you want done.

just our numbers to test with

local a = 2
--# we cannot save mathematical operators into a variable, we must save them as a string
local m = '+'
local b = 5

method 1

--# we need 3 things, the two numbers, and the mathematical operator in string form
local function calculate1(num1, num2, op)
  --# now we check which operator it is and return the result based off that operator
  if op == '+' then
	return num1 + num2
  elseif op == '-' then
	return num1 - num2
  elseif op == '/' then
	return num1 / num2
  elseif op == '*' then
	return num1 * num2
  end
  --# no valid operator supplied
  error('Invalid operator: '..tostring(op), 0)
end

method 2

--# we need 3 things, the two numbers, and the mathematical operator in string form
local function calculate2(num1, num2, op)
  --# we use loadstring to put our values into a statement
  local func = loadstring( 'return '..num1..' '..op..' '..num2 )
  --# run the loadstring function
  return func()
  --# note we can do the above 2 lines in one line, I split it out to make it more apparent whats happening, this is how you would do it in one line
  --# return loadstring( 'return '..num1..' '..op..' '..num2 )()
end

test the functions

print(calculate1(a, b, m))
print(calculate2(a, b, m))
tonkku107 #6
Posted 23 May 2013 - 11:46 AM
ok so this is 2 of the ways to get what you want done.

just our numbers to test with

local a = 2
--# we cannot save mathematical operators into a variable, we must save them as a string
local m = '+'
local b = 5

method 1

--# we need 3 things, the two numbers, and the mathematical operator in string form
local function calculate1(num1, num2, op)
  --# now we check which operator it is and return the result based off that operator
  if op == '+' then
	return num1 + num2
  elseif op == '-' then
	return num1 - num2
  elseif op == '/' then
	return num1 / num2
  elseif op == '*' then
	return num1 * num2
  end
  --# no valid operator supplied
  error('Invalid operator: '..tostring(op), 0)
end

method 2

--# we need 3 things, the two numbers, and the mathematical operator in string form
local function calculate2(num1, num2, op)
  --# we use loadstring to put our values into a statement
  local func = loadstring( 'return '..num1..' '..op..' '..num2 )
  --# run the loadstring function
  return func()
  --# note we can do the above 2 lines in one line, I split it out to make it more apparent whats happening, this is how you would do it in one line
  --# return loadstring( 'return '..num1..' '..op..' '..num2 )()
end

test the functions

print(calculate1(a, b, m))
print(calculate2(a, b, m))
Thanks! is there any way of putting like 10-2 into a single input (10 would ne num1 - will be op and 2 will be num2)?
theoriginalbit #7
Posted 23 May 2013 - 12:10 PM
Thanks! is there any way of putting like 10-2 into a single input (10 would ne num1 - will be op and 2 will be num2)?
Not quite sure what you mean. do you mean like the loadstring method where its all put into one string?
tonkku107 #8
Posted 23 May 2013 - 12:15 PM
Thanks! is there any way of putting like 10-2 into a single input (10 would ne num1 - will be op and 2 will be num2)?
Not quite sure what you mean. do you mean like the loadstring method where its all put into one string?
I mean if i input like 3+4 it can count it
theoriginalbit #9
Posted 23 May 2013 - 12:21 PM
I mean if i input like 3+4 it can count it
Count or calculate? i.e. would the input say 3 (number of parts) or 7 (the sum of 3 and 4)
If you mean calculate then BOTH of the methods that I provided will calculate the result of 3+4 and return the value.
tonkku107 #10
Posted 23 May 2013 - 12:54 PM
I mean if i input like 3+4 it can count it
Count or calculate?
Whatever…
i.e. would the input say 3 (number of parts) or 7 (the sum of 3 and 4)
If you mean calculate then BOTH of the methods that I provided will calculate the result of 3+4 and return the value.
if i input 3+4 it will say: 7
Like So:
>program
What would you like to caculate ( or count i don't know): 3+4
It is 7!
>_
theoriginalbit #11
Posted 23 May 2013 - 12:57 PM
if i input 3+4 it will say: 7
Like So:
>program
What would you like to caculate ( or count i don't know): 3+4
It is 7!
>_
In that case you would just use loadstring instead of passing two values and an operator to the calculate function.