I want to write a code in java that evaluates an expression, like if the parentheses are closed as well as the brackets etc., but I must do it using a stack. I already have made the Stack interface, StackEmptyException class and StackFullException class.
public interface Stack
{
public int size( );
// Returns the size of the Stack
public boolean isEmpty( );
// Returns true if the Stack is empty
public boolean isFull( );
// Returns true if the Stack is full
public Object top( ) throws StackEmptyException;
// Returns the top item of the Stack
public void push(Object item) throws StackFullException;
// Adds a new item into the Stack
public Object pop( ) throws StackEmptyException;
// Removes the top item of the Stack
}//End of interface Stack
Use the the Shunting-yard algorithm for parsing mathematical expressions specified in infix notation .
eg:
(2+1)*4*(3+1)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.