This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public double evaluate(String s) { | |
String[] elements = getStringArray(s); | |
Stack<Double> numbers = new Stack(); | |
double result = 0.0; | |
for (String c : elements) { | |
switch (c) { | |
case "+": { | |
result = numbers.push(numbers.pop() + numbers.pop()); | |
break; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
public void evaluateWhenAllOperatorAreFound() { | |
ReversePolishNotation rpn = new ReversePolishNotation(); | |
double result = rpn.evaluate("10 6 9 3 + -11 * / * 17 + 5 +"); | |
assertEquals(21.5, result, 0.1); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public double evaluate(String s) { | |
String[] elements = getStringArray(s); | |
Stack<Double> numbers = new Stack(); | |
double result = 0.0; | |
for (String c : elements) { | |
switch (c) { | |
case "+": | |
result = numbers.push(numbers.pop() + numbers.pop()); | |
break; | |
case "-": |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
public void addNumbersWhenMinusOperatorIsFound() { | |
ReversePolishNotation rpn = new ReversePolishNotation(); | |
double result = rpn.evaluate("5 5 2 + -"); | |
assertEquals(2.0, result, 0.1); | |
} | |
@Test | |
public void addNumbersWhenMultiplyOperatorIsFound() { | |
ReversePolishNotation rpn = new ReversePolishNotation(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public double evaluate(String s) { | |
String[] elements = getStringArray(s); | |
Stack<Double> numbers = new Stack(); | |
double result = 0.0; | |
for (int i = 0; i < elements.length; i++) { | |
String c = elements[i]; | |
if (c.equals("+") && i >= 2) { | |
result = numbers.push(numbers.pop() + numbers.pop()); | |
} else { | |
numbers.push(Double.parseDouble(c)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
public void addNumbersWhenMultiplePlusOperatorIsFound() { | |
ReversePolishNotation rpn = new ReversePolishNotation(); | |
double result = rpn.evaluate("5 5 2 + +"); | |
assertEquals(12.0, result, 0.1); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public double evaluate(String s) { | |
String[] elements = getStringArray(s); | |
double result = 0.0; | |
for (int i = 0; i < elements.length; i++) { | |
String c = elements[i]; | |
if (c.equals("+") && i >= 2) { | |
result = parseDouble(elements[i - 1]) + parseDouble(elements[i - 2]); | |
} | |
} | |
return result; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
public void addNumbersWhenPlusOperatorIsFound() { | |
ReversePolishNotation rpn = new ReversePolishNotation(); | |
double result = rpn.evaluate("3 4 +"); | |
assertEquals(7.0, result, 0.1); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void addNumbersWhenPlusOperatorIsFound() { | |
ReversePolishNotation rpn = new ReversePolishNotation(); | |
double result = rpn.evaluate("5 5 +"); | |
assertEquals(10.0, result, 0.1); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public String[] getStringArray(String s) { | |
return s.split(" "); | |
} |
NewerOlder