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

Really Simple Calculator

Started by Daegrath(), 10 December 2012 - 07:34 PM
Daegrath() #1
Posted 10 December 2012 - 08:34 PM
a = {…}
if a[2] == ("*") then
print(a[1]*a[3])
elseif a[2] == ("+") then
print(a[1]+a[3])
elseif a[2] == ("-") then
print(a[1]-a[3])
elseif a[2] == ("/") then
print(a[1]/a[3])
end
i_am_th0r #2
Posted 11 December 2012 - 12:28 PM
U need to debug your program becuase there is a problem with your code ,i typed it in exactly and i get an error,as well add these lines of code
term.clear()
term.setCursorPos(1,1)

heres some images
http://imgur.com/nHXmO
http://imgur.com/DkLA0

in one of the images i edited the = sign i messed up but i still receieve the same error.Please show some proof of this calculator working.
If it works thumbs up
Daegrath() #3
Posted 11 December 2012 - 01:40 PM
Example: Cal 728 + 874
Put the name of the script where it says Cal.
Daegrath() #4
Posted 11 December 2012 - 01:43 PM
Do you use the latest version of ComputerCraft?
Daegrath() #5
Posted 11 December 2012 - 01:47 PM
The Script
Daegrath() #6
Posted 11 December 2012 - 02:04 PM
Here is a photo of it working
PixelToast #7
Posted 11 December 2012 - 02:09 PM
why not just:

print(({pcall(setfenv(loadstring(table.concat({ ... }," ")),math))})[2])
or something like that
Daegrath() #8
Posted 11 December 2012 - 02:34 PM
what does that do?
Bubba #9
Posted 11 December 2012 - 03:08 PM
what does that do?

It does math. Basically it just loads whatever math you input into the program. It also loads the math library for math operations and changes the environment so that you don't have to type say math.sqrt(9). Instead you just say sqrt(9)
Daegrath() #10
Posted 11 December 2012 - 03:59 PM
How would you implement that into a calculator that does the same as mine does?
Bubba #11
Posted 11 December 2012 - 04:06 PM
How would you implement that into a calculator that does the same as mine does?

It does do the same thing that yours does, just with more usable functions and in a more complex way. With Pixel's program you could do the following:
progname 2+2
4
progname sqrt(6+3)
3
progname floor(0.5)
0
progname ceil(0.5)
1

See? I'm not 100% code that the code works as I can't test it, but it looks about right.
Daegrath() #12
Posted 11 December 2012 - 04:33 PM
I copy and pasted it into a computer and it does not seem to be working.
Bubba #13
Posted 11 December 2012 - 04:39 PM
I copy and pasted it into a computer and it does not seem to be working.

Yeah I can't debug because my Java installation is broken. But the general idea would work. Maybe try this?


local tInput = {...}
setfenv(1, math)
local run = _G.loadstring(_G.table.concat(tInput), " "))
_G.print(run())

Somebody else should probably be answering that as being unable to debug makes writing code difficult.
Daegrath() #14
Posted 11 December 2012 - 04:48 PM
debug?
Bubba #15
Posted 11 December 2012 - 04:49 PM
debug?

Yeah. Check the code to see if it runs, and if not then fixing it by using the errors as a guide.
Daegrath() #16
Posted 11 December 2012 - 04:51 PM
ok
PixelToast #17
Posted 11 December 2012 - 05:28 PM
yea, i dont get whats wrong with loadstring .-.

nvm!
i got it :3
i forgot to concat return to it *facepalm*

working code:

print(({pcall(setfenv(loadstring("return "..table.concat({ ... }," ")),math))})[2])
http://puu.sh/1zEfA
<3 one liners