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

Calcplus: A Simple Calculator | V1.1 Released

Started by DiabolusNeil, 25 October 2013 - 06:48 PM
DiabolusNeil #1
Posted 25 October 2013 - 08:48 PM
Hello everybody! I was just messing around with the concept of a calculator, and made what I dubbed CalcPlus. Now, before you say that this is spam and that anyone can make a calculator in 5 minutes, hear me out. CalcPlus, as said by the title, is intelligent. How so? CalcPlus is able to detect non-Lua errors and output to the user what the error is. It can do multiple functions: addition, subtraction, multiplication, division, and work with exponents. It can also run as many times as you deem necessary.

I know this is a simple program for you advanced programmers out there, but some people prefer simple.


Pastebin Code
Lite: hiLxaPNp

http://pastebin.com/hiLxaPNp
Color: 0LNMBhzX
http://pastebin.com/0LNMBhzX

Changelog
v1.1
- [NEW] Color scheme for Advanced Computers, doesn't run if activated on normal Computer
- [NEW] Displays answer from last calculation and can be recalled quickly with "ans"

- [NEW] The non-colored version is now called "CalcPlus Lite", colored version unofficially dubbed "Color"
- [NEW] Cannot terminate the program
- [TWEAK] Slightly changed interface
- [TWEAK] Replaced "No function given" with "Missing information"
- [TWEAK] The "error" variable is now called "fault" (Pastebin displays "error" as a function)
- [TWEAK] Now says "ENTER = Yes | Anything Else = No"
- [TWEAK] Sleep time between the output of the calculation and the prompt is now 0.5 seconds instead of 1.

v1.0
- [RELEASE] Initial release

Screenshots

SpoilerThe general user interface for Lite


A calculation demonstration using Lite


Demonstrating the recalling of a previous answer in Lite


The general user interface using Color


Demonstration using Color

Raw Code:
SpoilerCalcPlus v1.1 Lite
Spoiler

-- CalcPlus v1.1 Lite | Last Updated 10/26/2013
-- Minimal Requirement: Normal Computer
-- Written by DiabolusNeil

os.pullEvent = os.pullEventRaw

local num1, num2, func, answer, fault, run
local empty = ""
local version = "v1.1"

local function main()
  term.clear()
  term.setCursorPos(17,1)
  print("CalcPlus "..version.." Lite")
  term.setCursorPos(18,2)
  print("By DiabolusNeil\n")
  print("First number:")
  print("Function:")
  print("Second number:")
  if answer ~= nil then
	term.setCursorPos(1,18)
	print("Last Answer: "..answer)
  end
  term.setCursorPos(15,4)
  num1 = read()
  term.setCursorPos(11,5)
  func = read()
  term.setCursorPos(16,6)
  num2 = read()
  print("")

  if num1 == "ans" then num1 = answer end
  if num2 == "ans" then num2 = answer end

  if func == "+" then
	answer = num1 + num2
  elseif func == "-" then
	answer = num1 - num2
  elseif func == "*" then
	answer = num1 * num2
  elseif func == "/" then
	answer = num1 / num2
  elseif func == "^" then
	answer = num1 ^ num2
  elseif func == empty or num1 == empty or num2 == empty then
	fault = "Missing information"
  elseif type(num1) or type(num2) == string then
	fault = "Invalid given information"
  else
	fault = "Invalid given information"
  end

  if fault == nil then
	print(num1.." "..func.." "..num2.." = "..answer)
  else
	print("ERROR: "..fault)
  end

  print("")
  sleep(0.5)
  print("Do you want to make a new calculation?")
  print("ENTER = Yes | Anything Else = No")
  event, key = os.pullEvent("key")

  if key == 28 then
	run = true
  else
	run = false
  end
end

-- Actual program
run = true
while run do
  fault = nil
  main()
end
term.clear()
term.setCursorPos(1,1)
CalcPlus v1.1 Color
Spoiler

-- CalcPlus v1.1 | Last Updated 10/26/2013
-- Minimal Requirement: Advanced Computer
-- Written by DiabolusNeil

os.pullEvent = os.pullEventRaw

local num1, num2, func, answer, fault, run
local empty = ""
local version = "v1.1"

local function main()
  term.clear()
  term.setTextColor(colors.red)
  term.setCursorPos(19,1)
  print("CalcPlus "..version)
  term.setCursorPos(18,2)
  print("By DiabolusNeil\n")
  term.setTextColor(colors.green)
  print("First number:")
  print("Function:")
  print("Second number:")
  if answer ~= nil then
	term.setCursorPos(1,18)
	term.setTextColor(colors.red)
	print("Last Answer: "..answer)
  end
  term.setTextColor(colors.yellow)
  term.setCursorPos(15,4)
  num1 = read()
  term.setCursorPos(11,5)
  func = read()
  term.setCursorPos(16,6)
  num2 = read()
  print("")

  if num1 == "ans" then num1 = answer end
  if num2 == "ans" then num2 = answer end

  if func == "+" then
	answer = num1 + num2
  elseif func == "-" then
	answer = num1 - num2
  elseif func == "*" then
	answer = num1 * num2
  elseif func == "/" then
	answer = num1 / num2
  elseif func == "^" then
	answer = num1 ^ num2
  elseif func == empty or num1 == empty or num2 == empty then
	fault = "Missing information"
  elseif type(num1) or type(num2) == string then
	fault = "Invalid given information"
  else
	fault = "Invalid given information"
  end

  if fault == nil then
	term.setTextColor(colors.orange)
	print(num1.." "..func.." "..num2.." = "..answer)
  else
	term.setTextColor(colors.red)
	print("ERROR: "..fault)
  end

  print("")
  sleep(0.5)
  term.setTextColor(colors.green)
  print("Do you want to make a new calculation?")
  print("ENTER = Yes | Anything Else = No")
  event, key = os.pullEvent("key")

  if key == 28 then
	run = true
  else
	run = false
  end
end

-- Actual program
if not term.isColor() then
  print("\nRequires an Advanced Computer\n")
else
  term.setBackgroundColor(colors.blue)
  run = true
  while run do
	fault = nil
	main()
  end
  term.setCursorPos(1,1)
  term.setBackgroundColor(colors.black)
  term.setTextColor(colors.white)
  term.clear()
end

Tell me what you think!
Engineer #2
Posted 25 October 2013 - 09:11 PM
First about the title: 'intelligent calculator'
It is very raw to throw it out there, because I consider intelligent on another way. I see AI, Artificial Intelligence, about the same as intelligent, logically. I don't expect you to write a complete AI, but somewhere in that direction.
As of now it is 'just a bunch of elseifs checking for something'.
In this particular case I would say you can write out an algorithm to see possible errors. That is what I see as smart. And that was I expecting when I read the title.

Please, don't feel attacked here, I'm just giving my opinion on the intelligent part.

Now for the actual program:
When taking a look at the screenshots it does what it should. Maybe it would be nice to write a different GUI for advanced computers, maybe with a numpad of sorts.

However I kinda disagree with the name too, because with 'CalcPlus' I was expecting something plus a calculator :P/>
Maybe adding some features like longer calculations and more 'functions' on numbers you can do. The program has potential if you learn Lua enough :P/>

Enough the downtalk for me, something positive. As you are probably a beginner, you did as good job. We can do functions between two numbers, and that was intended.
So, for the program alone, you did a great job :)/>
I'm not just an evil person, see? :P/>
Symmetryc #3
Posted 26 October 2013 - 11:35 AM
I agree with Engineer, while this is impressive for a new programmer, the title saying that it's "Plus" and "Intelligent" is highly misleading.
MudkipTheEpic #4
Posted 26 October 2013 - 11:37 AM
Tip: Rename error to something else. There is a built in function named error which you are overwriting.
Zudo #5
Posted 26 October 2013 - 11:42 AM
error = nil

Why do you even need to do this?
DiabolusNeil #6
Posted 26 October 2013 - 09:22 PM
To Engineer: Thanks for the good criticism. Now addressing my other thoughts to your comment: It's ComputerCraft, how more advanced can you get with a calculator? Also, I am planning to add a colored GUI for Advanced Computers, and other possible features.

To MudkipTheEpic: First off, hi! I remember you from MudServ. Anyway, I am using "error" as a variable, not as it's intended use. You're probably referencing to Pastebin highlighting all the "error"s in the code, thinking that I'm using it as it's intended purpose. I'll tweak that.

To ZudoHackz: I need to set "error" equal to "nil" every time the code runs. Take this scenario: The user gets an error on an attempt to use the program, and they try to use it again. If I don't redeclare "error" equal to nil, "error" will stay equal to what it was assigned when the program detected an error, and never output the proper calculation, as the program checks each time if "error" is equal to "nil".

Thanks for the good constructive criticism. I'll modify the program and add more features.
MudkipTheEpic #7
Posted 26 October 2013 - 09:25 PM
-snip-

To MudkipTheEpic: I am using "error" as a variable, not as it's intended use. You're probably referencing to Pastebin highlighting all the "error"s in the code, thinking that I'm using it as it's intended purpose. I'll tweak that.

Thanks for the good constructive criticism. I'll modify the program and add more features.

When a program tries to use the function error once running your program, it will crash.
DiabolusNeil #8
Posted 27 October 2013 - 09:13 AM
When a program tries to use the function error once running your program, it will crash.
I don't know what you're talking about. It worked prefectly for me. Anyway, I changed it to "fault" now.
theoriginalbit #9
Posted 27 October 2013 - 09:21 AM
I don't know what you're talking about. It worked prefectly for me. Anyway, I changed it to "fault" now.
What he means is there is a function in the global table called error, this is used by many other programs, apis, and CraftOS, I'm going to go ahead and assume (based off the fact that he stated this) that you also don't localise your variables meaning that your error variable would remain well after your program has finished, meaning that if another program attempts to invoke the error function, it will not be there, as it will be your variable, whatever you may have set it to. Funnily enough, theoretically it would actually crash the computer, since if error, error'd (i.e. someone tried to call error when its a boolean variable) there'd be no way for the computer to error telling you "attempt to call ? (a boolean value)" since the error function is overridden.
MKlegoman357 #10
Posted 27 October 2013 - 09:54 AM
Now addressing my other thoughts to your comment: It's ComputerCraft, how more advanced can you get with a calculator?

It could be as advanced as real-life calculators. How more advanced it can get? For basic calculations like this:

(2 / 3) * 5 + 20 * (15 / 3)

It would be nice if I could write all of this, press Enter and get the answer.
theoriginalbit #11
Posted 27 October 2013 - 10:38 AM
Now addressing my other thoughts to your comment: It's ComputerCraft, how more advanced can you get with a calculator?
(2 / 3) * 5 + 20 * (15 / 3)
Also add in things like xy, logx(y), sqrt, !x, and several other advanced mathematical operators.
Symmetryc #12
Posted 27 October 2013 - 02:04 PM
This is how much more advanced you can get (and probably a quite a bit further):
Spoiler
Here's the program's topic: http://www.computerc...ced-calculator/

Edit: Also, why are you overwriting os.pullEvent? Not only should this program be terminatable, but it should also restore the function…
DiabolusNeil #13
Posted 27 October 2013 - 02:26 PM
Like I said in the program's description, this is supposed to be a simple calculator. I also changed the title a bit to avoid any failed expectations.