Last active
July 15, 2020 11:41
-
-
Save maniish-jaiin/a4ad48c411db64e87541a419edaaf5df to your computer and use it in GitHub Desktop.
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)); | |
} | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment