import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class PostfixTester
{
/**
* Reads and evaluates multiple postfix expressions.
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException{
String expression, again;
int result;
//Scanner in = new Scanner(System.in);
Scanner in = new Scanner(new File("test.txt"));
PostfixEvaluator evaluator = new PostfixEvaluator();
while(in.hasNext()){
expression = in.nextLine();
System.out.println(expression);
try{
result = evaluator.evaluate(expression);
System.out.println("The result is: " + result);
}
catch(EmptyCollectionException e){
e.getMessage();
}
catch(InvalidPostfixExpressionException e){
e.getMessage();
}
System.out.println();
}
}
}
import java.util.Stack;
import java.util.Scanner;
public class PostfixEvaluator
{
private final static char ADD = '+';
private final static char SUBTRACT = '-';
private final static char MULTIPLY = '*';
private final static char DIVIDE = '/';
private ArrayStack<Integer> stack;
/**
* Sets up this evalutor by creating a new stack.
*/
public PostfixEvaluator()
{
stack = new ArrayStack<Integer>();
}
/**
* Evaluates the specified postfix expression. If an operand is
* encountered, it is pushed onto the stack. If an operator is
* encountered, two operands are popped, the operation is
* evaluated, and the result is pushed onto the stack.
* @param expr string representation of a postfix expression
* @return value of the given expression
*/
public int evaluate(String expr)
{
int op1, op2, result = 0;
String token;
Scanner parser = new Scanner(expr);
while (parser.hasNext())
{
token = parser.next();
if (isOperator(token))
{
op2 = (stack.pop()).intValue();
op1 = (stack.pop()).intValue();
result = evaluateSingleOperator(token.charAt(0), op1, op2);
stack.push(new Integer(result));
}
else
stack.push(new Integer(Integer.parseInt(token)));
}
if(stack.size() != 1){
throw new InvalidPostfixExpressionException();
}
return result;
}
/**
* Determines if the specified token is an operator.
* @param token the token to be evaluated
* @return true if token is operator
*/
private boolean isOperator(String token)
{
return ( token.equals("+") || token.equals("-") ||
token.equals("*") || token.equals("/") );
}
/**
* Peforms integer evaluation on a single expression consisting of
* the specified operator and operands.
* @param operation operation to be performed
* @param op1 the first operand
* @param op2 the second operand
* @return value of the expression
*/
private int evaluateSingleOperator(char operation, int op1, int op2)
{
int result = 0;
switch (operation)
{
case ADD:
result = op1 + op2;
break;
case SUBTRACT:
result = op1 - op2;
break;
case MULTIPLY:
result = op1 * op2;
break;
case DIVIDE:
result = op1 / op2;
}
return result;
}
}
import java.util.Arrays;
public class ArrayStack<T> implements StackADT<T>
{
private final static int DEFAULT_CAPACITY = 100;
private int top;
private T[] stack;
/**
* Creates an empty stack using the default capacity.
*/
public ArrayStack()
{
this(DEFAULT_CAPACITY);
}
/**
* Creates an empty stack using the specified capacity.
* @param initialCapacity the initial size of the array
*/
public ArrayStack(int initialCapacity)
{
top = 0;
stack = (T[])(new Object[initialCapacity]);
}
/**
* Adds the specified element to the top of this stack, expanding
* the capacity of the array if necessary.
* @param element generic element to be pushed onto stack
*/
public void push(T element)
{
if (size() == stack.length)
expandCapacity();
stack[top] = element;
top++;
}
/**
* Creates a new array to store the contents of this stack with
* twice the capacity of the old one.
*/
private void expandCapacity()
{
stack = Arrays.copyOf(stack, stack.length * 2);
}
/**
* Removes the element at the top of this stack and returns a
* reference to it.
* @return element removed from top of stack
* @throws EmptyCollectionException if stack is empty
*/
public T pop() throws EmptyCollectionException
{
if (isEmpty())
throw new EmptyCollectionException("stack");
top--;
T result = stack[top];
stack[top] = null;
return result;
}
/**
* Returns a reference to the element at the top of this stack.
* The element is not removed from the stack.
* @return element on top of stack
* @throws EmptyCollectionException if stack is empty
*/
public T peek() throws EmptyCollectionException
{
if (isEmpty())
throw new EmptyCollectionException("stack");
return stack[top-1];
}
/**
* Returns true if this stack is empty and false otherwise.
* @return true if this stack is empty
*/
public boolean isEmpty()
{
// To be completed as a Programming Project
return top==0;
}
/**
* Returns the number of elements in this stack.
* @return the number of elements in the stack
*/
public int size()
{
// To be completed as a Programming Project
return top;
}
/**
* Returns a string representation of this stack.
* @return a string representation of the stack
*/
public String toString()
{
return stack.toString();
}
}
我的表达式输入文件(出于测试目的,最后两个应该抛出两个异常):
8 4 + 3 *
7 5 2 * +
3 1 + 4 2 - *
5 8 2 - +
5 8 - +
6 3 2 -
我的实际输出:
8 4 + 3 *
The result is: 36
7 5 2 * +
3 1 + 4 2 - *
5 8 2 - +
5 8 - +
6 3 2 -
显然,我希望前四个表达式像第一个表达式一样跟随,最后两个表达式显示我的异常消息,但我似乎不知道哪里出错了。
这是一个理论上的问题: 我必须计算一个表达式,我已经从中缀转换到后缀。后缀保存在中,因为我希望避免使用。这样我就知道数字之间的除法在哪里,我可以按“正确”的顺序访问它。 它看起来是这样的: 现在我想用两个堆栈: null 如果我到达一个运算符,并且数量计数至少为2,我将执行该操作并将其推到目标堆栈上。到达原始堆栈的末尾(现在是空的),我会把所有的东西都传递给它,然后从头开始,直到只剩下结果。 我现
我使用的是4.5.2.NET框架。有什么想法吗?
本文向大家介绍中缀表达式转后缀表达式相关面试题,主要包含被问及中缀表达式转后缀表达式时的应答技巧和注意事项,需要的朋友参考一下 参考回答: 对于中缀表达式,遇到操作数直接将其输出,如果遇到操作符和左括号全部压入栈中,若遇到右括号则将栈中元素全部弹出,直到遇到左括号为止。压栈过程中,若遇到其它操作符,从栈中弹出元素直到遇到更低优先级的操作符为止。
我正在编写一个程序,要求用户输入后缀表达式,然后将结果输出到表达式。我正试图使用一个单链表来实现这一点,并使用适配器模式来创建一个堆栈。 类、类和实现的代码都是直接从我自己的数据结构书中获得的。因此,类是唯一一个包含我自己的代码(并且有错误)的类。 我以前写过一个程序,它只使用堆栈计算后缀表达式,但这次我对包含的额外类感到困惑。 我肯定我有很多错误,但对我来说最明显的错误是在我的类中,每次我将一个
我想使用spring security 5.3 我的代码中有什么错误?
我最近一直在研究java中的树。我在sanfoundry上找到了这个代码。com,这对于表达式树来说是非常棒的。它使用前缀,然后打印出前缀表达式的中缀和后缀,最后打印出答案。我的问题是,我正试图找出如何将它简化为只接受后缀并打印出答案。因此,它不必读入前缀并进行所有这些操作,而是读入后缀并打印出答案。下面是我找到的代码。这是一个简单的修复,只是让它做后缀?还是更难的? 这是主要的方法。