diff --git a/src/com/github/antoinejt/jasc/ConsoleUI.java b/src/com/github/antoinejt/jasc/ConsoleUI.java index 67403e0..0aaf202 100644 --- a/src/com/github/antoinejt/jasc/ConsoleUI.java +++ b/src/com/github/antoinejt/jasc/ConsoleUI.java @@ -9,6 +9,24 @@ import java.util.*; final class ConsoleUI { + private static final List commands = new ArrayList<>(); + private static final Map operators = new HashMap<>(); + static { + operators.put("+", OperationType.ADDITION); + operators.put("-", OperationType.SUBSTRACTION); + operators.put("*", OperationType.MULTIPLICATION); + operators.put("/", OperationType.DIVISION); + operators.put("%", OperationType.MODULO); + operators.put("^", OperationType.POWER); + + commands.addAll(operators.keySet()); // operators are added here + commands.addAll(Arrays.asList( + "sqrt", "log", "ln", "lb", "cos", "sin", "tan", "arccos", "arcsin", "arctan", "exp", // Functions + "=", "help", "clear", "quit" // Commands + )); + } + + // TODO Replace that by some txt templates private static void displayHelp(){ TextFormat.listThings("Available operators (acts on 2 operands) : ", "+ : Addition operator", @@ -67,11 +85,6 @@ public static void useConsole() throws Exception { String input; while (true){ input = scanner.next(); - List commands = Arrays.asList( - "+", "-", "*", "/", "%", "^", // Operators - "sqrt", "log", "ln", "lb", "cos", "sin", "tan", "arccos", "arcsin", "arctan", "exp", // Functions - "=", "help", "clear", "quit" // Commands - ); boolean hasInputNumber = !commands.contains(input); if (hasInputNumber){ try { @@ -81,14 +94,6 @@ public static void useConsole() throws Exception { System.err.println("Your input is invalid!"); } } else { - Map operators = new HashMap<>(); - operators.put("+", OperationType.ADDITION); - operators.put("-", OperationType.SUBSTRACTION); - operators.put("*", OperationType.MULTIPLICATION); - operators.put("/", OperationType.DIVISION); - operators.put("%", OperationType.MODULO); - operators.put("^", OperationType.POWER); - if (operators.containsKey(input)){ try { calculatorEngine.operate(operators.get(input)); diff --git a/src/com/github/antoinejt/jasc/Constants.java b/src/com/github/antoinejt/jasc/Constants.java index 4478ae7..ccbde0e 100644 --- a/src/com/github/antoinejt/jasc/Constants.java +++ b/src/com/github/antoinejt/jasc/Constants.java @@ -1,6 +1,6 @@ package com.github.antoinejt.jasc; final class Constants { - static final String VERSION = "0.3.2"; - static final String LAST_UPDATE = "May 3rd 2019"; + static final String VERSION = "0.3.3"; + static final String LAST_UPDATE = "June 11th 2019"; } diff --git a/src/com/github/antoinejt/jasc/Main.java b/src/com/github/antoinejt/jasc/Main.java index cb49e73..2a96a9a 100644 --- a/src/com/github/antoinejt/jasc/Main.java +++ b/src/com/github/antoinejt/jasc/Main.java @@ -4,8 +4,8 @@ public class Main { public static void main(String[] args){ try { ConsoleUI.useConsole(); - } catch (Exception e) { - e.printStackTrace(); + } catch (Exception exception) { + exception.printStackTrace(); } } } diff --git a/src/com/github/antoinejt/jasc/calculator/CalculatorEngine.java b/src/com/github/antoinejt/jasc/calculator/CalculatorEngine.java index 224950c..e995abf 100644 --- a/src/com/github/antoinejt/jasc/calculator/CalculatorEngine.java +++ b/src/com/github/antoinejt/jasc/calculator/CalculatorEngine.java @@ -19,35 +19,36 @@ public List getNumbers(){ List stackContent = null; try { stackContent = (List) ReflectUtil.getPrivateField(stack, "stack"); - } catch (IllegalAccessException | NoSuchFieldException e) { - e.printStackTrace(); + } catch (IllegalAccessException | NoSuchFieldException exception) { + exception.printStackTrace(); } return stackContent; } private float[] getOperands(){ - float[] ret = new float[2]; + float[] operands = new float[2]; for (int i = 0; i < 2; i++){ - ret[i] = stack.pop(); + operands[i] = stack.pop(); } - return ret; + return operands; } private double getFunctionResult(FunctionType functionType) throws OperandException { if (stack.getSize() > 0){ - float calc = stack.pop(); - switch(functionType){ // TODO Implement that on ConsoleUI - case SQRT: return Math.sqrt(calc); - case LOG10: return Math.log10(calc); - case LN: return Math.log(calc); - case LOGB: return (int)(Math.log(calc)/Math.log(2)+1e-10); // https://stackoverflow.com/questions/3305059/how-do-you-calculate-log-base-2-in-java-for-integers - case COS: return Math.cos(calc); - case SIN: return Math.sin(calc); - case TAN: return Math.tan(calc); - case ARCCOS: return Math.acos(calc); - case ARCSIN: return Math.asin(calc); - case ARCTAN: return Math.atan(calc); - case EXP: return Math.exp(calc); + float number = stack.pop(); + switch(functionType){ // TODO Implement that on ConsoleUI (why?) + case SQRT: return Math.sqrt(number); + case LOG10: return Math.log10(number); + case LN: return Math.log(number); + // TODO Put that in a dedicated function + case LOGB: return (int)(Math.log(number)/Math.log(2)+1e-10); // https://stackoverflow.com/questions/3305059/how-do-you-calculate-log-base-2-in-java-for-integers + case COS: return Math.cos(number); + case SIN: return Math.sin(number); + case TAN: return Math.tan(number); + case ARCCOS: return Math.acos(number); + case ARCSIN: return Math.asin(number); + case ARCTAN: return Math.atan(number); + case EXP: return Math.exp(number); } } else { throw new OperandException("Stack is empty!"); @@ -59,7 +60,7 @@ public void applyFunction(FunctionType functionType) throws CalculatorException try { double result = getFunctionResult(functionType); if (!Double.isNaN(result)){ - stack.push(Double.valueOf(result).floatValue()); + stack.push((float) result); } else { throw new CalculatorException("Some weird error occurred when applying function. Please contact the application maintainer!"); } @@ -70,27 +71,27 @@ public void applyFunction(FunctionType functionType) throws CalculatorException public void operate(OperationType operation) throws OperandException, CalculatorException { if (stack.getSize() > 1) { - float[] operand = getOperands(); - float calc; - if (operation == OperationType.DIVISION && operand[0] == 0.0f){ + float[] operands = getOperands(); + float result; + if (operation == OperationType.DIVISION && operands[0] == 0.0f){ System.err.println("Division by zero!"); for(int i = 0; i < 2; i++){ - stack.push(operand[1-i]); // Reinject operands into the stack! + stack.push(operands[1-i]); // Reinject operands into the stack! } return; } switch(operation){ - case ADDITION: calc = operand[1] + operand[0]; break; - case SUBSTRACTION: calc = operand[1] - operand[0]; break; - case MULTIPLICATION: calc = operand[1] * operand[0]; break; - case DIVISION: calc = operand[1] / operand[0]; break; - case MODULO: calc = operand[1] % operand[0]; break; - case POWER: calc = (float) Math.pow(operand[1], operand[0]); break; + case ADDITION: result = operands[1] + operands[0]; break; + case SUBSTRACTION: result = operands[1] - operands[0]; break; + case MULTIPLICATION: result = operands[1] * operands[0]; break; + case DIVISION: result = operands[1] / operands[0]; break; + case MODULO: result = operands[1] % operands[0]; break; + case POWER: result = (float) Math.pow(operands[1], operands[0]); break; default: throw new CalculatorException("The provided operation is not handled!"); } // I can't imagine how this can happened - // if (Float.isNaN(calc)) return; - stack.push(calc); + // if (Float.isNaN(result)) return; + stack.push(result); } else { throw new OperandException("Can't operate without at least 2 operands!"); } diff --git a/src/com/github/antoinejt/jasc/calculator/Stack.java b/src/com/github/antoinejt/jasc/calculator/Stack.java index 8782318..74c0094 100644 --- a/src/com/github/antoinejt/jasc/calculator/Stack.java +++ b/src/com/github/antoinejt/jasc/calculator/Stack.java @@ -3,12 +3,10 @@ import java.util.EmptyStackException; final class Stack { - private int size = 0; private java.util.Stack stack = new java.util.Stack<>(); T pop(){ - if (size > 0){ - size--; + if (stack.size() > 0){ return stack.pop(); } else { throw new EmptyStackException(); @@ -16,16 +14,14 @@ T pop(){ } void push(T item){ - size++; stack.push(item); } void clear(){ - size = 0; stack = new java.util.Stack<>(); } int getSize(){ - return size; + return stack.size(); } }