Skip to content

Latest commit

 

History

History
79 lines (70 loc) · 4.25 KB

GettingStarted.org

File metadata and controls

79 lines (70 loc) · 4.25 KB

Getting started with advCalc

More on building

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

Using it from the command line

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.

Command line options

OptionDescription
-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.
-qBe quiet. Don’t spit unnecessary output.
-cQuit after reading all the shell arguments.
-jGive JSON formatted output.

The mechanism

The expression calculator

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? :

  1. Process the shell arguments
  2. Take input
  3. Exit if we get exit as an input
  4. Else call execute():
    1. Initialize a calcParse instance with the input
    2. Call startParsing() on the instance just created
    3. If startParsing() Throws error then:
      1. Display the error
    4. Else get the answer by calling Ans() on the instance

Footnotes

  • 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.