【发布时间】:2015-06-26 15:25:29
【问题描述】:
我被分配编写一个类,该类采用中缀表示法的数学表达式并将该表达式转换为后缀表示法的等效表达式。那部分我已经完成了。
我还被分配编写一个迭代器,让客户端代码迭代后缀表达式的标记。
所以,到目前为止,我的迭代器代码是:
class PostfixIterator implements Iterator<String> { //this is line 117
private Deque<String> postfix;
public PostfixIterator(Deque<String> postfix) {
this.postfix = postfix;
}
public boolean hasNext() {
return postfix.isEmpty();
}
public String next() {
return postfix.pop(); //this is line 130
}
}
当我尝试创建迭代器的实例并调用其中一个方法时,我得到一个空指针异常,我不知道为什么。
这是我的主要外观:
public static void main(String[] args){
InfixToPostfix a = new InfixToPostfix("(123)^45+6*7/89");
Iterator itr = a.iterator();
System.out.println(itr.next());
}
根据我的编译器,return postfix.pop() 的计算结果为 null。我不知道为什么。
那么,有人可以帮我完成这项工作,并解释为什么我现在拥有的东西不起作用吗?
谢谢
这是我的整个 InfixToPost 修复类:
import java.util.*;
public class InfixToPostfix{
private Deque<String> postfix;
public InfixToPostfix(String infix){
Deque<String> postfix = new LinkedList<String>();
Deque<String> infixQ = new LinkedList<String>();
//tokenize the user input
int i = 0;
char ch;
infix = infix.replaceAll("\\s","");//make sure there is no whitespace
while(i < infix.length()){
ch = infix.charAt(i);
if(ch == '(' || ch == ')'|| ch == '+'|| ch == '-'
|| ch == '/' || ch == '%'|| ch == '*'
|| ch == '^'){
String s =ch+"";
infixQ.add(s);
i++;
}
else if (Character.isDigit(ch)){
String s ="";
int j = i;
char c = infix.charAt(j);
while(j <= infix.length()-1 && //accumulate the digits in that number
Character.isDigit(c = infix.charAt(j))){
s = s + c;
j++;
}
infixQ.add(s);
i=j;
}
else if (Character.isLetter(ch)){
String s ="";
int j = i;
char c = infix.charAt(j);
while(j <= infix.length()-1 && //accumulate the lettes in that variable
Character.isLetter(c = infix.charAt(j))){
s = s + c;
j++;
}
infixQ.add(s);
i=j;
}
}
System.out.println(infixQ);
//start shunting-yard
Deque<String> stack = new ArrayDeque<String>();
Iterator<String> itr = infixQ.iterator();
while(itr.hasNext()){
String s = itr.next();
//if token is number or a variable, put it on the output queue
if(Character.isDigit(s.charAt(0))
|| Character.isLetter(s.charAt(0))){
postfix.add(s);
}
if(s.equals("(")){
stack.push(s);
}
if(s.equals(")")){
while((!stack.isEmpty())&&(!stack.peek().equals("("))){
postfix.add(stack.pop());
}
stack.pop();
}
if(s.equals("+") || s.equals("-")){
while((!stack.isEmpty()) && (stack.peek().equals("+")
|| stack.peek().equals("-")
|| stack.peek().equals("*") || stack.peek().equals("/")
|| stack.peek().equals("^"))){
postfix.add(stack.pop());
}
stack.push(s);
}
if(s.equals("*") || s.equals("/") || s.equals("%")){
if(!stack.isEmpty()){
while((!stack.isEmpty())&&(stack.peek().equals("*")
|| stack.peek().equals("/")
|| stack.peek().equals("%")
|| stack.peek().equals("^"))){
postfix.add(stack.pop());
}
}
stack.push(s);
}
if(s.equals("^")){
stack.push(s);
}
}
while(!stack.isEmpty()){
postfix.add(stack.pop());
}
System.out.println(stack.isEmpty());
System.out.println(postfix);
}
public Iterator<String> iterator(){
return new PostfixIterator(postfix);
}
public static void main(String[] args){
InfixToPostfix a = new InfixToPostfix("(123)^45+6*7/89");
Iterator itr = a.iterator();
System.out.println(itr.next()); // this is line 112
}
}
我很肯定它写得不好。不过,我只需要它工作,这样我就可以把它上交。
这是我的堆栈跟踪:
Exception in thread "main" java.lang.NullPointerException
at PostfixIterator.next(InfixToPostfix.java:130)
at PostfixIterator.next(InfixToPostfix.java:117)
at InfixToPostfix.main(InfixToPostfix.java:112)
【问题讨论】:
-
你的 InfixToPostfix 类在哪里?我不明白 PostfixIterator 将如何迭代 InfixToPostfix?!!!您需要发布更多信息
-
如果 postfix.pop() 返回 null 那么它将被打印为 null,只是因为 "itr" 本身为 null 就会发生异常。请验证这一点。
-
你能发布你的 InfixToPostfix 课程吗?做 a.iterator();正确返回您的 PostfixIterator 类?
-
现在你需要发布你的堆栈跟踪 plz
-
发布了我的堆栈跟踪。