Skip to content

Commit

Permalink
issue #55: handle []-brackets in formulas
Browse files Browse the repository at this point in the history
  • Loading branch information
siggel committed Dec 11, 2020
1 parent 0667ab3 commit bb64171
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 3 deletions.
5 changes: 5 additions & 0 deletions app/src/main/assets/changes_version_24_de.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<p>Version 1.2.24:</p>
<ul>
<li>Neu: []-Klammern in Formeln
</li>
</ul>
5 changes: 5 additions & 0 deletions app/src/main/assets/changes_version_24_en.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<p>Version 1.2.24:</p>
<ul>
<li>New: Handle []-brackets in formulas
</li>
</ul>
2 changes: 1 addition & 1 deletion app/src/main/assets/terms_of_service_de.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</head>

<body>
<p> Copyright (c) 2018 by siggel &lt;siggel-apps@gmx.de&gt;</p>
<p> Copyright (c) 2018-2020 by siggel &lt;siggel-apps@gmx.de&gt;</p>
<p> Dieses Programm ist freie Software. Sie können es unter den Bedingungen der GNU General Public
License, wie von der Free Software Foundation veröffentlicht, weitergeben und/oder modifizieren,
entweder gemäß Version 3 der Lizenz oder (nach Ihrer Wahl) jeder späteren Version.</p>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/assets/terms_of_service_en.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</head>

<body>
<p> Copyright (c) 2018 by siggel &lt;siggel-apps@gmx.de&gt;</p>
<p> Copyright (c) 2018-2020 by siggel &lt;siggel-apps@gmx.de&gt;</p>
<p> Coordinate Joker is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ class Calculator {
* @return calculated result
*/
static double evaluate(String formula, Integer x, Integer y) {
formula = simplify(formula);
// Check if there are any parenthesis. If so then evaluate it first and
// replace it with the value.
formula = formula.replace(" ", "");
int openIndex = findOpeningParenthesis(formula);
int closeIndex = findCosingParenthesis(formula, openIndex);
while (openIndex != -1 && closeIndex != -1 && openIndex < closeIndex) {
Expand Down Expand Up @@ -105,6 +105,21 @@ static double evaluate(String formula, Integer x, Integer y) {
.evaluate();
}

/**
* simplify formula string so calculation can start
*
* @param formula the coordinate formula before simplification
* @return the coordinate formula after simplification
*/
private static String simplify(String formula) {
// get rid of whitespace
formula = formula.replace(" ", "");
// handle []-brackets like ()-brackets
formula = formula.replace("[", "(");
formula = formula.replace("]", ")");
return formula;
}

private static int findOpeningParenthesis(final String text) {
return findOpeningParenthesis(text, 0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public void multipleParenthesesWithProduct() {
public void nestedParentheses() {
assertEquals(42.0, Calculator.evaluate("(x+(x+y)) (x+x)", 1, 2));
assertEquals(42.0, Calculator.evaluate("(x+(x+y))(x+x)", 1, 2));
assertEquals(42.0, Calculator.evaluate("(x+[x+y])[x+x]", 1, 2));
}

@Test
Expand Down

0 comments on commit bb64171

Please sign in to comment.