"evaluate reverse polish notation gfg" Code Answer's

You're definitely familiar with the best coding language C++ that developers use to develop their projects and they get all their queries like "evaluate reverse polish notation gfg" answered properly. Developers are finding an appropriate answer about evaluate reverse polish notation gfg related to the C++ coding language. By visiting this online portal developers get answers concerning C++ codes question like evaluate reverse polish notation gfg. Enter your desired code related query in the search bar and get every piece of information about C++ code related question on evaluate reverse polish notation gfg. 

evaluate reverse polish notation gfg

By Odd OstrichOdd Ostrich on Aug 15, 2020
public class Test {
 
	public static void main(String[] args) throws IOException {
		String[] tokens = new String[] { "2", "1", "+", "3", "*" };
		System.out.println(evalRPN(tokens));
	}
 
	public static int evalRPN(String[] tokens) {
		int returnValue = 0;
		String operators = "+-*/";
 
		Stack<String> stack = new Stack<String>();
 
		for (String t : tokens) {
			if (!operators.contains(t)) { //push to stack if it is a number
				stack.push(t);
			} else {//pop numbers from stack if it is an operator
				int a = Integer.valueOf(stack.pop());
				int b = Integer.valueOf(stack.pop());
				switch (t) {
				case "+":
					stack.push(String.valueOf(a + b));
					break;
				case "-":
					stack.push(String.valueOf(b - a));
					break;
				case "*":
					stack.push(String.valueOf(a * b));
					break;
				case "/":
					stack.push(String.valueOf(b / a));
					break;
				}
			}
		}
 
		returnValue = Integer.valueOf(stack.pop());
 
		return returnValue;
	}
}

Source: www.programcreek.com

Add Comment

0

evaluate reverse polish notation gfg

By Odd OstrichOdd Ostrich on Aug 15, 2020
  ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9
  ["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6

Source: www.programcreek.com

Add Comment

0

All those coders who are working on the C++ based application and are stuck on evaluate reverse polish notation gfg can get a collection of related answers to their query. Programmers need to enter their query on evaluate reverse polish notation gfg related to C++ code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about evaluate reverse polish notation gfg for the programmers working on C++ code while coding their module. Coders are also allowed to rectify already present answers of evaluate reverse polish notation gfg while working on the C++ language code. Developers can add up suggestions if they deem fit any other answer relating to "evaluate reverse polish notation gfg". Visit this developer's friendly online web community, CodeProZone, and get your queries like evaluate reverse polish notation gfg resolved professionally and stay updated to the latest C++ updates. 

C++ answers related to "evaluate reverse polish notation gfg"

View All C++ queries

C++ queries related to "evaluate reverse polish notation gfg"

Browse Other Code Languages

CodeProZone