c = a m bit 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
Posted 18 May 2013 - 07:53 AM
c = a m bit says '=' exepted but i tried to add = everywhere!
Posted 18 May 2013 - 08:16 AM
local n1 = 5 local n2 = 10 local calculation = n1 + n2 -- If you print calculation 15 will come out of it
Posted 18 May 2013 - 08:21 AM
Posted 18 May 2013 - 08:41 AM
Freack100, on 18 May 2013 - 08:21 AM, said:
Edited by Engineer, 18 May 2013 - 08:44 AM.
Posted 18 May 2013 - 08:55 AM
local a = 2 --# we cannot save mathematical operators into a variable, we must save them as a string local m = '+' local b = 5
--# 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
--# 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
print(calculate1(a, b, m)) print(calculate2(a, b, m))
Posted 23 May 2013 - 10:46 AM
theoriginalbit, on 18 May 2013 - 08:55 AM, said:
local a = 2 --# we cannot save mathematical operators into a variable, we must save them as a string local m = '+' local b = 5
--# 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
--# 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
print(calculate1(a, b, m)) print(calculate2(a, b, m))
Posted 23 May 2013 - 11:21 AM
tonkku107, on 23 May 2013 - 11:15 AM, said:
Posted 23 May 2013 - 11:54 AM
theoriginalbit, on 23 May 2013 - 11:21 AM, said:
theoriginalbit, on 23 May 2013 - 11:21 AM, said:
Posted 23 May 2013 - 11:57 AM
tonkku107, on 23 May 2013 - 11:54 AM, said:
0 members, 2 guests, 0 anonymous users