Git Product home page Git Product logo

calculator's Introduction

Calculator

A simple calculator made in Java thats parses a String input and returns a double.

Created by BricksAndPieces

Features

  • Fully text based, no need for a UI
  • Supports power, parentheses, and order of operations
  • Based around one method that will repeatedly call itself to fully parse the equation

How it works
This calculator follows all math rules such as the Order of Operations (PEMDAS).

P - Parentheses

E - Exponents

M - Multiplication

D - Divison

A - Addition

S - Subtraction

Along with this idea, the calculator also supports power, roots (soon), and some other smaller features.

The calculator is heavily built around the idea of recursion. It is used to parse the equation that is inputted as a String into a double output. The first step in parsing the String is to look for any parentheses and format according to them. In this case, we are just parsing items withing the parentheses first before moving on with the rest of the equation.

final int end = s.indexOf(")");
if(end != -1) {
final int start = s.substring(0, end).lastIndexOf("(");
if(start == -1)
  throw new IllegalArgumentException("Parentheses do not match");

return parse(s.substring(0, start) + parse(s.substring(start+1, end)) + s.substring(end+1));

Please note that "s" is the String representation of the equation

We then need to parse the actual equation after all the parentheses are taken care of. This is relatively simple and just means we need to look for the operator signs (+, - , *, /, ^) and parse accordingly. We go backwards in PEMDAS because we start at the outside and work our way in. That way, when we finally reach the end of the recursion, we start with the power, the the division, and so on.

if(s.contains(ADD)) {
  final int index = s.indexOf(ADD);
  return parse(s.substring(0, index)) + parse(s.substring(index+1));
}else if(s.contains(SUBTRACT)) {
  final int index = s.indexOf(SUBTRACT);
  return parse(s.substring(0, index)) - parse(s.substring(index+1));
}else if(s.contains(MULTIPLY)) {
  final int index = s.indexOf(MULTIPLY);
  return parse(s.substring(0, index)) * parse(s.substring(index+1));
}else if(s.contains(DIVIDE)) {
  final int index = s.indexOf(DIVIDE);
  return parse(s.substring(0, index)) / parse(s.substring(index+1));
}else if(s.contains(POWER)) {
  final int index = s.indexOf(POWER);
  return Math.pow(parse(s.substring(0, index)), parse(s.substring(index+1)));
}

Please note that "s" is the String representation of the equation

That is all that is needed to successfully parse the String. All that is left to do is return the double if there are no operators left and if its a valid equation. This can be done easily with Double.parseDouble(s);.

This is just how the calculator works in concept. Feel free to clone this project locally to try it out yourself! More information on how the code works along with the entire Calculator class can be found here.

Issues
Find any issues? Feel free to suggest a change and/or report a bug!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.