Skip to content

Commit

Permalink
🔀 Merge pull request #19 from AntoineJT/develop
Browse files Browse the repository at this point in the history
🔖 Release 0.8.0
  • Loading branch information
AntoineJT authored Oct 2, 2020
2 parents 875209e + eb4c747 commit a04eb45
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 37 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ plugins {

group 'com.github.antoinejt'

def lastUpdate = 'December 2nd 2019'
def lastUpdate = 'December 6th 2019'

def majorVersionNumber = '0'
def minorVersionNumber = '7'
def minorVersionNumber = '8'
def patchVersionNumber = '0'
def wipBuildNumber = '0' // if different from 0 it indicates that this is a wip version, not a yet released one

Expand Down
20 changes: 18 additions & 2 deletions src/main/java/com/github/antoinejt/jasc/ConsoleUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class ConsoleUI {
.put("%", OperationType.MODULO)
.put("^", OperationType.POWER).build();
private static final Set<String> commands = new HashSet<>(
Arrays.asList("=", "help", "clear", "pop", "quit")
Arrays.asList("=", "help", "clear", "pop", "infos", "quit")
);

private static void displayViewFromInside(String pathToFile, Map<String, String> data) {
Expand All @@ -72,6 +72,19 @@ private static void displayViewFromInside(String pathToFile, Map<String, String>
System.out.println(viewContent);
}

private static void displayInfos() {
Map<String, String> data = new HashMapBuilder<String, String>()
.put("VERSION", ManifestInfos.VERSION.toString())
.put("LAST_UPDATE", ManifestInfos.LAST_UPDATE.toString())
.put("REVISION", ManifestInfos.REVISION.toString())
.put("BUILT_BY", ManifestInfos.BUILT_BY.toString())
.put("TIMESTAMP", ManifestInfos.TIMESTAMP.toString())
.put("COMPILED_WITH", ManifestInfos.COMPILED_WITH.toString())
.put("BUILD_JDK", ManifestInfos.BUILD_JDK.toString())
.put("BUILD_OS", ManifestInfos.BUILD_OS.toString()).build();
displayViewFromInside("/views/cli/infos.txt", data);
}

private static void displayHelp() {
displayViewFromInside("/views/cli/help.txt", null);
}
Expand Down Expand Up @@ -156,6 +169,9 @@ private static void executeCommand(CalculatorEngine calculatorEngine, String inp
case "pop":
calculatorEngine.removeLastNumber();
break;
case "infos":
displayInfos();
break;
case "quit":
System.exit(0);
break;
Expand All @@ -174,7 +190,7 @@ private static void tryToApplyOperation(CalculatorEngine calculatorEngine, Strin

private static void tryToAddNumberToTheStack(CalculatorEngine calculatorEngine, String input) {
try {
float number = Float.parseFloat(input);
double number = Double.parseDouble(input);
calculatorEngine.addNumber(number);
} catch (NumberFormatException unused) {
System.err.println("Your input is invalid!");
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/com/github/antoinejt/jasc/ManifestInfos.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,15 @@
import java.util.jar.Manifest;

public enum ManifestInfos {
LAST_UPDATE("Last-Update"),
VERSION("Version");
LAST_UPDATE("Last-Update")
, VERSION("Version")
, REVISION("Build-Revision")
, BUILT_BY("Built-By")
, TIMESTAMP("Build-Timestamp")
, COMPILED_WITH("Created-By")
, BUILD_JDK("Build-Jdk")
, BUILD_OS("Build-OS")
;

private static Attributes attributes = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
import java.util.Stack;

public class CalculatorEngine {
private Stack<Float> stack = new Stack<>(); // TODO MrMicky says that it's better to use Deque instead of Stack
private Stack<Double> stack = new Stack<>(); // TODO MrMicky says that it's better to use Deque instead of Stack

public void addNumber(float number) {
public void addNumber(double number) {
stack.push(number);
}

Expand All @@ -47,7 +47,7 @@ public List getNumbers() {

public void applyFunction(FunctionType functionType) {
try {
float result = (float) getFunctionResult(functionType);
double result = getFunctionResult(functionType);
stack.push(result);
} catch (IllegalStateException unused) {
System.err.println("No operand left to apply this function to!");
Expand All @@ -59,7 +59,7 @@ private double getFunctionResult(FunctionType functionType) throws IllegalStateE
throw new IllegalStateException("Stack is empty!");
}

float number = stack.pop();
double number = stack.pop();
return functionType.apply(number);
}

Expand All @@ -72,40 +72,37 @@ public void applyOperation(OperationType operation) throws IllegalStateException
throw new IllegalStateException("Can't operate without at least 2 operands!");
}

float[] operands = getOperands();
double[] operands = getOperands();

if (operation == OperationType.DIVISION && operands[0] == 0.0f) {
if (operation == OperationType.DIVISION && operands[0] == 0.0d) {
System.err.println("Division by zero!");
reinjectOperandsIntoTheStack(operands);
return;
}

float result = getOperationResult(operation, operands);
double result = getOperationResult(operation, operands);
stack.push(result);
}

private float[] getOperands() {
float[] operands = new float[2];

for (int i = 0; i < 2; i++) {
operands[i] = stack.pop();
}
private double[] getOperands() {
double[] operands = new double[2];
operands[0] = stack.pop();
operands[1] = stack.pop();
return operands;
}

private void reinjectOperandsIntoTheStack(float[] operands) {
private void reinjectOperandsIntoTheStack(double[] operands) {
assert operands.length == 2;

for (int i = 0; i < 2; i++) {
stack.push(operands[1 - i]);
}
stack.push(operands[1]);
stack.push(operands[0]);
}

private float getOperationResult(OperationType operation, float[] operands) {
private double getOperationResult(OperationType operation, double[] operands) {
assert operands.length >= 2;

float a = operands[0];
float b = operands[1];
double a = operands[0];
double b = operands[1];

return operation.apply(a, b);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,21 @@ public enum FunctionType {
ARCTAN(Math::atan),
EXP(Math::exp);

private final Function<Float, Double> function;
private final Function<Double, Double> function;

FunctionType(Function<Float, Double> function) {
FunctionType(Function<Double, Double> function) {
this.function = function;
}

private static int computeBinaryLog(float number) {
private static int computeBinaryLog(double number) {
// https://stackoverflow.com/questions/3305059/how-do-you-calculate-log-base-2-in-java-for-integers
double logNumber = Math.log(number);
double log2 = Math.log(2);

return (int) (logNumber / log2 + 1e-10);
}

public double apply(float number) {
public double apply(double number) {
return function.apply(number);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,24 @@
import java.util.function.BiFunction;

public enum OperationType {
ADDITION(Float::sum),
ADDITION(Double::sum),
SUBSTRACTION((a, b) -> b - a),
MULTIPLICATION((a, b) -> b * a),
DIVISION((a, b) -> b / a),
MODULO((a, b) -> b % a),
POWER(OperationType::pow);

private final BiFunction<Float, Float, Float> operation;
private final BiFunction<Double, Double, Double> operation;

OperationType(BiFunction<Float, Float, Float> operation) {
OperationType(BiFunction<Double, Double, Double> operation) {
this.operation = operation;
}

private static float pow(float a, float b) {
return (float) Math.pow(b, a);
private static double pow(double a, double b) {
return Math.pow(b, a);
}

public float apply(float a, float b) {
public double apply(double a, double b) {
return operation.apply(a, b);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ public String parse(Map<String, String> data) throws IllegalStateException {
|| !viewContent.contains(" }}")) {
return viewContent;
}
checkViewValidity();
// TODO Fix the bad count bug
// checkViewValidity();
return fillView(data);
}

/*
private int countOccurrences(String regex) {
return view.toString().split(regex).length - 1;
}
Expand All @@ -60,6 +62,7 @@ private void checkViewValidity() throws IllegalStateException {
throw new IllegalStateException("View is invalid! Please fix it!");
}
}
*/

private String fillView(Map<String, String> data) {
String result = view.toString();
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/views/cli/help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Available commands :
- help : Show the list of available commands
- clear : Reset stack content
- pop : Remove last number from the stack
- infos : Display build infos
- quit : Allows to quit
Special features:
- put a $ before an operator to loop its execution (i.e. $+ will make sum of all the stack numbers)
12 changes: 12 additions & 0 deletions src/main/resources/views/cli/infos.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Build infos
------------
Version : {{ VERSION }}
Last update : {{ LAST_UPDATE }}
Revision (Commit hash) : {{ REVISION }}

Built by : {{ BUILT_BY }}
Build Timestamp : {{ TIMESTAMP }}

Compiled with : {{ COMPILED_WITH }}
Build JDK : {{ BUILD_JDK }}
Build OS : {{ BUILD_OS }}

0 comments on commit a04eb45

Please sign in to comment.