Skip to content

Simple text-based calculator app written in Java.

Notifications You must be signed in to change notification settings

eli215/calculator-app

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Calculator

Calculator is a simple text-based calculator app made in Java. It features the basic arithmetic operators, as well as a few trigonometric and logarithmic functions.

Operands

Composed of real numbers which fit within the bounds of an 8-byte double variable.

Binary operators

Syntax: <expression> <operator> <expression>

Operation Symbol Example
Addition + 1 + 2 = 3
Subtraction - 2 - 3 = -1
Multiplication * 2 * 3 = 6
Division / 4 / 2 = 2
Exponentiation ^ 2 ^ 3 = 8

Unary operators

Negation syntax: <operator> <expression>
Function syntax: <operator> (<expression>)

Operation Symbol Example
Negation - -1 + 3 = 2
Sine sin() sin(30) = ½
Cosine cos() cos(90) = 0
Tangent tan() tan(45) = 1
Cotangent cot() cot(90) = 0
Natural log (loge) ln() ln(e) = 1
Decimal log (log10) log() log(100) = 2

Note: these mathematical functions are considered unary operators because they require only one term as input.

Grouping "operator"

Syntax: (<expression>)

Use parentheses to elevate the evaluation precedence of an expression or sub-expression.

Usage

Executable calculator-1.0.jar file is located in \target directory.

With command-line argument:

java -jar calculator-1.0.jar "(12-(2^2)*2)/-4"
-1
java -jar calculator-1.0.jar "2+4-(12/-3)"
10
java -jar calculator-1.0.jar "(1+(2+3)"
[Error] Mismatched parenthesis.

Without command-line argument:

java -jar calculator-1.0.jar
Welcome to Eli's Text-based Calculator v1.0
Supported operations: +, -, *, /, ^, sin(), cos(), tan(), cot(), ln(), log().
Type "EXIT" to exit the app.
>> (12-(2^2)*2)/-4
-1
>> 2+4-(12/-3)
10
>> (1+(2+3)
[Error] Mismatched parenthesis.
>> EXIT

Rules

  • Each operator must be used with its respective number of operands.
    • eg. The expression "10/5" is valid, but "/5" is missing its left operand.
  • No mismatched grouping parentheses; each '(' requires a matching ')'.
  • Function-call parentheses are mandatory.
  • Trigonometric function arguments are in degrees.
  • Spaces are optional and have no effect on expression evaluation.

Credits

exp4j library

  • implementation of Dijkstra's Shunting Yard Algorithm
  • Token class & its utilities

Source: https://www.objecthunter.net/exp4j/


apache/commons/lang3/math/NumberUtils

  • isParsable() and withDecimalsParsing() methods used for validating real number operands

Source: https://commons.apache.org/proper/commons-lang/apidocs/src-html/org/apache/commons/lang3/math/NumberUtils.html#line.1721

About

Simple text-based calculator app written in Java.

Topics

Resources

Stars

Watchers

Forks

Languages