From 571f47841621330635cb89e883d24ee2c6da94f8 Mon Sep 17 00:00:00 2001 From: yuryver Date: Tue, 22 Aug 2017 13:46:01 -0400 Subject: [PATCH] Reverse polish notation initial --- .../org/fundacionjala/coding/yury/Calc.java | 112 ++++++++++++++++++ .../fundacionjala/coding/yury/CalcTest.java | 69 +++++++++++ 2 files changed, 181 insertions(+) create mode 100644 src/main/java/org/fundacionjala/coding/yury/Calc.java create mode 100644 src/test/java/org/fundacionjala/coding/yury/CalcTest.java diff --git a/src/main/java/org/fundacionjala/coding/yury/Calc.java b/src/main/java/org/fundacionjala/coding/yury/Calc.java new file mode 100644 index 0000000..eff0f17 --- /dev/null +++ b/src/main/java/org/fundacionjala/coding/yury/Calc.java @@ -0,0 +1,112 @@ +package org.fundacionjala.coding.yury; + +import java.util.LinkedList; + +/** + * Created by JRPNHP48 Team. + */ + +public final class Calc { + private int contDigit = -1; + private int contOperator = 0; + private double num1; + private double num2; + private LinkedList copy; + + /** + * This constructor. + */ + public Calc() { + copy = new LinkedList(); + } + + /** + * This method evaluated expression. + * + * @param rpn operation. + * @return result of operation. + */ + public double evaluate(String rpn) { + if (rpn.isEmpty()) { + return 0; + } + String[] token = rpn.split(" "); + for (int i = 0; i < token.length; i++) { + switch (token[i]) { + case "+": + changeVariable(); + copy.add((String.valueOf(num2 + num1))); + break; + case "-": + changeVariable(); + copy.add((String.valueOf(num2 - num1))); + break; + case "*": + changeVariable(); + copy.add((String.valueOf(num2 * num1))); + break; + case "/": + changeVariable(); + copy.add((String.valueOf(num2 / num1))); + break; + default: + copy.add(token[i]); + break; + } + } + return !isValidRpnExpression(rpn) ? Double.parseDouble(copy.getLast()) : Double.parseDouble(copy.get(0)); + } + + /** + * this method change the variable format. + */ + private void changeVariable() { + num1 = Double.parseDouble(copy.pollLast()); + num2 = Double.parseDouble(copy.pollLast()); + } + + /** + * This method validate expression. + * + * @param rpn operation. + * @return if expresion is validate. + */ + public boolean isValidRpnExpression(String rpn) { + for (String token : rpn.split(" ")) { + if (isNumeric(token)) { + contDigit++; + } else if (isValidOperator(token)) { + contOperator++; + if (contOperator > contDigit) { + return false; + } + } else { + return false; + } + } + return contOperator == contDigit; + } + + /** + * @param string operator. + * @return boolean. + */ + private static boolean isValidOperator(String string) { + return string.equals("+") || string.equals("-") || string.equals("*") || string.equals("/"); + } + + /** + * this method verify that a number is digit. + * + * @param string for evaluate. + * @return boolean. + */ + private static boolean isNumeric(String string) { + try { + Integer.parseInt(string); + return true; + } catch (NumberFormatException e) { + return false; + } + } +} diff --git a/src/test/java/org/fundacionjala/coding/yury/CalcTest.java b/src/test/java/org/fundacionjala/coding/yury/CalcTest.java new file mode 100644 index 0000000..d99121a --- /dev/null +++ b/src/test/java/org/fundacionjala/coding/yury/CalcTest.java @@ -0,0 +1,69 @@ +package org.fundacionjala.coding.yury; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * Test. + */ +public class CalcTest { + + private Calc calc = new Calc(); + + /** + * Test should Work With Empty String. + */ + @Test + public void shouldWorkWithEmptyString() { + assertEquals("Should work with empty string", 0, calc.evaluate(""), 0); + } + + /** + * Test should Parse Numbers. + */ + @Test + public void shouldParseNumbers() { + assertEquals("Should parse numbers", 3, calc.evaluate("1 2 3"), 0); + } + + /** + * Test should Parse Float Numbers. + */ + @Test + public void shouldParseFloatNumbers() { + assertEquals("Should parse float numbers", 3.5, calc.evaluate("1 2 3.5"), 0); + } + + /** + * Test should Support Addition. + */ + @Test + public void shouldSupportAddition() { + assertEquals("Should support addition", 4, calc.evaluate("1 3 +"), 0); + } + + /** + * Test should Support Multiplication. + */ + @Test + public void shouldSupportMultiplication() { + assertEquals("Should support multiplication", 3, calc.evaluate("1 3 *"), 0); + } + + /** + * Test should Support Substraction. + */ + @Test + public void shouldSupportSubstraction() { + assertEquals("Should support substraction", -2, calc.evaluate("1 3 -"), 0); + } + + /** + * Test should Support Division. + */ + @Test + public void shouldSupportDivision() { + assertEquals("Should support division", 2, calc.evaluate("4 2 /"), 0); + } +}