Dijkstra's Two-stack Algorithm for Expression Evaluation(Java)

tags: stack  Algorithms  java  Expression evaluation

Use stack to calculate the input formula (there is a space between each operator and number)

import java.util.Scanner;
import java.util.LinkedList;
import java.util.Queue;

import edu.princeton.cs.algs4.Stack;

public class Evaluate {
	public static void main(String[] args){
		Stack<String> ops=new Stack<String>();
		Stack<Double> values=new Stack<Double>();
		Scanner sc=new Scanner(System.in);
		String next=sc.nextLine();
		String[] expression=next.split("\\s+");
		Queue<String> que=new LinkedList<String>();
		for (String x : expression)
			que.add(x);
		while(!que.isEmpty()){
			//push if operator
			String s=que.poll();
			if(s.equals("("))	;
			else if(s.equals("+"))	ops.push(s);
			else if(s.equals("-"))	ops.push(s);
			else if(s.equals("*"))	ops.push(s);
			else if(s.equals("/"))	ops.push(s);
			else if(s.equals("sqrt"))	ops.push(s);
			else if(s.equals(")")){
			//pop,evaluate and push result if token is")"
				String op=ops.pop();
				double v=values.pop();
				if(op.equals("+"))	v=values.pop()+v;
				else if(op.equals("-"))v=values.pop()-v;
				else if(op.equals("*"))v=values.pop()*v;
				else if(op.equals("/"))v=values.pop()/v;
				else if(op.equals("sqrt"))v=Math.sqrt(v);					
				values.push(v);
			}
			else values.push(Double.parseDouble(s));
		}
		System.out.print(values.pop());
	}
}

input:      ( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
output:    101.0

Intelligent Recommendation

Dijkstra's dual-stack arithmetic expression evaluation algorithm ("Algorithm" fourth edition notes)

The file name is Evaluate.java Algorithm ideas: 1. Use two stacks to store operators and operands respectively; 2. Push the operator onto the stack ops and push the operand onto the stack vals; 3. Ign...

Algorithm fourth edition P80 Dijkstra's double-stack arithmetic expression evaluation algorithm

purpose According to the book’s algorithm, I understand myself laterSilent writingAlgorithm java code result Experience 1. Use two stacks to store different data separately: one stores numbers, ...

"Algorithms" (4th Edition) study notes-Dijkstra's double-stack arithmetic expression evaluation algorithm

Arithmetic expression evaluation For calculations such as (1+((2+3)*(4+5))), we need to implement arithmetic expressions through the stack. Here we define arithmetic expressions without parentheses. T...

Simple expression evaluation (Java, Stack)

Simple string expressions (Java, Stack) Learn from Leetcode1006[Palace Water 3 Leaves] QuestionMore detailed ideas can look at his inscription Implementing the stack by Deque Define operator priority ...

Dijkstra double-stack arithmetic expression evaluation algorithm----Java double-stack method for the value of arithmetic expression

1. First download two jar libraries: stdlib.jar and algs4.jar package click to download 2. Import these two packages into your own Eclipse: The steps are as follows: 1. Right click on the project you ...

More Recommendation

C++ stack application (two): postfix expression evaluation

In daily life, most of the arithmetic expressions we come into contact with are infix expressions, and their general form is: 5+6/2-3*4 means that the operator is located in the middle of the two oper...

Dual-stack arithmetic expression evaluation algorithm

Dual-stack arithmetic expression evaluation algorithm Stack is a stack-based policy-last-out (LIFO) type of collection. Arithmetic expression for example, (1 + (5 - 2) * (3 + 6)) Algorithm requires de...

Dijkstra dual-stack expression evaluation algorithm

Take the expression "(1 + ((2 + 3) * (4 * 5)))" as an example: The algorithm is divided into four steps: Push the operand onto the operand stack Push the operator onto the operator stack Ign...

Dijkstra dual-stack arithmetic expression evaluation algorithm

A code 2 test Three evaluation trajectories Four code reference https://gitee.com/cakin24/Algorithm...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top