2010-11-12 73 views
0

我正在编写一个解决Postfix问题的程序。因为我已经知道它,所以我不需要帮助。但是我偶然发现了替换所有函数的问题 在这个程序中,我们给出了应该定义的运算符;我将定义存储在地图中。 E表示这个问题是一个评估,D表示这个问题是一个定义。这些定义可以嵌套在一起。我尝试使用replaceAll函数检索定义时出现问题。它的作品除了一个例子。我会给输入文件和我的输出以显示我的意思。Java ReplaceAll问题

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

class Problem 
{ 

    private static String line; 
    private static HashMap<String, String> map = new HashMap(); 
    private static Stack operandStack = new Stack(); 

    public static void main(String[] args) throws IOException 
    { 

     FileReader fin = new FileReader("postfix.in"); 
     BufferedReader infile = new BufferedReader(fin); 

     FileWriter fout = new FileWriter("postfix.out"); 
     BufferedWriter outfile = new BufferedWriter(fout); 

     line = infile.readLine(); 
     do 
     { 

      if(line.substring(0,1).equals("E")) 
      { 

       line = line.replaceAll("E", ""); 
       line = line.replaceAll("\"+", ""); 
       for(int i = 0; i < line.length(); i++) 
       { 

        if(map.containsKey(line.substring(i,i+1)) || 
         line.substring(i, i+1).matches("!|-|!|%|/")) 
        { 
         //check to see if its not a predifined operator 
         if(!line.substring(i, i+1).matches("!|-|!|%|/")) 
         { 
          String operator; 

         operator = line.substring(i, i+1); 

         //simplifies operators 
         line = line.replaceAll("\\"+operator, map.get(operator)); 
         } 

        } 
        else if(!line.substring(i,i+1).equals(" ")) 
        { 

         operandStack.push(line.substring(i,i+1)); 
        } 

       } 

       System.out.println(line); 
       operandStack.clear(); 

      } 
      else if(line.substring(0,1).equals("D")) 
      { 
       line = line.replaceAll("\"$", "");  //remove quote at end of string 
       map.put(line.substring(1,2), line.substring(3)); //put the definition on the map 
      } 

     // System.out.println(map); 
      line = infile.readLine(); 
     }while(!line.equals("Q")); 

     infile.close(); 
     outfile.close(); 


    } 




} 

这里是输入文件

D+" 0%--" 
E"1 2+" 
D*" 1%//" 
E"3 4*" 
[email protected]"!!**" 
E"17 [email protected]+6*5/" 
D&"!*" 
D$" 1%/" 
Da"$/" 
D'" 0%-" 
E"28 5&'-$" 
E"3 4$/" 
E"3 4a" 
E"4 5a3/" 
E"[email protected]&" 
E"4&@" 
E"2!!!!****@" 
E"2&&&@" 
Q 

输出的代码

1 2 0%-- 
3 4 1%// 
17 4!! 1%// 1%// 0%--6 1%//5/ 
28 5! 1%// 0%-- 1%/ 
3 4 1%// 
3 4a  //this is not simplified 
4 5a3/ //this is not simpliied 
4!! 1%// 1%//! 1%// 
4! 1%//!! 1%// 1%// 
2!!!! 1%// 1%// 1%// 1%//!! 1%// 1%// 
2! 1%//! 1%//! 1%//!! 1%// 1%// 

我认为,要解决这个问题还是在于修复这条线的,但我不知道该怎么用它做。

line = line.replaceAll("\\"+operator, map.get(operator)); 

回答

1

看看是否有效。更换你的关注行:

line = line.replaceAll("\\"+operator, map.get(operator)); 

下列要求:

line = line.replaceAll(
      Pattern.quote(operator), 
      Matcher.quoteReplacement(map.get(operator))); 

它产生的输出:

1 2 0%-- 
3 4 1%// 
17 4!! 1%// 1%// 0%--6 1%//5/ 
28 5! 1%// 0%-- 1%/ 
3 4 1%// 
3 4$/ 
4 5$/3/ 
4!! 1%// 1%//! 1%// 
4! 1%//!! 1%// 1%// 
2!!!! 1%// 1%// 1%// 1%//!! 1%// 1%// 
2! 1%//! 1%//! 1%//!! 1%// 1%// 

这似乎不错,但我还没有广泛期待,看看它是否是正确的。

+0

为什么不只是'line = line.replace(operator,map.get(operator));'' – user102008 2011-06-20 09:48:19