2012-03-01 24 views
1

我应该编写一个程序来将中缀转换为后缀。它适用于某些,但其他时间不正确。特别是在包含派生词的中缀表达式上。谁能给我一个线索,为什么这是错的?例如,中缀表达中缀后缀程序不能正常工作

((5 + 5 * (6 - 2) + 4^2) * 8) 

返回5562-*42^++8*((2

import java.io.*; 
import java.util.Scanner; 

public class InfixToPostfix 
{ 
    //class attributes 
    private char curValue; 
    private String postfix; 
    private LineWriter lw; 
    private ObjectStack os; 

    //constructor 
    public InfixToPostfix(LineWriter l, ObjectStack o) 
    { 
    curValue = ' '; 
    lw=l; 
    os=o; 
    } 

    public String conversion(String buf) 
    { 
    String temp =" "; 
    StringBuffer postfixStrBuf= new StringBuffer(temp); 
    char popped= new Character(' '); 
    char topped=' '; 

    for (int i=0; i<buf.length(); i++) 
    { 
     curValue= buf.charAt(i); 

     if (curValue == '(') 
     os.push(curValue); 

     if (curValue == ')') 
     { 
     while (popped != '(') 
     { 
      popped = ((Character)os.pop()); 
      if (popped != '(') 
      postfixStrBuf.append(popped); 
     } 
     } 

     if (isOperator(curValue)) 
     { 
     if(os.isEmpty()) 
      os.push((Character)(curValue)); 
     else 
      topped=((Character)os.top()); 

     if ((priority(topped)) >= (priority(curValue)) && (topped != ' ')) 
     { 
      popped = ((Character)os.pop()); 
      if (popped != '(') 
      postfixStrBuf.append(popped); 
      //if it is a left paranthess, we want to go ahead and push it anyways 
      os.push((Character)(curValue)); 
     } 

     if ((priority(topped)) < (priority(curValue)) && (topped != ' ')) 
      os.push((Character)(curValue)); 
     } 

     else if (!isOperator(curValue) && (curValue != ' ') && (curValue != '(') && (curValue != ')')) 
     postfixStrBuf.append(curValue); 
    } 

    //before you grab the next line of the file , pop off whatever is remaining off the stack and append it to 
    //the infix expression 

    getRemainingOp(postfixStrBuf); 

    return postfix; 

    //postfixStrBuf.delete(0, postfixStrBuf.length()); 
    } 

    public int priority(char curValue) 
    { 
    switch (curValue) 
    { 
     case '^': return 3; 
     case '*': 
     case '/': return 2; 
     case '+': 
     case '-': return 1; 
     default : return 0; 
    } 
    } 

    public boolean isOperator(char curValue) 
    { 
    boolean operator = false; 
    if ((curValue == '^') || (curValue == '*') || (curValue == '/') || (curValue == '+') || (curValue == '-')) 
     operator = true; 
    return operator; 
    } 

    public String getRemainingOp(StringBuffer postfixStrBuf) 
    { 
    char popped=' '; 
    while (!(os.isEmpty())) 
    { 
     opped = ((Character)os.pop()); 
     postfixStrBuf.append(popped); 
    } 
    postfix=postfixStrBuf.toString(); 
    return postfix; 
    } 
} 
+0

读你的代码我不想回答你的问题。 – Alexander 2012-03-01 05:03:58

+0

这是因为答案是显而易见的,还是因为它没有意义? – user1175955 2012-03-01 05:06:35

回答

0

我只会发布内环应该怎么样子(不铸件无处不):

if (curValue == '(') { 
    os.push(curValue); 
} else if (curValue == ')') { 
    if(!os.isEmpty()) { 
     topped = os.pop(); 
     while (!os.isEmpty() && (topped != '(')) { 
      postfixStrBuf.append(topped); 
      topped = os.pop(); 
     } 
    } 
} else if (isOperator(curValue)) { 
    if (os.isEmpty()) { 
     os.push(curValue); 
    } else { 
     while(!os.isEmpty() && (priority(os.top()) >= priority(curValue))) { 
      popped = os.pop(); 
      postfixStrBuf.append(popped); 
     } 
     os.push(curValue); 
    } 
} else if (curValue != ' ') { 
    postfixStrBuf.append(curValue); 
} 

披露:这是很为时已晚,所以我希望它是好的。您应该修正变量初始化的方式并返回getRemainingOp方法。

+0

非常感谢亚历山大,我非常感谢。我将在getRemainingOp – user1175955 2012-03-01 06:06:48

+0

@ user1175955上工作,然后将其标记为答案。 – Alexander 2012-03-01 14:42:32