将操作数<T> 转换为整数
Converting Operand<T> to Integer
我正在使用自己实现的链表创建一个 Postfix 求值器方法,我真的卡在了这里的最后。我需要通过弹出 return 最终堆栈值。在我的方法中,我的 return 类型必须是整数。我尝试了一个解决方案,它告诉我 "cannot convert from Operand<Integer>
to Integer
"。我假设我需要以某种方式转换它,但我不知道该怎么做,任何帮助将不胜感激。
我找不到任何有用的信息,如果这是重复的,我深表歉意。
这是我的方法:
public class ArithPostfixEvaluator implements PostfixEvaluator<Integer> {
private final StackInterface<Operand<Integer>> stack;
/**
* Constructs an {@link ArithPostfixEvaluator}
*/
public ArithPostfixEvaluator(){
//Initialize the LinkedStack
stack = new LinkedStack<>();
}
public Integer evaluate(String expr) throws IllegalPostfixExpressionException {
ArithPostfixParser parser = new ArithPostfixParser(expr);
for (Token<Integer> token : parser) {
Type type = token.getType();
switch(type){
case OPERAND:
//What to do when we see an operand?
//create an operand variable
Operand<Integer> operand = token.getOperand();
//push the operand onto the stack
stack.push(operand);
break;
case OPERATOR:
//What to do when we see an operator?
//create an operator variable
Operator<Integer> operator = token.getOperator();
//make a new operand called result
Operand<Integer> result;
//pop 2 operands
Operand<Integer> op1 = stack.pop();
Operand<Integer> op2 = stack.pop();
//the first operand goes in the second position
operator.setOperand(2, op1);
operator.setOperand(1, op2);
//perform operation on result
result = operator.performOperation();
//push the result back onto the stack
stack.push(result);
break;
default:
throw new IllegalStateException("Parser returned an invalid Type: " + type);
}
}
// what to return?
///////////PROBLEM AREA////////////////
Integer Finalval = stack.pop();
//pop the remaining element on the stack
return Finalval ;
}
这是我的链表:
public class LinkedStack<T> implements StackInterface<T> {
private LLNode<T> head;
private int size;
public T pop() throws StackUnderflowException {
if(isEmpty()) throw new StackUnderflowException("Stack Underflow yo");
T temp = head.getData();
head = head.getNext();
return temp;
}
public T top() throws StackUnderflowException {
if(isEmpty()) throw new StackUnderflowException("Stack Underflow yo");
return head.getData();
}
public boolean isEmpty() {
return (head == null);
}
public int size() {
return size;
}
public void push(T elem) {
LLNode<T> newnode = new LLNode<T>(elem);
newnode.setNext(head);
head = newnode;
size++;
}
}
还有我的操作数和运算符类:
public class Operand<T> {
private final T value;
public Operand(T value){
this.value = value;
}
public T getValue(){
return value;
}
public String toString() {
return value.toString();
}
}
public interface Operator<T> {
public int getNumberOfArguments();
public Operand<T> performOperation();
public void setOperand(int position, Operand<T> operand);
}
private final StackInterface<Operand<Integer>> stack;
堆栈包含 Operand<Integer>
s,所以你不能这样做:
Integer Finalval = stack.pop();
它试图将 Operand<Integer>
存储在 Integer
变量中。你可以这样做:
Integer Finalval = stack.pop().getValue();
我正在使用自己实现的链表创建一个 Postfix 求值器方法,我真的卡在了这里的最后。我需要通过弹出 return 最终堆栈值。在我的方法中,我的 return 类型必须是整数。我尝试了一个解决方案,它告诉我 "cannot convert from Operand<Integer>
to Integer
"。我假设我需要以某种方式转换它,但我不知道该怎么做,任何帮助将不胜感激。
我找不到任何有用的信息,如果这是重复的,我深表歉意。
这是我的方法:
public class ArithPostfixEvaluator implements PostfixEvaluator<Integer> {
private final StackInterface<Operand<Integer>> stack;
/**
* Constructs an {@link ArithPostfixEvaluator}
*/
public ArithPostfixEvaluator(){
//Initialize the LinkedStack
stack = new LinkedStack<>();
}
public Integer evaluate(String expr) throws IllegalPostfixExpressionException {
ArithPostfixParser parser = new ArithPostfixParser(expr);
for (Token<Integer> token : parser) {
Type type = token.getType();
switch(type){
case OPERAND:
//What to do when we see an operand?
//create an operand variable
Operand<Integer> operand = token.getOperand();
//push the operand onto the stack
stack.push(operand);
break;
case OPERATOR:
//What to do when we see an operator?
//create an operator variable
Operator<Integer> operator = token.getOperator();
//make a new operand called result
Operand<Integer> result;
//pop 2 operands
Operand<Integer> op1 = stack.pop();
Operand<Integer> op2 = stack.pop();
//the first operand goes in the second position
operator.setOperand(2, op1);
operator.setOperand(1, op2);
//perform operation on result
result = operator.performOperation();
//push the result back onto the stack
stack.push(result);
break;
default:
throw new IllegalStateException("Parser returned an invalid Type: " + type);
}
}
// what to return?
///////////PROBLEM AREA////////////////
Integer Finalval = stack.pop();
//pop the remaining element on the stack
return Finalval ;
}
这是我的链表:
public class LinkedStack<T> implements StackInterface<T> {
private LLNode<T> head;
private int size;
public T pop() throws StackUnderflowException {
if(isEmpty()) throw new StackUnderflowException("Stack Underflow yo");
T temp = head.getData();
head = head.getNext();
return temp;
}
public T top() throws StackUnderflowException {
if(isEmpty()) throw new StackUnderflowException("Stack Underflow yo");
return head.getData();
}
public boolean isEmpty() {
return (head == null);
}
public int size() {
return size;
}
public void push(T elem) {
LLNode<T> newnode = new LLNode<T>(elem);
newnode.setNext(head);
head = newnode;
size++;
}
}
还有我的操作数和运算符类:
public class Operand<T> {
private final T value;
public Operand(T value){
this.value = value;
}
public T getValue(){
return value;
}
public String toString() {
return value.toString();
}
}
public interface Operator<T> {
public int getNumberOfArguments();
public Operand<T> performOperation();
public void setOperand(int position, Operand<T> operand);
}
private final StackInterface<Operand<Integer>> stack;
堆栈包含 Operand<Integer>
s,所以你不能这样做:
Integer Finalval = stack.pop();
它试图将 Operand<Integer>
存储在 Integer
变量中。你可以这样做:
Integer Finalval = stack.pop().getValue();