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

Pocket Calculator!

Started by Enderslime, 29 October 2015 - 03:15 PM
Enderslime #1
Posted 29 October 2015 - 04:15 PM
I've created a very basic calculator, that does some pretty basic math.
number * number
number + number
number - number
number / number

The installation is pretty easy, all you have to do is the following.

Create a pocket computer and type the following

pastebin get 0wkqTE1A calc

then,

edit startup

once editing startup type,

shell.run("calc")

almost done!

save the program, exit, then reboot your pocket computer (Hold CTRL+R)

ENJOY!
Blue #2
Posted 29 October 2015 - 05:04 PM
Nice and simple,I like it! :)/> Just a few things I'd recommend:
- Use tables,it's alot better than having a lot of "else if"s. It might seem complicated at first but they're really useful! Especially for this type of program.
- Add key support: It would be nice if this was compatible with normal computers(which don't require gold to craft in survival). Because you have no colors,all you need to do is add support for using keys as an alternative to the mouse.
- Make it larger: Pocket computers are small,but not that small! It would be nice if the calculator was a bit larger or if it was more centered,it's a lot easier to use that way!
- Use indenting: Indenting makes code easier to read for you and other people. Also,if you aren't already,navigate to your ComputerCraft folder(in %appdata%) and use a program like Notepad++,it's a lot easier to code that way instead of using the in-game editor.
Also, one more thing,
pastebin get 0wkqTE1A calc
then,
edit startup once editing startup type : shell.run("calc") almost done! save the program, exit, then reboot your pocket computer (Hold CTRL+R)
Why put it as a startup? Last time I checked Windows wasn't a calculator program :P/>. TIP: If you really want to use it as a startup,just do this,you don't need to create a new program:

pastebin get 0wkqTE1A startup
Creator #3
Posted 29 October 2015 - 05:28 PM
You can also do this:
  1. get all the input: "5+3" (this is what the string should look like)
  2. when = is pressed do this
  3. input = "return "..input 
    (input something like:"3+5*6")
  4. then do this:
  5. 
    	local func = loadstring(input)
    	
  6. and finally do this:
  7. 
    	print(func())
    	
Enderslime #4
Posted 29 October 2015 - 07:53 PM
Nice and simple,I like it! :)/> Just a few things I'd recommend:
- Use tables,it's alot better than having a lot of "else if"s. It might seem complicated at first but they're really useful! Especially for this type of program.
- Add key support: It would be nice if this was compatible with normal computers(which don't require gold to craft in survival). Because you have no colors,all you need to do is add support for using keys as an alternative to the mouse.
- Make it larger: Pocket computers are small,but not that small! It would be nice if the calculator was a bit larger or if it was more centered,it's a lot easier to use that way!
- Use indenting: Indenting makes code easier to read for you and other people. Also,if you aren't already,navigate to your ComputerCraft folder(in %appdata%) and use a program like Notepad++,it's a lot easier to code that way instead of using the in-game editor.
Also, one more thing,
pastebin get 0wkqTE1A calc
then,
edit startup once editing startup type : shell.run("calc") almost done! save the program, exit, then reboot your pocket computer (Hold CTRL+R)
Why put it as a startup? Last time I checked Windows wasn't a calculator program :P/>. TIP: If you really want to use it as a startup,just do this,you don't need to create a new program:

pastebin get 0wkqTE1A startup

Quite a bit of things but ill do what i can!

Also I wasnt doing the program in startup because of the fact that someone might have other code there and it might get in the way, thanks for the notepad++ idea BTW :D/>

You can also do this:
  1. get all the input: "5+3" (this is what the string should look like)
  2. when = is pressed do this
  3. input = "return "..input 
    (input something like:"3+5*6")
  4. then do this:
  5. 
    		local func = loadstring(input)
    		
  6. and finally do this:
  7. 
    		print(func())
    		

Could you explain why I should do that?
Creator #5
Posted 29 October 2015 - 08:00 PM
This way you can calculate more complex expressions and not only a*b or a+b, but (a+b*c)/d.
Enderslime #6
Posted 29 October 2015 - 08:07 PM
This way you can calculate more complex expressions and not only a*b or a+b, but (a+b*c)/d.
then ill have to a little more revamping, i have a system put in so that you can do more than one + - / * so that the program doesnt glitch, but ill look into it
Enderslime #7
Posted 29 October 2015 - 08:15 PM
Nice and simple,I like it! :)/> Just a few things I'd recommend:
- Use tables,it's alot better than having a lot of "else if"s. It might seem complicated at first but they're really useful! Especially for this type of program.
- Add key support: It would be nice if this was compatible with normal computers(which don't require gold to craft in survival). Because you have no colors,all you need to do is add support for using keys as an alternative to the mouse.
- Make it larger: Pocket computers are small,but not that small! It would be nice if the calculator was a bit larger or if it was more centered,it's a lot easier to use that way!
- Use indenting: Indenting makes code easier to read for you and other people. Also,if you aren't already,navigate to your ComputerCraft folder(in %appdata%) and use a program like Notepad++,it's a lot easier to code that way instead of using the in-game editor.
Also, one more thing,
pastebin get 0wkqTE1A calc
then,
edit startup once editing startup type : shell.run("calc") almost done! save the program, exit, then reboot your pocket computer (Hold CTRL+R)
Why put it as a startup? Last time I checked Windows wasn't a calculator program :P/>. TIP: If you really want to use it as a startup,just do this,you don't need to create a new program:

pastebin get 0wkqTE1A startup
quick question, how do you enlarge text on pocket computer/computer, the term api says its only for monitors.
Enderslime #8
Posted 29 October 2015 - 08:24 PM
I'm looking at the tables wiki page and a tutorial, and I don't really get it, anyone mind to explain?
Creator #9
Posted 29 October 2015 - 08:38 PM
You can't enlarge text on pocket PCs.
Enderslime #10
Posted 29 October 2015 - 08:54 PM
You can't enlarge text on pocket PCs.
guessing that also applies to computers.
Creator #11
Posted 29 October 2015 - 08:57 PM
Yup, it does.

If you use loadstring, you don't need the ifs about +,-,*,/.
Enderslime #12
Posted 29 October 2015 - 09:03 PM
currently i'm rewriting the code with tables, my dad just explained.
Blue #13
Posted 29 October 2015 - 09:06 PM
currently i'm rewriting the code with tables, my dad just explained.
Awesome!
Creator #14
Posted 29 October 2015 - 09:07 PM
currently i'm rewriting the code with tables, my dad just explained.
Awesome!

Awesome dad! Where do you get one of these? (Mine is awesome too, but he can't code.)
Enderslime #15
Posted 29 October 2015 - 09:17 PM
currently i'm rewriting the code with tables, my dad just explained.
Awesome!

Awesome dad! Where do you get one of these? (Mine is awesome too, but he can't code.)
lol, here is what i have currently, this might be a bit before i give the finished product. (cause its not done yet :P/>)

Buttons["1"]["1"] = "1"
Buttons["1"]["3"] = "2"
Buttons["1"]["5"] = "3"

Buttons["2"]["1"] = "4"
Buttons["2"]["3"] = "5"
Buttons["2"]["5"] = "6 "

Buttons["3"]["1"] = "7"
Buttons["3"]["3"] = "8"
Buttons["3"]["5"] = "9"

Buttons["4"]["1"] = "+"
Buttons["4"]["3"] = "0"
Buttons["4"]["5"] = "*"

Buttons["5"]["1"] = "-"
Buttons["5"]["3"] = "="
Buttons["5"]["5"] = "/"
Creator #16
Posted 29 October 2015 - 09:20 PM
Replace the string indexes with actual number indexes because the mouse click event returns numbers. Then you can do:

local aUselessVar = Buttons[x] and Buttons[x][y] and Buttons[x][y]()

That way, no ifs!
Enderslime #17
Posted 29 October 2015 - 09:31 PM
currently its not even getting to the click event, its just giving the error :
calc2:1: attempt to index ? (a nil value)
Creator #18
Posted 29 October 2015 - 09:37 PM
Code?
Enderslime #19
Posted 29 October 2015 - 09:40 PM
my dad and i were testing, the reason we know it doesnt get to the click event is because it gets that eroor with this.
Buttons["1"]["1"] = "1"
Buttons["1"]["3"] = "2"
Buttons["1"]["5"] = "3"

Buttons["2"]["1"] = "4"
Buttons["2"]["3"] = "5"
Buttons["2"]["5"] = "6 "

Buttons["3"]["1"] = "7"
Buttons["3"]["3"] = "8"
Buttons["3"]["5"] = "9"

Buttons["4"]["1"] = "+"
Buttons["4"]["3"] = "0"
Buttons["4"]["5"] = "*"

Buttons["5"]["1"] = "-"
Buttons["5"]["3"] = "="
Buttons["5"]["5"] = "/"
Enderslime #20
Posted 29 October 2015 - 09:46 PM
my dad and i were testing, the reason we know it doesnt get to the click event is because it gets that eroor with this.
Buttons["1"]["1"] = "1"
Buttons["1"]["3"] = "2"
Buttons["1"]["5"] = "3"

Buttons["2"]["1"] = "4"
Buttons["2"]["3"] = "5"
Buttons["2"]["5"] = "6 "

Buttons["3"]["1"] = "7"
Buttons["3"]["3"] = "8"
Buttons["3"]["5"] = "9"

Buttons["4"]["1"] = "+"
Buttons["4"]["3"] = "0"
Buttons["4"]["5"] = "*"

Buttons["5"]["1"] = "-"
Buttons["5"]["3"] = "="
Buttons["5"]["5"] = "/"
also, my dad isnt the best at lua, he is better at c sharp / java
Creator #21
Posted 29 October 2015 - 10:06 PM
Replace Button["3"]["3"] with Button[3][3].
Enderslime #22
Posted 30 October 2015 - 12:13 AM
got basic math working with the tables, 20+ lines smaller, working on making infinite equations

got basic math working with the tables, 20+ lines smaller, working on making infinite equations
also, here is my current code
local Buttons = {}

Buttons["1,1"] = "1"
Buttons["1,3"] = "2"
Buttons["1,5"] = "3"

Buttons["2,1"] = "4"
Buttons["2,3"] = "5"
Buttons["2,5"] = "6 "

Buttons["3,1"] = "7"
Buttons["3,3"] = "8"
Buttons["3,5"] = "9"

Buttons["4,1"] = "+"
Buttons["4,3"] = "0"
Buttons["4,5"] = "*"

Buttons["5,1"] = "-"
Buttons["5,3"] = "="
Buttons["5,5"] = "/"

current = ""

while true do
term.setCursorPos(1,1)
term.clear()
print("1 2 3")
print("4 5 6")
print("7 8 9")
print("+ 0 *")
print("- = /")
print(current)
local event, button, x, y = os.pullEvent("mouse_click")
press = Buttons[y..","..x]
if press == nil then
elseif press == "+" or press == "-" or press == "*" or press == "/" then
sign = press
num1 = current
current = ""
elseif press == "=" then
num2 = current
if sign == "+" then
answer = num1 + num2
elseif sign == "-" then
answer = num1 - num2
elseif sign == "*" then
answer = num1 * num2
elseif sign ==
end
print(answer)
sleep(5)
current = ""
else
current = current..press
end
end
Enderslime #23
Posted 30 October 2015 - 12:19 AM
got basic math working with the tables, 20+ lines smaller, working on making infinite equations

got basic math working with the tables, 20+ lines smaller, working on making infinite equations
also, here is my current code
local Buttons = {}

Buttons["1,1"] = "1"
Buttons["1,3"] = "2"
Buttons["1,5"] = "3"

Buttons["2,1"] = "4"
Buttons["2,3"] = "5"
Buttons["2,5"] = "6 "

Buttons["3,1"] = "7"
Buttons["3,3"] = "8"
Buttons["3,5"] = "9"

Buttons["4,1"] = "+"
Buttons["4,3"] = "0"
Buttons["4,5"] = "*"

Buttons["5,1"] = "-"
Buttons["5,3"] = "="
Buttons["5,5"] = "/"

current = ""

while true do
term.setCursorPos(1,1)
term.clear()
print("1 2 3")
print("4 5 6")
print("7 8 9")
print("+ 0 *")
print("- = /")
print(current)
local event, button, x, y = os.pullEvent("mouse_click")
press = Buttons[y..","..x]
if press == nil then
elseif press == "+" or press == "-" or press == "*" or press == "/" then
sign = press
num1 = current
current = ""
elseif press == "=" then
num2 = current
if sign == "+" then
answer = num1 + num2
elseif sign == "-" then
answer = num1 - num2
elseif sign == "*" then
answer = num1 * num2
elseif sign ==
end
print(answer)
sleep(5)
current = ""
else
current = current..press
end
end
messed up with the copy paste there, use this code
local Buttons = {}

Buttons["1,1"] = "1"
Buttons["1,3"] = "2"
Buttons["1,5"] = "3"

Buttons["2,1"] = "4"
Buttons["2,3"] = "5"
Buttons["2,5"] = "6 "

Buttons["3,1"] = "7"
Buttons["3,3"] = "8"
Buttons["3,5"] = "9"

Buttons["4,1"] = "+"
Buttons["4,3"] = "0"
Buttons["4,5"] = "*"

Buttons["5,1"] = "-"
Buttons["5,3"] = "="
Buttons["5,5"] = "/"

current = ""

while true do
term.setCursorPos(1,1)
term.clear()
print("1 2 3")
print("4 5 6")
print("7 8 9")
print("+ 0 *")
print("- = /")
print(current)
local event, button, x, y = os.pullEvent("mouse_click")
press = Buttons[y..","..x]
if press == nil then
elseif press == "+" or press == "-" or press == "*" or press == "/" then
sign = press
num1 = current
current = ""
elseif press == "=" then
num2 = current
if sign == "+" then
answer = num1 + num2
elseif sign == "-" then
answer = num1 - num2
elseif sign == "*" then
answer = num1 * num2
elseif sign == "/" then
answer = num1 / num2
end
print(answer)
sleep(5)
current = ""
else
current = current..press
end
end
Creator #24
Posted 30 October 2015 - 05:13 AM
Why the sleep?
Creator #25
Posted 30 October 2015 - 05:38 AM
You forgot various things like tostringing x & y:


local Buttons = {}
Buttons["1,1"] = "1"
Buttons["1,3"] = "2"
Buttons["1,5"] = "3"
Buttons["2,1"] = "4"
Buttons["2,3"] = "5"
Buttons["2,5"] = "6 "
Buttons["3,1"] = "7"
Buttons["3,3"] = "8"
Buttons["3,5"] = "9"
Buttons["4,1"] = "+"
Buttons["4,3"] = "0"
Buttons["4,5"] = "*"
Buttons["5,1"] = "-"
Buttons["5,3"] = "="
Buttons["5,5"] = "/"
current = ""
while true do
term.setCursorPos(1,1)
term.clear()
print("1 2 3")
print("4 5 6")
print("7 8 9")
print("+ 0 *")
print("- = /")
print(current)
local event, button, x, y = os.pullEvent("mouse_click")
press = Buttons[tostring(y)..","..tostring(x)]
if press then
   if press == "=" then
	  local func = loadstring("return "..current)
	  if func then func() end
	  current = ""
   else
	   current = current..press
   end
end
end
Blue #26
Posted 30 October 2015 - 06:55 AM
Why the sleep?
Because otherwise the code wouldn't display the answer,it would just continue the loop. This could be solved by keeping the answer like real calculators. For example : 7 x 8 = 12 then x2 = 22 instead of 7 x 8 x 2.
Edited on 30 October 2015 - 06:00 AM
Creator #27
Posted 30 October 2015 - 08:31 AM
Why the sleep?
Because otherwise the code wouldn't display the answer,it would just continue the loop. This could be solved by keeping the answer like real calculators. For example : 7 x 8 = 12 then x2 = 22 instead of 7 x 8 x 2.

Or you do read(). That way you can determine the time you want to waste.
Enderslime #28
Posted 30 October 2015 - 01:31 PM
You forgot various things like tostringing x & y:


local Buttons = {}
Buttons["1,1"] = "1"
Buttons["1,3"] = "2"
Buttons["1,5"] = "3"
Buttons["2,1"] = "4"
Buttons["2,3"] = "5"
Buttons["2,5"] = "6 "
Buttons["3,1"] = "7"
Buttons["3,3"] = "8"
Buttons["3,5"] = "9"
Buttons["4,1"] = "+"
Buttons["4,3"] = "0"
Buttons["4,5"] = "*"
Buttons["5,1"] = "-"
Buttons["5,3"] = "="
Buttons["5,5"] = "/"
current = ""
while true do
term.setCursorPos(1,1)
term.clear()
print("1 2 3")
print("4 5 6")
print("7 8 9")
print("+ 0 *")
print("- = /")
print(current)
local event, button, x, y = os.pullEvent("mouse_click")
press = Buttons[tostring(y)..","..tostring(x)]
if press then
   if press == "=" then
	  local func = loadstring("return "..current)
	  if func then func() end
	  current = ""
   else
	   current = current..press
   end
end
end
]
i gave you the code that doesn't do large equations, (and it works) what would tostring do?
Creator #29
Posted 30 October 2015 - 01:36 PM
Mouse click events returns numbers, however, you can't concatenate a number and a string. This is why you use to string.
Enderslime #30
Posted 30 October 2015 - 01:37 PM
that code works tho?
Creator #31
Posted 30 October 2015 - 01:39 PM
It should just add a sleep after the current = "".

How do you respond so fast?
Enderslime #32
Posted 30 October 2015 - 01:42 PM
continually refreshing,
Enderslime #33
Posted 30 October 2015 - 01:49 PM
Why the sleep?
Because otherwise the code wouldn't display the answer,it would just continue the loop. This could be solved by keeping the answer like real calculators. For example : 7 x 8 = 12 then x2 = 22 instead of 7 x 8 x 2.
Why the sleep?
Because otherwise the code wouldn't display the answer,it would just continue the loop. This could be solved by keeping the answer like real calculators. For example : 7 x 8 = 12 then x2 = 22 instead of 7 x 8 x 2.
thats what im planning on, and also 12x2 is not 22 and 7x8 is not 12…

Why the sleep?
Because otherwise the code wouldn't display the answer,it would just continue the loop. This could be solved by keeping the answer like real calculators. For example : 7 x 8 = 12 then x2 = 22 instead of 7 x 8 x 2.
Why the sleep?
Because otherwise the code wouldn't display the answer,it would just continue the loop. This could be solved by keeping the answer like real calculators. For example : 7 x 8 = 12 then x2 = 22 instead of 7 x 8 x 2.
thats what im planning on, and also 12x2 is not 22 and 7x8 is not 12…
oops double quote
Blue #34
Posted 30 October 2015 - 03:11 PM
Enderslime said:
thats what im planning on, and also 12x2 is not 22 and 7x8 is not 12…
Lol! I don't know what I was thinking there.
Enderslime #35
Posted 30 October 2015 - 03:35 PM
Enderslime said:
thats what im planning on, and also 12x2 is not 22 and 7x8 is not 12…
Lol! I don't know what I was thinking there.
lol
H4X0RZ #36
Posted 30 October 2015 - 08:09 PM
Mouse click events returns numbers, however, you can't concatenate a number and a string. This is why you use to string.
You can't? try entering

(1)..(1)
into the Lua promt. :P/>
Edited on 30 October 2015 - 07:12 PM
Enderslime #37
Posted 31 October 2015 - 06:00 PM
is there a way to remove a decimal from a number? its a different program im working on
H4X0RZ #38
Posted 31 October 2015 - 06:02 PM
is there a way to remove a decimal from a number? its a different program im working on

You can round the number. Either use math.floor (always rounds down) or math.ceil (always rounds up).

Or use a custom function (something like this):

function round(n)
  return (math.floor(n+.49)==math.floor(n)) and math.floor(n) or math.ceil(n)
end
Edited on 31 October 2015 - 05:05 PM
Enderslime #39
Posted 31 October 2015 - 06:06 PM
is there a way to remove a decimal from a number? its a different program im working on
say i have 2.67, i just want the 2

is there a way to remove a decimal from a number? its a different program im working on
say i have 2.67, i just want the 2
math.floor(2.67) ?

is there a way to remove a decimal from a number? its a different program im working on
say i have 2.67, i just want the 2

is there a way to remove a decimal from a number? its a different program im working on
say i have 2.67, i just want the 2
math.floor(2.67) ?
or a = math.floor(2.67) ?
so a would be 2?

ok, this works!

print("Num")
a = read()
a = math.floor(a)
print(a)

ty
H4X0RZ #40
Posted 31 October 2015 - 06:08 PM
If you always want the number before the point ignoring the "if the number after the point is greater than .5 round up to the next full number" rule you should use math.floor. The function will return the rounded number so…

someVariable = 2.67
someOtherVariable = math.floor(2.67)
LDDestroier #41
Posted 12 November 2015 - 01:23 AM
math.floor() rounds down.
math.ceil() rounds up.
There isn't any standard function to round to the closest integer, but this'll work, methinks:

function round(number)
    return math.floor(number+0.5)
end