postfix 2 infix

This commit is contained in:
rusefi 2017-03-07 22:25:29 -05:00
parent e563021963
commit bf837065ab
2 changed files with 51 additions and 3 deletions

View File

@ -1,11 +1,15 @@
package com.fathzer.soft.javaluator;
import java.util.ArrayList;
import java.util.List;
/** An <a href="http://en.wikipedia.org/wiki/Operator_(mathematics)">operator</a>.
* @author Jean-Marc Astesana
* @see <a href="../../../license.html">License information</a>
*/
public class Operator {
public final String rpnSymbol;
public static final List<String> _2_OPERATORS = new ArrayList<>();
/** An Operator's <a href="http://en.wikipedia.org/wiki/Operator_associativity">associativity</a>.
*/
@ -54,6 +58,9 @@ public class Operator {
this.operandCount = operandCount;
this.associativity = associativity;
this.precedence = precedence;
if (operandCount == 2) {
_2_OPERATORS.add(getRpnSymbol());
}
}
/** Gets the operator's symbol.

View File

@ -1,19 +1,23 @@
package com.rusefi.test;
import com.fathzer.soft.javaluator.DoubleEvaluator;
import com.fathzer.soft.javaluator.Operator;
import org.junit.Test;
import java.text.ParseException;
import java.util.Stack;
import static org.junit.Assert.assertEquals;
public class ParserTest {
@Test
public void testBooleanConversion() throws ParseException {
assertParse("1 2 + 3 -", "1 + 2 - 3");
assertParse("1 2 | 3 |", "1 | 2 | 3");
assertParseB("1 2 + 3 -", "1 + 2 - 3");
assertParseB("1 2 | 3 |", "1 | 2 | 3");
assertParse("2 1 >", "2 > 1");
assertParseB("1 2 + 3 4 5 + / +", "1 + 2 + 3 / (4 +5 )");
assertParseB("2 1 >", "2 > 1");
assertParse("rpm 0 >", "rpm > false");
assertParse("rpm 0 >", "(rpm > false)");
assertParse("rpm user0 > clt user2 > |", "(rpm > user0) or (clt > user2)");
@ -21,6 +25,18 @@ public class ParserTest {
assertParse("rpm user0 > clt user2 > | vbatt user1 > |", "(rpm > user0) or (clt > user2) or (vbatt > user1)");
}
private void assertParseB(String rpn, String expression) {
assertParse(rpn, expression);
String h = getInfix(rpn);
System.out.println(h);
assertEquals(getReplace(expression), getReplace(h));
}
private String getReplace(String h) {
return h.replaceAll("[\\(\\)]", "").replace(" ", "");
}
private void assertParse(String rpn, String expression) {
DoubleEvaluator evaluator = new DoubleEvaluator();
evaluator.evaluate(expression.toLowerCase());
@ -28,6 +44,31 @@ public class ParserTest {
assertEquals(rpn, evaluator.getRusEfi());
}
private String getInfix(String rpn) {
String tokens[] = rpn.split(" ");
Stack<String> stack = new Stack<>();
for (String token : tokens) {
if (takesTwoParams(token)) {
if (stack.size() < 2)
throw new IllegalStateException("Not enough " + token);
String a = stack.pop();
String b = stack.pop();
stack.push("(" + b + " " + token + " " + a + ")");
} else {
stack.push(token);
}
}
if (stack.size() != 1)
throw new IllegalStateException("Unexpected: " + stack);
return stack.pop();
}
private boolean takesTwoParams(String token) {
return Operator._2_OPERATORS.contains(token);
}
@Test
public void test() {