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

CC Calc Simple Calculator

Started by Kouksi44, 01 August 2015 - 06:55 PM
Kouksi44 #1
Posted 01 August 2015 - 08:55 PM
CC Calc

Hello everybody ,

this is my new program I want to show you :)/>

What is it ?

As you probably could already guess it is a … calculator ! Yep that´s it :D/>

How does it work ?

The special thing about this calculator is, that it is able to handle anything you throw at it as long as it consists of any of the standart operators + , - , * , / , ^ !!

Brackets are possible aswell !

Basically what my program does is :

It turns the input math expression into a Queue of tokens ( numbers,brackets and operators ) then turns those into a Queue of tokens in reverse polish notation( thanks to Wikipedia and their pseudocode :D/> ) and then evaluates this Expression !

Internally I am using a class api to create classes for Queues and Stacks which are needed in the process of turning the infix math expression into a RPN expression.


Currently the GUI of this program is … well not existant :D/>

After finishing the crucial parts of the program I was too lazy to create a nice GUI but I will probably look into that in the near future.

So currently the program will just ask you to enter your math expression you want to solve and will then print the ( hopefuly correct ) result

To install simply run:

 pastebin run ebWSYVTq 

It will create the following files :

Class.lua -> The previously mentoined class api
Classes.lua -> The file containing the Stack class and the Queue class
Calculator -> The actual program you can run afterwards !

Screenshot:
Spoilerhttp://imgur.com/ODDrss4
TODO:
SpoilerGUI

I hope you like it !

Kouksi44
Edited on 01 August 2015 - 10:01 PM
Lignum #2
Posted 01 August 2015 - 09:06 PM
Neat! This is the first calculator I've seen that allows you to enter expressions.

Though it does have some bugs:
8 * 3 + 2 will return 40, when it should be 26. However, 2 + 8 * 3 works perfectly fine.

If you allowed the expression to be entered as a program argument, this could make quite a handy command line tool.
Kouksi44 #3
Posted 01 August 2015 - 09:10 PM
Yup I just ran over that bug aswell :D/> will try to fix it :)/>

EDIT: BUG FIXED :D/> had some parentheses missing in a condition ! -> 8*3+2 is now resulting in : 26 ! :)/>
Edited on 01 August 2015 - 07:44 PM
Kouksi44 #4
Posted 01 August 2015 - 11:29 PM
Update: I implemented the command line support so : calculator "3*5/2" will work now :)/>
I also added support for exponents so 3*2^3 is now a valid expression and will return 24 !

EDIT: Added the modulo operator % aswell ! :)/>
Edited on 01 August 2015 - 09:59 PM
Lignum #5
Posted 01 August 2015 - 11:48 PM
I'm glad to see that it's fixed now. Though, I've noticed that you can't use spaces in the command line. As you probably know, this is because you're only taking the first argument.

You can use table.concat to fix this:

local expression = table.concat({ ... }, " ")
Kouksi44 #6
Posted 02 August 2015 - 12:14 AM
Thanks Lignum and once again fixed !
Kouksi44 #7
Posted 02 August 2015 - 01:56 PM
New Update : I added support for some functions now !

Functions : abs(), cos() , log(), logt() ( same as log10() but I´ve had some trouble implementing log10 as a function that my parser could recognise) sin(), tan(), sqrt() and random() can now be added to expressions so : 3*5+sqrt(23+abs(3-12)) is now perfectly valid and will return 20,656… !

I also added support for decimal numbers so 3.5+2 will now result in 5.5 !
Edited on 02 August 2015 - 11:58 AM
LeDark Lua #8
Posted 02 August 2015 - 05:21 PM
Kouksi, i have a sugestion for you: loadstring("return "..value)

and the value could be: value="((2 * 4)*5)/2" and you could do a little parser or you could just write math.cos(15) becouse it will load that string.


local __vals=read()
local __ret=loadstring("return "..__vals)() --This will run this as a function.
print(__ret)
but this isnt safe. You would need a parser to restrict like Lua code.
Edited on 02 August 2015 - 03:28 PM
Kouksi44 #9
Posted 02 August 2015 - 06:49 PM
That is definitely a possible way :D/> but letting the lua interpreter do all the math would be boring . I was mainly trying to parse the expression on my own rather than using already existing solutions :)/>
LeDark Lua #10
Posted 02 August 2015 - 07:27 PM
I would help you with the parser. We could check if its a numbers then parse it and return the value else ignore it. Simple as that :)/>
Edited on 02 August 2015 - 05:29 PM
Lignum #11
Posted 02 August 2015 - 10:59 PM
But what would be the benefit? Using loadstring is a rather dirty solution. It may work, but it's not only slow because it has to compile the code, but it's also insecure. While you could, as you mentioned, check the source for malicious code, there is still the risk of your code failing. And if you're already going through the effort of doing that, you might as well just write a parser yourself…
Konlab #12
Posted 13 August 2015 - 11:47 AM
sin seems to be broken
sin 0 is 0
but sin 360 or sin 720 returns completly wrong numbers
Exerro #13
Posted 13 August 2015 - 12:11 PM
I'm assuming sine in this uses math.sin() Lua-side, in which case you should be using radians, not degrees. Try sin( pi ) and you should get 0, sin( pi / 2 ) and you should get 1, sin( 0 ) should be 0, and sin( pi * 3 / 2 ) should be -1.
Konlab #14
Posted 13 August 2015 - 01:44 PM
I'm assuming sine in this uses math.sin() Lua-side, in which case you should be using radians, not degrees. Try sin( pi ) and you should get 0, sin( pi / 2 ) and you should get 1, sin( 0 ) should be 0, and sin( pi * 3 / 2 ) should be -1.
I wanted to try
No pi support :P/>/>/>
(IK I can use this site:
Pi
)

EDIT: YOU WERE RIGHT!
It has input in radians
EDIT: sorry Lyqyd
Edited on 01 September 2015 - 09:17 AM
HPWebcamAble #15
Posted 13 August 2015 - 09:28 PM
Nice program, but way over-complicated, for CC anyway.

You can have Lua evaluate math expressions!

local expression = "10*10+1/2"

print( loadstring("return "..expression)() )
Exerro #16
Posted 13 August 2015 - 09:47 PM
Nice program, but way over-complicated, for CC anyway.

You can have Lua evaluate math expressions!

local expression = "10*10+1/2"

print( loadstring("return "..expression)() )

That is definitely a possible way :D/> but letting the lua interpreter do all the math would be boring . I was mainly trying to parse the expression on my own rather than using already existing solutions :)/>

In addition, I wrote the math parser in Nova's prerelease's Calculator app myself because it allowed extended functionality, such as being able to do '3x' iirc, not to mention the fact that it really helped me write various other parsers for actual languages.

If it were a part of another program, I'd agree that loadstring is a better solution, but as this is the main project it's more fun to make it not a ~10 line program and try something new.
HPWebcamAble #17
Posted 14 August 2015 - 01:14 AM
That is definitely a possible way :D/> but letting the lua interpreter do all the math would be boring . I was mainly trying to parse the expression on my own rather than using already existing solutions :)/>

Oops.. thats what I get for only skimming the previous posts :P/>

If it were a part of another program, I'd agree that loadstring is a better solution, but as this is the main project it's more fun to make it not a ~10 line program and try something new.

Very true. This way you can implement additional things.