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

[Bugs and derps!]EnderCalc v2.0

Started by SuicidalSTDz, 04 February 2013 - 04:19 PM
SuicidalSTDz #1
Posted 04 February 2013 - 05:19 PM
Coded by SuicidalSTDz:

EnderCalc is a calculator that I coded in about 20 minutes that can do basic functions such as Addition, Subtraction, Mulitplication, and Division. I do plan on adding more functions such as Square Roots, Squaring, and much more. If you plan on using EnderCalc in any program/OS you MUST give credit to the original author "SuicidalSTDz" and state that you own nothing!(Sounds harsh but, it's true) You may edit EnderCalc for personal use but are not permitted to redistribute any modified code. I hope you all enjoy this neat little program!

This calculator is used in my OS: EnderOS


Changelog:
v2.0:
-Re-written from the ground up(LIke everything else I do ;)/>)
-More functions!
-Recursion loop solved(At least I think it is for now :P/>)

v1.1:
-Changed the layout
-Added a random number generator

v1.0:
-Added Addition, Subtraction, Multiplication, and Division
-The beginning!


If you find any bugs, please state them below in the comments and tell me how I did for my first calculator!(more to come obviously)


Mikee251 #2
Posted 04 February 2013 - 05:42 PM
You should add an Answer key, it stores the last given result, so you can do stuff like Answer * 2 . All it really needs is a variable that tracks it
SuicidalSTDz #3
Posted 04 February 2013 - 05:44 PM
You should add an Answer key, it stores the last given result, so you can do stuff like Answer * 2 . All it really needs is a variable that tracks it
Very possible, I will add that in tomorrow since i'm too tired from bug hunting :P/>.
SuicidalSTDz #4
Posted 05 February 2013 - 02:43 PM
Pythagorean Theorem implementation is done! Now working on Squares, Square roots and more!
Dlcruz129 #5
Posted 05 February 2013 - 04:02 PM
Pythagorean Theorem implementation is done! Now working on Squares, Square roots and more!

Squares/square roots are easy. Just do num^2 for squaring and math.sqrt(num) for square roots. May I suggest a y^x button, like on scientific calculators?
Zoinky #6
Posted 05 February 2013 - 07:00 PM
I took a look at the code and noticed that you used recursion to restart your program, keep in mind that after many calculations the calculator will error. You should try use loops instead of recursions to fix this. Although, it's likely nobody will make over 200 calculations it's still probably something that would be good to fix. Anyway, nice program.
SuicidalSTDz #7
Posted 06 February 2013 - 09:38 AM
I took a look at the code and noticed that you used recursion to restart your program, keep in mind that after many calculations the calculator will error. You should try use loops instead of recursions to fix this. Although, it's likely nobody will make over 200 calculations it's still probably something that would be good to fix. Anyway, nice program.
I do plan on using loops in the future, I just wanted to get all the code out of my head :)/>
SuicidalSTDz #8
Posted 06 February 2013 - 09:39 AM
Pythagorean Theorem implementation is done! Now working on Squares, Square roots and more!

Squares/square roots are easy. Just do num^2 for squaring and math.sqrt(num) for square roots. May I suggest a y^x button, like on scientific calculators?
I already have squares and roots done. I will look into making a y^x function :)/>
SuicidalSTDz #9
Posted 06 February 2013 - 03:06 PM
I took a look at the code and noticed that you used recursion to restart your program, keep in mind that after many calculations the calculator will error. You should try use loops instead of recursions to fix this. Although, it's likely nobody will make over 200 calculations it's still probably something that would be good to fix. Anyway, nice program.
Current recursion loop solution:
function reset()
term.clear()
term.setCursorPos(1,1)
end
running = shell.getRunningProgram()
reset()
term.setTextColor(colors.orange)
print("What would you like to do?")
term.setCursorPos(1,3)
term.setTextColor(colors.lime)
print("{Add}")
local event, button, xPos, yPos = os.pullEvent("mouse_click")
if button == 1 then
if (xPos >=1 and xPos <=5 and yPos == 3) then
  repeat --Check first number validity
   reset()
   term.setTextColor(colors.orange)
   print("What is your first number?")
   term.setTextColor(colors.white)
   firstNum = read()
   valid = tonumber(firstNum)
   validNum = type(valid)
  until validNum == "number" and #firstNum <= 4
  repeat --Check second number validity
   reset()
   term.setTextColor(colors.orange)
   print("What is your second number?")
   term.setTextColor(colors.white)
   secondNum = read()
   valid = tonumber(secondNum)
   validNum = type(valid)
  until validNum == "number" and #secondNum <= 4
  term.setTextColor(colors.lightBlue)
  sum = (firstNum+secondNum)
  reset()
  print("The sum of "..firstNum.." and "..secondNum.." is "..sum)
  term.setCursorPos(1,3)
  print("{Clear}")
  repeat --Repeat until mouse click == 1 and mouse click is on said position
   local event, button, xPos, yPos = os.pullEvent("mouse_click")
  until button == 1 and xPos >=1 and xPos <=7 and yPos == 3
  shell.run(running)
else
  shell.run(running)
end
else
shell.run(running)
end
Lyqyd #10
Posted 06 February 2013 - 03:15 PM
That doesn't fix the recursion issue.
SuicidalSTDz #11
Posted 06 February 2013 - 03:18 PM
That doesn't fix the recursion issue.
Oh, whoops. One sec. :)/>

How about this?

function reset()
term.clear()
term.setCursorPos(1,1)
end
x = 2
while x == 2 do
running = shell.getRunningProgram()
reset()
term.setTextColor(colors.orange)
print("What would you like to do?")
term.setCursorPos(1,3)
term.setTextColor(colors.lime)
print("{Add} {Subtract}")
local event, button, xPos, yPos = os.pullEvent("mouse_click")
if button == 1 then
  if (xPos >=1 and xPos <=5 and yPos == 3) then --ADDITION
   repeat --Check first number validity and length
	reset()
	term.setTextColor(colors.orange)
	print("What is your first number?")
	term.setTextColor(colors.white)
	firstNum = read()
	valid = tonumber(firstNum)
	validNum = type(valid)
   until validNum == "number" and #firstNum <= 6
   repeat --Check second number validity and length
	reset()
	term.setTextColor(colors.orange)
	print("What is your second number?")
	term.setTextColor(colors.white)
	secondNum = read()
	valid = tonumber(secondNum)
	validNum = type(valid)
   until validNum == "number" and #secondNum <= 6
   term.setTextColor(colors.lightBlue)
   sum = (firstNum+secondNum)
   reset()
   print("The sum of "..firstNum.." and "..secondNum.." is "..sum)
   term.setCursorPos(1,3)
   term.setTextColor(colors.lime)
   print("{Clear}")
   repeat --Repeat until mouse click == 1 and mouse click is on said position
	local event, button, xPos, yPos = os.pullEvent("mouse_click")
   until button == 1 and xPos >=1 and xPos <=7 and yPos == 3
   x = 2
  elseif (xPos >=7 and xPos <=16 and yPos == 3) then --SUBTRACT
   repeat --Check second number validity and length
	reset()
	term.setTextColor(colors.orange)
	print("What is your first number?")
	term.setTextColor(colors.white)
	firstNum = read()
	valid = tonumber(firstNum)
	validNum = type(valid)
   until validNum == "number" and #firstNum <= 6
   repeat --Check second number validity and length
	reset()
	term.setTextColor(colors.orange)
	print("What is your second number?")
	term.setTextColor(colors.white)
	secondNum = read()
	valid = tonumber(secondNum)
	validNum = type(valid)
   until validNum == "number" and #secondNum <= 6
   term.setTextColor(colors.lightBlue)
   diff = (firstNum-secondNum)
   reset()
   print("The difference between "..firstNum.." and "..secondNum.." is "..diff)
   term.setCursorPos(1,3)
   term.setTextColor(colors.lime)
   print("{Clear}")
   repeat --Repeat until mouse click == 1 and mouse click is on said position
	local event, button, xPos, yPos = os.pullEvent("mouse_click")
   until button == 1 and xPos >=1 and xPos <=7 and yPos == 3
  else
   x = 2
  end
else
  x = 2
end
end

I haven't seen any problems(huge) with this code at all yet. Not sure if x = 2 at the end is needed or not. (Kinda tired ;)/> )
Theiket #12
Posted 06 February 2013 - 04:07 PM
Looks cool. Might need you to teach me a bit. XD Keep it up dude.
SuicidalSTDz #13
Posted 06 February 2013 - 04:17 PM
Looks cool. Might need you to teach me a bit. XD Keep it up dude.
Lol, thanks. I am quite new to the math library in Lua but it seems pretty self-explanatory for the most part.
SuicidalSTDz #14
Posted 06 February 2013 - 04:30 PM
A sneak peak of EnderCalc v1.2: A complete re-write!

[attachment=991:Capture.PNG]

Lightshow video!

Pastebin Link

I will be adding everything back in that was previously in EnderCalc, I just have to fix a few things first. I hope you all enjoy!
shiphorns #15
Posted 07 February 2013 - 10:32 AM
You're going to confuse readers of your code with the x==2 business, because they're going to expect that x is actually used for something in the calculation, and that '2' has some significance. But in your code, it's an arbitrary value. Normally, when you want a loop to run at least once (as in your code), and continue until some condition is met, you would format it like this:


done = false
repeat
 if (some condition that should cause looping to end) then
  done = true
 end
until done

Another common pattern for the same exact execution (without need of a boolean flag) would be this:

while true do
 if (some condition that should cause looping to end) then
  break
 end
end

Which type of construct you go with would depend on what code inside the loop executes before and after the check. Sometimes you'll want to flip a flag to signal the last iteration of the loop, but keep executing code in the body. Other times, you'll want to break out of the loop as soon as a condition is met. Yet another option to consider, functionally equivalent to the repeat loop:


done = false
while not done do
 if (some condition that should cause looping to end) then
  done = true
 end
end

Any of these is more easily understood than an arbitrary value comparison.
SuicidalSTDz #16
Posted 07 February 2013 - 10:37 AM
You're going to confuse readers of your code with the x==2 business, because they're going to expect that x is actually used for something in the calculation, and that '2' has some significance. But in your code, it's an arbitrary value. Normally, when you want a loop to running at least once (as in your code), and continue until some condition is met, you would format it like this:


done = false
repeat
if <some condition that should cause looping to end> then
  done = true
end
until done
Little do you know, I already have gotten rid of this code(It was a placeholder) ;)/>
SuicidalSTDz #17
Posted 13 February 2013 - 02:21 PM
EnderCalc is now updated to 2.0! I will be adding the last answer back in tomorrow(Too tired to do any more) EnderCalc has new features so go check it out! I will be adding in Distance and Midpoint functions and many more in the future! Enjoy and leave a comment on how I did and what I should add/do differently. :)/> There are also probably a few bugs since I am pretty tired.

EDIT: Just found a derp in the y^x function, I reversed the order [Head Bang] It is x^y in the calculator but should be y^x. I will fix this tomorrow :)/>

EDIT: THE BACK BUTTON DOES NOT WORK!! I had it in so I wouldn't forget to add it in to the calculator in my OS… They are different but only by how they exit. Fix it tomorrow :)/>
Azhf #18
Posted 07 March 2013 - 06:53 PM
Hope you don't mind, but before I found this, I made a floppy-OS thing-a-ma-jig. Would you mind if I advanced it a bit and posted it? Still will not be nearly as good as this ^_^/> Also, if I need some help, could you help me? Thanks.
SuicidalSTDz #19
Posted 07 March 2013 - 06:59 PM
I have no problem with that. The answer to your second question is, yes. I would love to help you with anything. On a side note, EnderCalc will not be updated until the release of EnderOS for certain raisins (See if there are any Fallout New Vegas: Old World Blue fans in here ;)/>)
Azhf #20
Posted 07 March 2013 - 07:03 PM
I have no problem with that. The answer to your second question is, yes. I would love to help you with anything. On a side note, EnderCalc will not be updated until the release of EnderOS for certain raisins (See if there are any Fallout New Vegas: Old World Blue fans in here ;)/>)
Thanks man :D/> Hopefully this weekend or sometime soon we could get on Skype or something? I will tell you my Skype later on, rather than now. Mainly because I don't have Skype on this computer, but I do on the one that I can play only during weekends. Confusing? XD :P/> btw I don't feel like installing Skype on here ^_^/>
SuicidalSTDz #21
Posted 07 March 2013 - 07:08 PM
I have no problem with that. The answer to your second question is, yes. I would love to help you with anything. On a side note, EnderCalc will not be updated until the release of EnderOS for certain raisins (See if there are any Fallout New Vegas: Old World Blue fans in here ;)/>)
Thanks man :D/> Hopefully this weekend or sometime soon we could get on Skype or something? I will tell you my Skype later on, rather than now. Mainly because I don't have Skype on this computer, but I do on the one that I can play only during weekends. Confusing? XD :P/> btw I don't feel like installing Skype on here ^_^/>
No problem. Also, I can get on Skype whenever and literally wherever so any time is good for me. I have Skype on all my devices so… I can do as I please :P/>
Azhf #22
Posted 07 March 2013 - 07:11 PM
I have no problem with that. The answer to your second question is, yes. I would love to help you with anything. On a side note, EnderCalc will not be updated until the release of EnderOS for certain raisins (See if there are any Fallout New Vegas: Old World Blue fans in here ;)/>)
Thanks man :D/> Hopefully this weekend or sometime soon we could get on Skype or something? I will tell you my Skype later on, rather than now. Mainly because I don't have Skype on this computer, but I do on the one that I can play only during weekends. Confusing? XD :P/> btw I don't feel like installing Skype on here ^_^/>
No problem. Also, I can get on Skype whenever and literally wherever so any time is good for me. I have Skype on all my devices so… I can do as I please :P/>
lol ok. Thanks. Looking forward to it. BTW I am not a master coder so my OS will probably be EXTREMELY basic. XD btw(again) I have dropbox so I may be able to give you the link, you could download it, and look at it when I am done? Also, how do you rank up from clueless and such? I think that is why I currently cannot post on programs. :P/>
SuicidalSTDz #23
Posted 08 March 2013 - 10:48 AM
lol ok. Thanks. Looking forward to it. BTW I am not a master coder so my OS will probably be EXTREMELY basic. XD btw(again) I have dropbox so I may be able to give you the link, you could download it, and look at it when I am done? Also, how do you rank up from clueless and such? I think that is why I currently cannot post on programs. :P/>
I'll be glad to look at it ;)/> You rank up according to your post count. Also, it just so happens you reached the amount of posts you need to make before making a new topic!

About EnderCalc:
I am sad to say, EnderCalc will no longer be maintained by me. I have way too much going on right now so I am dropping this project.

Are we still allowed to use EnderCalc?
Yes, as of now, EnderCalc is in the Public Domain meaning ANYONE has rights to use/distribute/modify/perform/copy/etc any or all code of EnderCalc.

Sincerely,
SuicidalSTDz