diff --git a/LICENSE b/LICENSE index 45afc86..c3d2307 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ -The MIT License (MIT) +MIT License -Copyright (c) 2015 Vivek Kannan +Copyright (c) 2019 Mong Alvarez Jr. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -19,4 +19,3 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - diff --git a/LICENSE_2015 b/LICENSE_2015 new file mode 100644 index 0000000..ef3a5b0 --- /dev/null +++ b/LICENSE_2015 @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2015 Vivek Kannan + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md index e528e2a..3053f45 100644 --- a/README.md +++ b/README.md @@ -1,47 +1,96 @@ -#calc +# Calculator CalC +This is a simple console app calculator +that supports most of the functions that one can +find in a scientific calculator. - calc is a simple command-line based calculator that supports most of the functions that one can find in a scientific calculator. +![console](resources/console.png) -##Usage +*** +Furthermore, to demonstrate creating GUI in +another language that will communicate +with this program, Mong Alvarez Jr. decided +to use PureBasic for the GUI. But there is so much to +be done yet. Mong Alvarez Jr. does not have +enough time to finish this (as always). +This is just the initial effort. - Compile with, +![GUI](resources/GUI.png) + +*** +In component-based approach, software developers +need only to know the behavior of a component +and how they would interact with that component. +One good example is Java Bean. + +But here, the only interface is the command line argument +and that is enough so that PureBasic can +interact with this program. But C and PureBasic +were not meant for component-based build, yet +in situations like this, software +developers can agree on how each component should +communicate with one another. + +## Compile and Create Executables +Compile with, - gcc -std=c99 -o calc calc.c helpers.c -lm + gcc -std=c99 -o calc calc.c helpers.c -lm - Run with, +Run with, - ./calc [OPTIONS] ... + ./calc [OPTIONS] ... + +or in Windows OS, + + calc.exe [OPTIONS] ... + +Obviously, the expressions are expected to be quoted +within single quotation marks to prevent bash/shell from parsing them. - Obviously, the expressions are expected to be quoted within single quotes to prevent bash/shell from parsing them. +In Windows OS, you can just type the arguments without space/s +to be interpreted as single expression or can be inside +double quotation marks. -##Options +### PureBasic Compile and Create Executable +As usual, in PureBasic, it is very easy to compile +the `calc.pb` using the PureBasic IDE. - The following flags can be used, +The compiled file of `calc.pb` used an icon that is +included here. This is the favorite icon of Mong +Alvarez Jr. that he created +and is the icon being used for his apps: - 1. -d, consider all angles to be degrees. Results are also in degrees. - 2. -n, print results of each epressions in a new line instead of separating them by ';'. - 3. -h (or) --help, print help and exit. +![icon](resources/icon.ico) -##Expressions - calc supports multiple expressions in a single call. For example, +## Options +The following flags can be used: + +1. `-d`, consider all angles to be degrees. Results are also in degrees. +2. `-n`, print results of each expressions in a new +line instead of separating them by `;`. +3. `-h` (or) `--help`, print help and exit. + +## Expressions +CalC supports multiple expressions in a single call. + +For example, ./calc '3.4' 'atan(1/0) - 3.14159265358979323846 * sqrt 3' - The result of each expression is printed either in the same line separated by semi-colons or in separate lines if the -n flag is used. +The result of each expression is printed either in the same line +separated by semicolons or in separate lines if the `-n` flag is used. - For example, the output of the above is +For example, the output of the above is: 3.4;-3.870601766 - With -n flag, +with `-n` flag, 3.4 -3.870601766 -##Features - -###Operators -The following operators are supported, +## Features +### Operators +The following operators are supported: + --> addition - --> subtraction @@ -51,9 +100,14 @@ The following operators are supported, ! --> factorial ^ --> exponent -Unary '+' and '-' are also supported. '~' and '_' operators act as aliases for unary '-' and '+' respectively. +Unary `+` and `-` are also supported. `~` and `_` +operators act as +aliases for unary `-` and `+`, respectively. -The '$' (dollar) operator can be used to refer to the result of a particular sub-expression. Sub-expression are created using the ',' (comma) operator. For example, +The `$` (dollar) operator can be used to refer +to the result of a particular +sub-expression. Sub-expression are created +using the `,` (comma) operator. For example, ./calc '7 - 5, sin$1, log($1 - -$2)' @@ -63,31 +117,35 @@ The expression has three sub-expression. $2 = sin$1 = sin(7 - 5) $3 = log($1 - -$2) = log(7 - 5 - -sin(7 - 5)) -The results of all three expressions is printed in comma separated format. +The results of all three expressions is printed +in comma-separated format. 2,0.9092974268,0.4637881228 -###Functions - - The following function are supported, +### Functions +The following functions are supported: + +``` +sin, cos, tan, asin, acos, atan, sinh, cosh, tanh, asinh, acosh, atanh, log (base 10), ln (base e), exp (e raised), floor, ceil, round, sqrt, abs, sgn +``` + +`sgn` returns 1 if positive, -1 if negative else 0. - sin, cos, tan, asin, acos, atan, sinh, cosh, tanh, asinh, acosh, atanh, log (base 10), ln (base e), exp (e raised), floor, ceil, round, sqrt, abs, sgn. - - sgn returns 1 if positive, -1 if negative else 0. - - Trignometric function may have infinite precision related issues. For example, +Trignometric function may have infinite precision related issues. + +For example, ./calc 'sin pi' 'cos pi' - Results in, +results in, 7.338823075E-05;-0.9999999973 - The actual values being 0 and -1. pi here is a symbolic constant. +The actual values being 0 and -1. `pi` here +is a symbolic constant. -###Symbolic Constants - - The following symbolic constants are supported, +### Symbolic Constants +The following symbolic constants are supported: pi --> 3.14159265358979323846 e --> 2.7182818284590452354 @@ -95,14 +153,36 @@ The results of all three expressions is printed in comma separated format. inf --> infinity rand --> random double in the range (0, 1). (ironic, i know...) - For example, +For example, ./calc 'atan inf' 'rand, sin $1, cos($2 - $1)' '-+_+_++_--~---~+_rand' + +will result to: 1.570796327;0.4473471527,0.4325752577,0.9998908975;0.6601152609 -##TODO +## TO DO +1. add support for complex numbers +2. add implicit multiplication: +`(sin 3 cos 4 --> sin 3 * cos 4` or +`(9 - 4) (sin 3) --> (9 - 4) * (sin 3))` +3. add symbolic mathematics + +*** +For the GUI written in PureBasic: + +- complete the necessary GUI rendering for all +the features from the original CalC program +particularly the scientific calculator + +- create an algorithm so that measuring and +adjusting the buttons and panels would be easy enough + +Take note, Mong is determined to do all these things, +but he is always busy. + +## LICENSE +CalC was originally licensed under MIT by Vivek Kannan. +The copy of that license is retained at this repo. - 1. Add support for complex numbers. - 2. Implicit multiplication. (sin 3 cos 4 --> sin 3 * cos 4, (9 - 4) (sin 3) --> (9 - 4) * (sin 3)) - 3. Symbolic mathematics. \ No newline at end of file +Also, Mong Alvarez Jr. licensed this repo under MIT. diff --git a/_config.yml b/_config.yml new file mode 100644 index 0000000..c419263 --- /dev/null +++ b/_config.yml @@ -0,0 +1 @@ +theme: jekyll-theme-cayman \ No newline at end of file diff --git a/calc.pb b/calc.pb new file mode 100644 index 0000000..1b83b60 --- /dev/null +++ b/calc.pb @@ -0,0 +1,212 @@ +Enumeration + #WIN_MAIN + + ;different buttons + #Button_1 + #Button_2 + #Button_3 + #Button_4 + #Button_5 + #Button_6 + #Button_7 + #Button_8 + #Button_9 + #Button_0 + #Button_Add + #Button_Subtract + #Button_Multiply + #Button_Divide + #Button_ReturnResult + #Button_Period + + #BUTTON_CLEAR + #DISPLAY_AREA + #CONTAINER_GADGET + +EndEnumeration + +;again declaration of the behavior of the window +#F1 = #PB_Window_SystemMenu +#F2 = #PB_Window_ScreenCentered +#F3 = #PB_Window_MinimizeGadget + +#Flags = #F1 | #F2 | #F3 + +;for the RunProgram() +#Flags2 = #PB_Program_Open | #PB_Program_Read | #PB_Program_Hide + +;working directory +;please check this if there are problems +directory.s = GetCurrentDirectory() + +If OpenWindow(#WIN_MAIN, 0, 0, 385, 500, "Simple Calculator", #Flags) + + ;declarations of different widgets + ;#PB_Default is Comic Sans MS + LoadFont(1, "Comic Sans MS", 14,#PB_Font_Bold|#PB_Font_HighQuality) + SetGadgetFont(#PB_Default,FontID(1)) + ButtonGadget(#BUTTON_CLEAR, 45, 135, 110,50, "CLEAR") + + + LoadFont(2, "Consolas", 18,#PB_Font_Bold|#PB_Font_HighQuality) + SetGadgetFont(#PB_Default,FontID(2)) + + ButtonGadget(#Button_1, 45, 200, 50, 50,"1") + ButtonGadget(#Button_2, 105, 200, 50, 50,"2") + ButtonGadget(#Button_3, 165, 200, 50, 50,"3") + ButtonGadget(#Button_Add, 225, 200, 50, 50,"+") + ButtonGadget(#Button_Subtract, 285, 200, 50, 50,"-") + + ButtonGadget(#Button_4, 45, 270, 50, 50, "4") + ButtonGadget(#Button_5, 105, 270, 50, 50, "5") + ButtonGadget(#Button_6, 165, 270, 50, 50, "6") + ButtonGadget(#Button_Multiply, 225, 270, 50, 50, "*") + ButtonGadget(#Button_Divide, 285, 270, 50, 50,"/") + + ButtonGadget(#Button_7, 45, 340, 50, 50, "7") + ButtonGadget(#Button_8, 105, 340, 50, 50, "8") + ButtonGadget(#Button_9, 165, 340, 50, 50, "9") + ButtonGadget(#Button_ReturnResult, 225, 340, 110, 120, "=") + + ButtonGadget(#Button_0, 45, 410, 110, 50, "0") + ButtonGadget(#Button_Period, 165, 410, 50, 50, Chr(46)) + + ContainerGadget(#CONTAINER_GADGET, 15, 10, 355, 110, #PB_Container_Double) + SetGadgetColor(#CONTAINER_GADGET, #PB_Gadget_BackColor, RGB(255,255,255)) + LoadFont(3, "Consolas", 20,#PB_Font_HighQuality) + SetGadgetFont(#PB_Default,FontID(3)) + TextGadget(#DISPLAY_AREA, 0, 35, 345,28,"", #PB_Text_Right) + SetGadgetColor(#DISPLAY_AREA, #PB_Gadget_BackColor, RGB(255,255,255)) + + Repeat + Event.l = WaitWindowEvent() + Select Event + Case #PB_Event_Gadget + + Select EventGadget() + + Case #BUTTON_1 + old.s = GetGadgetText(#DISPLAY_AREA) + new.s = GetGadgetText(#Button_1) + SetGadgetText(#DISPLAY_AREA, old + new) + + Case #BUTTON_2 + old.s = GetGadgetText(#DISPLAY_AREA) + new.s = GetGadgetText(#Button_2) + SetGadgetText(#DISPLAY_AREA, old + new) + + Case #BUTTON_3 + old.s = GetGadgetText(#DISPLAY_AREA) + new.s = GetGadgetText(#Button_3) + SetGadgetText(#DISPLAY_AREA, old + new) + + Case #BUTTON_4 + old.s = GetGadgetText(#DISPLAY_AREA) + new.s = GetGadgetText(#Button_4) + SetGadgetText(#DISPLAY_AREA, old + new) + + Case #BUTTON_5 + old.s = GetGadgetText(#DISPLAY_AREA) + new.s = GetGadgetText(#Button_5) + SetGadgetText(#DISPLAY_AREA, old + new) + + Case #BUTTON_6 + old.s = GetGadgetText(#DISPLAY_AREA) + new.s = GetGadgetText(#Button_6) + SetGadgetText(#DISPLAY_AREA, old + new) + + Case #BUTTON_7 + old.s = GetGadgetText(#DISPLAY_AREA) + new.s = GetGadgetText(#Button_7) + SetGadgetText(#DISPLAY_AREA, old + new) + + Case #BUTTON_8 + old.s = GetGadgetText(#DISPLAY_AREA) + new.s = GetGadgetText(#Button_8) + SetGadgetText(#DISPLAY_AREA, old + new) + + Case #BUTTON_9 + old.s = GetGadgetText(#DISPLAY_AREA) + new.s = GetGadgetText(#Button_9) + SetGadgetText(#DISPLAY_AREA, old + new) + + Case #BUTTON_0 + old.s = GetGadgetText(#DISPLAY_AREA) + new.s = GetGadgetText(#Button_0) + SetGadgetText(#DISPLAY_AREA, old + new) + + Case #Button_Period + old.s = GetGadgetText(#DISPLAY_AREA) + new.s = GetGadgetText(#Button_Period) + SetGadgetText(#DISPLAY_AREA, old + new) + + Case #Button_Add + old.s = GetGadgetText(#DISPLAY_AREA) + new.s = GetGadgetText(#Button_Add) + SetGadgetText(#DISPLAY_AREA, old + new) + + Case #BUTTON_Subtract + old.s = GetGadgetText(#DISPLAY_AREA) + new.s = GetGadgetText(#Button_Subtract) + SetGadgetText(#DISPLAY_AREA, old + new) + + Case #BUTTON_Multiply + old.s = GetGadgetText(#DISPLAY_AREA) + new.s = GetGadgetText(#Button_Multiply) + SetGadgetText(#DISPLAY_AREA, old + new) + + Case #BUTTON_Divide + old.s = GetGadgetText(#DISPLAY_AREA) + new.s = GetGadgetText(#Button_Divide) + SetGadgetText(#DISPLAY_AREA, old + new) + + Case #BUTTON_CLEAR + SetGadgetText(#DISPLAY_AREA, "") + + DisableGadget(#Button_0,0) + DisableGadget(#Button_1,0) + DisableGadget(#Button_2,0) + DisableGadget(#Button_3,0) + DisableGadget(#Button_4,0) + DisableGadget(#Button_5,0) + DisableGadget(#Button_6,0) + DisableGadget(#Button_7,0) + DisableGadget(#Button_8,0) + DisableGadget(#Button_9,0) + DisableGadget(#Button_Add,0) + DisableGadget(#Button_Subtract,0) + DisableGadget(#Button_Multiply,0) + DisableGadget(#Button_Divide,0) + DisableGadget(#Button_Period,0) + DisableGadget(#Button_ReturnResult,0) + + Case #Button_ReturnResult + argument.s = GetGadgetText(#DISPLAY_AREA) + SimpleCalculator = RunProgram( directory + "\calc.exe", argument, directory, #Flags2) + ;Debug SimpleCalculator + result.s = argument + "=" + ReadProgramString(SimpleCalculator) + SetGadgetText(#DISPLAY_AREA, result) + CloseProgram(SimpleCalculator) + + DisableGadget(#Button_0,1) + DisableGadget(#Button_1,1) + DisableGadget(#Button_2,1) + DisableGadget(#Button_3,1) + DisableGadget(#Button_4,1) + DisableGadget(#Button_5,1) + DisableGadget(#Button_6,1) + DisableGadget(#Button_7,1) + DisableGadget(#Button_8,1) + DisableGadget(#Button_9,1) + DisableGadget(#Button_Add,1) + DisableGadget(#Button_Subtract,1) + DisableGadget(#Button_Multiply,1) + DisableGadget(#Button_Divide,1) + DisableGadget(#Button_Period,1) + DisableGadget(#Button_ReturnResult,1) + + EndSelect + EndSelect + + Until Event = #PB_Event_CloseWindow +EndIf diff --git a/resources/GUI.png b/resources/GUI.png new file mode 100644 index 0000000..6a2d694 Binary files /dev/null and b/resources/GUI.png differ diff --git a/resources/console.png b/resources/console.png new file mode 100644 index 0000000..1019696 Binary files /dev/null and b/resources/console.png differ diff --git a/resources/icon.ico b/resources/icon.ico new file mode 100644 index 0000000..f7305dc Binary files /dev/null and b/resources/icon.ico differ