Jump to content




already another problem


  • You cannot reply to this topic
10 replies to this topic

#1 tonkku107

  • Members
  • 140 posts
  • LocationFinland

Posted 18 May 2013 - 07: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

#2 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 18 May 2013 - 08: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. :)

#3 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 18 May 2013 - 08:21 AM

I think you can't save math operators in variables :)

#4 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 18 May 2013 - 08:41 AM

View PostFreack100, on 18 May 2013 - 08:21 AM, said:

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 by Engineer, 18 May 2013 - 08:44 AM.


#5 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 18 May 2013 - 08: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))


#6 tonkku107

  • Members
  • 140 posts
  • LocationFinland

Posted 23 May 2013 - 10:46 AM

View Posttheoriginalbit, on 18 May 2013 - 08:55 AM, said:

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)?

#7 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 23 May 2013 - 11:10 AM

View Posttonkku107, on 23 May 2013 - 10:46 AM, said:

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?

#8 tonkku107

  • Members
  • 140 posts
  • LocationFinland

Posted 23 May 2013 - 11:15 AM

View Posttheoriginalbit, on 23 May 2013 - 11:10 AM, said:

View Posttonkku107, on 23 May 2013 - 10:46 AM, said:

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

#9 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 23 May 2013 - 11:21 AM

View Posttonkku107, on 23 May 2013 - 11:15 AM, said:

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.

#10 tonkku107

  • Members
  • 140 posts
  • LocationFinland

Posted 23 May 2013 - 11:54 AM

View Posttheoriginalbit, on 23 May 2013 - 11:21 AM, said:

View Posttonkku107, on 23 May 2013 - 11:15 AM, said:

I mean if i input like 3+4 it can count it
Count or calculate?
Whatever...

View Posttheoriginalbit, on 23 May 2013 - 11:21 AM, said:

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!
>_

#11 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 23 May 2013 - 11:57 AM

View Posttonkku107, on 23 May 2013 - 11:54 AM, said:

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.





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users