0

後置の問題を解決するプログラムを書いています。私はすでにそれを知っているので、アルゴリズムの助けは必要ありません。しかし、replace all 関数の問題に出くわしました。このプログラムでは、定義する必要がある演算子が与えられています。定義をマップに保存しました。E はこの問題が評価であることを意味し、D はこの問題が定義であることを意味します。定義は、互いに入れ子にすることができます。私の問題は、replaceAll 関数を使用して定義を取得しようとしたときに発生します。1つのインスタンスを除いて機能します。私が何を意味するかを示すために、入力ファイルと出力を示します。

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*"
D@"!!**"
E"17 4@+6*5/"
D&"!*"
D$" 1%/"
Da"$/"
D'" 0%-"
E"28 5&'-$"
E"3 4$/"
E"3 4a"
E"4 5a3/"
E"4@&"
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));
4

1 に答える 1

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%//

これは問題ないようですが、それが正しいかどうかを詳しく調べていません。

于 2010-11-12T06:12:52.523 に答える