{"id":22307308,"url":"https://github.com/plasmaa0/simplecalculator","last_synced_at":"2025-03-26T01:21:26.821Z","repository":{"id":44627961,"uuid":"449411135","full_name":"Plasmaa0/simpleCalculator","owner":"Plasmaa0","description":"Simple CLI Calculator Project","archived":false,"fork":false,"pushed_at":"2023-03-06T10:00:19.000Z","size":1129,"stargazers_count":3,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-30T22:25:19.577Z","etag":null,"topics":["abstract-syntax-tree","ast","binary-expression-tree","c-plus-plus","calculator","calculator-application","cli","cplusplus","cpp","evaluation","expression-evaluator","interpreter","lexer","math","parser"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Plasmaa0.png","metadata":{"files":{"readme":"README.MD","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":"docs/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-01-18T19:00:45.000Z","updated_at":"2024-10-28T18:55:32.000Z","dependencies_parsed_at":"2022-09-04T02:10:54.067Z","dependency_job_id":null,"html_url":"https://github.com/Plasmaa0/simpleCalculator","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Plasmaa0%2FsimpleCalculator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Plasmaa0%2FsimpleCalculator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Plasmaa0%2FsimpleCalculator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Plasmaa0%2FsimpleCalculator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Plasmaa0","download_url":"https://codeload.github.com/Plasmaa0/simpleCalculator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245568932,"owners_count":20636865,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["abstract-syntax-tree","ast","binary-expression-tree","c-plus-plus","calculator","calculator-application","cli","cplusplus","cpp","evaluation","expression-evaluator","interpreter","lexer","math","parser"],"created_at":"2024-12-03T20:09:28.704Z","updated_at":"2025-03-26T01:21:26.802Z","avatar_url":"https://github.com/Plasmaa0.png","language":"C++","readme":"![](https://tokei.rs/b1/github/Plasmaa0/simpleCalculator)\n# Simple Interpreting Calculator Project\n\n- [Simple Interpreting Calculator Project](#simple-interpreting-calculator-project)\n  - [Requirements](#requirements)\n  - [Building and running the project:](#building-and-running-the-project)\n  - [Demo](#demo)\n  - [Algorithm](#algorithm)\n  - [Main features](#main-features)\n  - [Other features](#other-features)\n\n## Requirements\n - g++ compiler\n\n## Building and running the project:\n```bash\nmake run\n```\n\n## Demo\n\u003cimg src=\"docs/example03.gif\" alt=\"example03\" width=\"400\" height=\"600\"/\u003e\n\n## Algorithm\n[**GeeksForGeeks**](https://www.geeksforgeeks.org/expression-tree/)\\\n[**Wikipedia**](https://en.wikipedia.org/wiki/Binary_expression_tree)\n1. Get the expression as a string.\n   - If string contains assignment operation then split it to variable and expression to evaluate. ('x=1+2' -\u003e variable to assign a value to: 'x' and value to be assigned '1+2' (will be evaluated in next steps))\n2. Convert string to array of special symbols (operators, operands, brackets, etc).\n   1. Handle sequences of numbers to get numbers with multiple digits and number with floating point.\n   2. Handle sequences of letters to get variables with multiple digits including numbers (like 'var1').\n   3. Handle unary operators by converting them to binary operators (replace '-x' with '(-1)*x').\n3. Convert array of special symbols to BET (binary expression tree)\n   ```\n   First example:\n   EXPRESSION: (3*(x-4)-8)*(6-2*y)\n   BINARY EXPRESSION TREE:\n   ---{*}\n      |--{-}\n      |   |--{*}\n      |   |   |--{3}\n      |   |   ---{-}\n      |   |       |--{x}\n      |   |       ---{4}\n      |   ---{8}\n      ---{-}\n         |--{6}\n         ---{*}\n               |--{2}\n               ---{y}\n\n\n   Second example (different output of a tree):\n   EXPRESSION: (34 * (-var - 4.5) + 68.95) * (67 - (5^2)/per)\n   BINARY EXPRESSION TREE:\n                  {per}\n               {/}\n                     {2.00}\n                  {^}\n                     {5.00}\n            {-}\n               {67.00}\n         {*}\n               {68.95}\n            {+}\n                     {4.50}\n                  {-}\n                        {var}\n                     {*}\n                        {-1.00}\n               {*}\n                  {34.00}\n   ```\n   - If current expression is length of 1, then add it to root.\n   - Else find the last operator with the minimum priority and add it to root. Then add to root the result of recursive call of the same function on the expression on the left and right side of this operator.\n4. Evaluate binary BET from bottom to top.\n   - If the node is number or variable, then return it's value.\n   - Else (the node is operator) return the result of recursive call of the same function on the right and left child.\n\n## Main features\n- Functions\n- Types: floating point, integer, nan\n- Basic binary operations: - + * /\n- Binary shifts \u003c and \u003e\n- Unary + and -\n- Multiple digits numbers. ex: 123\n- Multiple digits variables (including numbers on non-first position). ex: length1\n- Floating point number. 3.14 or .15\n- Positive integer powers: 2^3 -\u003e 8\n- Space ignoring in expressions for better user experience.\n- Syntax errors handling\n- External Libraries\n\n## Other features\n\n**Use ! sign to print something**\n- In a console mode it'll just print message after '!'\n- In a file mode it'll print message after '!' and result of evaluating of next line of file.\n```\nFile mode:\n   File:\n   !radius = \n   radius = 10\n\n   Output:\n   [6]: radius=\n   [7]: 10\n\nConsole mode:\n   \u003e\u003e\u003e !hello\n   hello\n```\n\n**See the result of previous evaluation with reserved underscore '_' variable**\n```\n\u003e\u003e\u003e 3*4 + 2^5\n44\n\u003e\u003e\u003e _\n44\n\u003e\u003e\u003e \n```\n\n**Display current variables with their values with 'vars' command**\n```\n\u003e\u003e\u003e x=1\n1\n\u003e\u003e\u003e y=2\n2\n\u003e\u003e\u003e z=x*y^2\n4\n\u003e\u003e\u003e vars\nVARIABLES:\n   _ = int   4\n   x = int   1\n   y = int   2\n   z = int   4\nEND\n```\n\n**External libraries**\nCreate .splc file with variable and function declarations.\\\nOn runtime use 'import' keyword.\\\nInside a library file you can only declare functions and assign values to variables. NOTHING MORE. Any other commands will be ignored.\\\nOnly one level of depth:\\\nLibrary -\u003e none\\\nInputFile -\u003e Library -\u003e none\n```\n\u003e\u003e\u003e import math\nimporting: 'math'\nimport finished\n```\nThere is a predefined math.splc with definition of pi, e, area(radius) - calculate area of a circle with a given radius.\n\n**Functions**\nDefine a function with keyword:\n```\ndef:\n```\nSyntaxis of defining a function:\n```\ndef: name([arg1, arg2, ...]) = expression\n```\nExample:\n```\n\u003e\u003e\u003e def: mult_add_square(a,b,c)=a*b+c^2\nnew function 'mult_add_square' added\n```\nMaximum number of function args see in constants.h: FUNCTION_MAX_ARGS_N.\\\nArgs can be:\n- Number\n- Variable\n- Call of other function\\\n**Args can't be expression (like '3+1' or 'x*10').**\nExample of calling a function defined in previous example with variable and number arg:\n```\n\u003e\u003e\u003e x = 2\n2\n\u003e\u003e\u003e mult_add_square(x,3,4) // 2*3 + 4^2 = 6 + 16 = 22\n22\n```\nExample of calling a function with argument as another function call:\n```\n\u003e\u003e\u003e def: f(x) = x^2\nnew function 'f' added\n\u003e\u003e\u003e def: g(x) = x+1\nnew function 'g' added\n\u003e\u003e\u003e g(f(2))\n5\n```\n**Functions can use other functions in their expression:**\n```\n\u003e\u003e\u003e def: f(x) = x^2\nnew function 'f' added\n\u003e\u003e\u003e def: g(y) = f(y)*2\nnew function 'g' added\n\u003e\u003e\u003e g(2)\n8\n```\nTo define a function with no args use null keyword\n```\n\u003e\u003e\u003e def: f(null) = 10*0.5\nnew function 'f' added\n\u003e\u003e\u003e funcs\nFUNCTIONS:\n   f() = 10*0.5\nEND\n```\n\n**Save current variables in text or binary file. Load previously saved variables if binary file with them exists. Use keywords 'save', 'saveB', 'load'.\\\nSave and load example:**\n```\n\u003e\u003e\u003e x=1\n1\n\u003e\u003e\u003e y=2\n2\n\u003e\u003e\u003e saveB\nsuccessfully saved 3 variables\n\u003e\u003e\u003e x=0\n0\n\u003e\u003e\u003e y=0\n0\n\u003e\u003e\u003e load\nsuccessfully loaded 3 variables\n\u003e\u003e\u003e x\n1\n\u003e\u003e\u003e y\n2\n```\n\n\n**Close the application with 'exit'**\n```\n\u003e\u003e\u003e exit\nbye\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fplasmaa0%2Fsimplecalculator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fplasmaa0%2Fsimplecalculator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fplasmaa0%2Fsimplecalculator/lists"}