Skip to content

A library that calculates a string into a number

Notifications You must be signed in to change notification settings

rayzhuca/calculator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

45 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Calculator

Calculator is a library for calculating a string into a double mathematically in Java.

How to use

Calculate a number

public static void main(String[] args) throws Exception {
  System.out.println(new Calculator().calculate("5+2/4"));
}

Output:

5.5

Block an operator

public static void main(String[] args) {
  Calculator calculator = new Calculator();
  Manager manager = new Manager();
  manager.removeOperation(Operator.ADD);
  try {
    System.out.println(calculator.calculate("2+2"));
  } catch (Exception e) {
    e.printStackTrace();
  }
}

Output:

java.lang.IllegalArgumentException: Blocked operator

Check validity before calculating

public static void main(String[] args) {
  Manager manager = new Manager();
  manager.removeOperation(Operator.ADD);
  Calculator calculator = new Calculator(manager);

  System.out.println(calculator.checkValidity("2+2"));

  manager.addOperation(Operator.ADD);
  System.out.println(calculator.checkValidity("2+2"));
}

Output:

false
true

Bugs

There are plenty of bugs and problems with this library, for example operations that is unary may not currently work. Please post bugs into Issues page for me to fix.

Upcoming updates

  • Unary operators
  • Postfix operators, like factorials
  • Algebra
  • Ability to save a variable into a calculator instance

Usage

Anyone is welcome to fork and work on this project, I may accept pull requests only if they are reasonable.

About

A library that calculates a string into a number

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages