Code creation:CalcIt

From Lê Duy Quang's personal wiki
Jump to: navigation, search

CalcIt is a console calculator written in Java. What makes it different from other calculator applications is that it uses Vietnam's math notation.

You can check out its code here. Licensed under MIT.


CalcIt can be used as:

  • a normal console application for evaluating math expressions.
  • a library for other applications to evaluate expressions.



As a program[edit]

CalcIt can be run as a standalone program in the command line.

  • If it is supplied arguments, those arguments will be merged into one string as the expression to calculate. It will print the result if valid and then exit.
  • If it is run without arguments, it will act as an “interpreter” that takes user’s input and gives the result out. To calculate an expression, just type it in and press Enter. The following commands are also available when typed in and press Enter:
    • help: Prints out the general help.
    • vars: Prints out the variables’ values.
    • exit: Exits the interpreter as well as the program.

Also, when nothing is typed and Enter is pressed, if there was a successful expression calculated previously in the session, it will be called back to calculate again.

As a library[edit]

Use the class Calculator. After making a new instance of it, calculate an expression by invoking the method calculate(String expression), which returns a value of type double.

If the expression contains errors, the library will throw ExpressionInvalidException, which contains a detailed message of the cause and the error location.

A variable can be retrieved by calling getVariable(int var).

More detailed descriptions can be found in the library’s Javadoc.


The library takes a mathematical expression in the form of a String. If the expression is valid, it will output the result evaluated from that expression.

Any spaces in the expression will be removed.


The library accepts numbers with comma (,) as the decimal separator. Example: 16; 35,25.

Percent sign (%) can be appended at the end of a number, dividing the number by 100. For example: 16% = 0,16; 35 . 16% = 35 . 0,16 = 5,6. Only one percent sign is permitted after a number.

The library also accepts pi (case nonsensitive) as a number, which is converted to 3,141592653589793.


The library accepts these operators:

Operator Description Example
+ Addition 16 + 25 = 41
Subtraction 16 – 25 = -9
. Multiplication 16 . 25 = 400
: Division 16 : 25 = 0,64
^ Exponentiation 5 ^ 3 = 125
v Root 3 v 125 = 5
& AND (for integers) 13 & 7 = 5
| OR (for integers) 13 | 7 = 15


These functions take a value in their bracelets as their inputs. Example: sin(45); cos[113]. If there is nothing inside the bracelet, the it will be treated as 0.

Name Description
sin Trigonometric sine
cos Trigonometric cosine
tan Trigonometric tangent
cot Trigonometric cotangent
log Base 10 logarithm
ln Natural (base e) logarithm


Three kinds of bracelets are accepted: () [] {}, but two braces in a pair must be matching. For example, [16.(35+25)] is valid, but [16.(35+25]) and [16.(35+25)} are not.

Empty bracelets () [] {} are treated as zeros.

If bracelets aren't closed at the end of the expression, the library will automatically close them.

The library doesn’t treat things right before a bracelet and the bracelet itself as a multiplication. That means, 16(3+5) is not valid.


Each instance of Calculator class and the program will have 6 user-assignable variables: A; B; C; D; E; F, which are initialized by 0, and can be assigned with <variable name> = <expression>. It also has 2 special variables: Ans & PreAns. Ans stores the result of the last calculation, and PreAns stores the result of the calculation right before that. These two numbers are also initialized by 0, and they cannot be assigned the way 6 former variables are.

The variables’ names are not case-sensitive, so they can be written freely, such as a; B; ANS; preans; pReAnS, and are still perfectly acceptable.

Multiple variables can be assigned at once.


If two operators have the same priority and are next to each other, they will be calculated from left to right.

Priority Operators/Symbols
First Things in bracelets; functions
Second Exponentiation (^); Root (v)
Third Multiplication (.); Division (:)
Fourth Addition (+); Subtraction (-)
Fifth AND (&)
Sixth OR (|)