Calculator for Big Numbers
From Java Example Source Code
Contents |
[edit] Overview - Calculator for Big Numbers
This is a example of Java program shows how to create a calculator for big numbers.
[edit] Java Source Code
- Package: example.biginteger
- File: BigNumCalc.java
package example.biginteger; import java.math.BigDecimal; import java.util.Stack; /** A trivial reverse-polish stack-based calculator for big numbers */ public class BigNumCalc { /** an array of Objects, simulating user input */ public static Object[] testInput = { new BigDecimal("3419229223372036854775807.23343"), new BigDecimal("2.0"), "*", }; public static void main(String[] args) { BigNumCalc calc = new BigNumCalc(); System.out.println(calc.calculate(testInput)); } Stack s = new Stack(); public BigDecimal calculate(Object[] input) { BigDecimal tmp; for (int i = 0; i < input.length; i++) { Object o = input[i]; if (o instanceof BigDecimal) { s.push(o); } else if (o instanceof String) { switch (((String) o).charAt(0)) { // + and * are commutative, order doesn't matter case '+': s.push(((BigDecimal) s.pop()).add((BigDecimal) s.pop())); break; case '*': s.push(((BigDecimal) s.pop()).multiply((BigDecimal) s.pop())); break; // - and /, order *does* matter case '-': tmp = (BigDecimal) s.pop(); s.push(((BigDecimal) s.pop()).subtract(tmp)); break; case '/': tmp = (BigDecimal) s.pop(); s.push(((BigDecimal) s.pop()).divide(tmp, BigDecimal.ROUND_UP)); break; default: throw new IllegalStateException("Unknown OPERATOR popped"); } } else { throw new IllegalArgumentException("Syntax error in input"); } } return (BigDecimal) s.pop(); } }
[edit] What Result You Can Get
Run the program, you will get:
6838458446744073709551614.466860
[edit] Required External Libraries and/or Files for this Java Example
Need nothing.
[edit] How to Run this Java Example Program
We recommend running this Java example program with Eclipse.
For assistance in working with Eclipse, please see How to Run Java Program with Eclipse.
It's fairly easy.
[edit] Question & Answer
Any question?
Click edit and post your question or answer here.
