We already discussed this in README.md. If your project seems
filled with unnecessary files then you might want to run cmake
inside a
directory like:
mkdir build && cd build
cmake .. # Now CMakeLists.txt is in the previous directory
make
You can just type in the name of the executable and it should startup prompting you for input:
$ ./calc # or ./src/calc
This is free software with ABSOLUTELY NO WARRANTY.
>> _
Just type in some expressions like 3+2
or 3*sin90
etc. and it would give you
some output based on your input.
Option | Description |
---|---|
-e <expression> | Given an expression. It will give the answer. |
-f <file> | Given a file having a list of expressions, the output is shown one by one. |
-q | Be quiet. Don’t spit unnecessary output. |
-c | Quit after reading all the shell arguments. |
-j | Give JSON formatted output. |
Given an expression of the form sin(cos(3.14 - 3.14 / 0.707))
the calculator
basically parses each token from the left. Here sin
is matched with a list of
known functions stored in unOpsHash list containing hashes of the known
unary functions. If it was -
instead then the above search would fail, thus
we would have to check binOpsHash list for available binary operators. If some
token fails both then that is an error, which currently is stated as parseError.
These hashes are calculated beforehand in main()
for efficiency purposes using
makeOperatorHashes function which basically converts human readable strings into
numbers and stores them into unOpsHash and binOpsHash lists.
After a token’s verification, their priority is determined by Operator::checkPriority based on which the operatorManager takes various decisions on whats to do next.
Things that the operatorManager
does:
- Calculate an atomic expression(An expression which can’t be more simplified for calculation purposes)
- Throw errors
- Insert a number in the
numberStack
if a number insertion is in demand - Modify the
operatorStack
based on the incoming operator
The actual parsing of tokens is done solely by calcParser.hpp.
How the CLI calculator works? :
- Process the shell arguments
- Take input
- Exit if we get
exit
as an input - Else call
execute()
:- Initialize a
calcParse
instance with the input - Call
startParsing()
on the instance just created - If
startParsing()
Throws error then:- Display the error
- Else get the answer by calling
Ans()
on the instance
- Initialize a
- If any of the above things seem inappropriate then its either a bug or this document needs verification. In any of the above cases it is advised that you open an issue, talk to us or send us an Email specifying the bug.
- It is advised that you read this file in Emacs for better interaction purposes
as GitHub has a very sparse support for
.org
files. - If you don’t have Unicode fonts on your system or your text editor doesn’t support them then this text may seem awkward to you.