Difference between revisions of "Calculator Tutorial"

From ComputerCraft Wiki
Jump to: navigation, search
m (Moved to tutorials category)
 
(4 intermediate revisions by 4 users not shown)
Line 1: Line 1:
------- is what that string does
+
<code>
  
add = "add" ------ it says when i tell it to add then it codes it to the word add
+
-- anything after two dashes is a comment, and wont be compiled.
 +
--[[ this is also a comment
 +
and so is this
 +
]]--
 +
 +
--this tutorial will teach you the basics of the print() and read() functions, if statements, as well as some variable use and math operations.
 +
 +
 +
print("Do you want to add, subtract, multiply or divide?") -- this code displays the question within the quotation marks
 +
op = read() -- this code creates a variable called 'op', the read() function stalls the program to accept user input, which is stored in op
 +
print("") -- just gives us some space to work with
 +
 +
print("what is the first number to be operated on?")
 +
 +
num1 = tonumber(read()) -- accepts the first number to be stored in the variable 'number1'
 +
print("")
 +
 +
print("and the second number?")
 +
num2 = tonumber(read())
 +
print("")
 +
 +
-- now we operate on the number depending on what the user typed first
 +
 +
if op == "add" then -- if you need help with conditional statements, see this page: [http://www.troubleshooters.com/codecorn/lua/luaif.htm]
 +
  result = num1+num2
 +
print(result) -- this prints the result
 +
end
 +
 +
if op == "multiply" then
 +
  result = num1*num2 -- asterisk represents multiply
 +
print(result)
 +
end
 +
 +
if op == "divide" then
 +
  result = num1/num2
 +
print(result)
 +
end
 +
 +
if op == "subtract" then
 +
  result = num1-num2
 +
print(result)
 +
end
  
subtract = "subtract" ------ the same as for add just with subtract
+
</code>
 
+
[[Category:Tutorials]]
multiply = "multiply" ------ same as for add just with multiply
+
 
+
devide = "devide" ------ sameas for add just for devide
+
 
+
print ("Do you want to add, subtract, multiply or devide?") ------- prints the message written between (" and ")
+
 
+
write ("") ------- makes room for you to write if you want add, subtract, multiply or devide.
+
 
+
put = io.read() ------ reads what you write and codes it in to the word "put"
+
 
+
write ("Enter your first number: ") ------ makes room for you to write your first number.
+
 
+
input = io.read() ------ reads what you write above and codes it in to the word "input"
+
 
+
write ("Enter your second number: ") -------- makes room for you to write the second number.
+
 
+
output = io.read() ------- reads what you write above and codes it into the word "output"
+
 
+
if put == add ------ then checks if the word you wrote when you chose if you wanted to add, subtract, multiply or devide then it checks if its the same as add, if its not thn it does nothing
+
 
+
x = input + output ------- if the word you wrote was the same as add then this adds the first number (input) and the second number (output) and then codes it into the letter x
+
 
+
print ("That = ", x) ------- prints That = (and then what x is)
+
 
+
elseif put == subtract then ------- if its not add that you wrote then this checks if its subtract
+
 
+
x = input - output -------- if it was subtract then this subtracts the input from the output and codes it into the letter x
+
 
+
print ("That = ", x) ------- the same as for add
+
 
+
elseif put == multiply then ------- if its not add or subtract you wrote then it checks if its multiply
+
 
+
x = input * output ------- if it is it multiplies input and output and codes it into the letter x
+
 
+
print ("That = ", x)------ the same as for add
+
 
+
elseif put == devide then -------- if its not add, subtract or multiply you wrote then this checks if its devide
+
 
+
x = input / output ------- this devides input from output and codes them into the letter x
+
 
+
print ("That = ", x) ------- the same as for add
+
 
+
end ------ since we only got  if then we only need 1 end
+
 
+
sleep(3) ------- waits 3 seconds before executing the next string
+
 
+
term.clear() ------- clears the screen
+
 
+
term.setCursorPos(1, 1) ------- returns the cursor to the top of the screen
+

Latest revision as of 07:54, 26 March 2014

-- anything after two dashes is a comment, and wont be compiled.
--[[ this is also a comment
and so is this
]]--

--this tutorial will teach you the basics of the print() and read() functions, if statements, as well as some variable use and math operations.


print("Do you want to add, subtract, multiply or divide?") -- this code displays the question within the quotation marks
op = read() -- this code creates a variable called 'op', the read() function stalls the program to accept user input, which is stored in op
print("") -- just gives us some space to work with

print("what is the first number to be operated on?")

num1 = tonumber(read()) -- accepts the first number to be stored in the variable 'number1'
print("")

print("and the second number?")
num2 = tonumber(read())
print("")

-- now we operate on the number depending on what the user typed first

if op == "add" then -- if you need help with conditional statements, see this page: [1]
 result = num1+num2
print(result) -- this prints the result
end

if op == "multiply" then
 result = num1*num2 -- asterisk represents multiply
print(result)
end

if op == "divide" then
 result = num1/num2
print(result)
end

if op == "subtract" then
 result = num1-num2
print(result)
end