Java中带堆栈的算术表达式求值
我想用java编写一个代码来计算表达式,比如括号和括号都是闭合的,但是我必须使用堆栈来完成。我已经创建了堆栈接口、StackEmptyException类和StackFullException类Java中带堆栈的算术表达式求值,java,math,stack,expression,Java,Math,Stack,Expression,我想用java编写一个代码来计算表达式,比如括号和括号都是闭合的,但是我必须使用堆栈来完成。我已经创建了堆栈接口、StackEmptyException类和StackFullException类 public interface Stack { public int size( ); // Returns the size of the Stack public boolean isEmpty( ); // Returns true if the Stack is empty public
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
使用用于解析中缀符号中指定的数学表达式
e、 g:
使用用于解析中缀符号中指定的数学表达式
e、 g:
也许我读错了,但所有这些都取决于您是如何实现堆栈的。您能定义您的输入是什么样的吗?我猜是类似于
(1+1)*2
,但仍然…例如:(2+1)*4*(3+1)是正确的,2+1)*2*(1+2)是错误的。我不知道如何处理堆栈。反向波兰符号应该更适合使用堆栈。否则,您需要表达式的语法才能对其求值。也许我读错了,但所有这些都取决于你是如何实现你的堆栈的。你能定义你的输入是什么样的吗?我猜是类似于(1+1)*2
,但仍然…例如:(2+1)*4*(3+1)是正确的,2+1)*2*(1+2)是错误的。我不知道如何处理堆栈。反向波兰符号应该更适合使用堆栈。否则,您需要表达式的语法才能对其求值。
(2+1)*4*(3+1)