0

重複の可能性:
区切り文字を除外しながら、2 つの文字の間に含まれる文字列を検索する正規表現

外部ライブラリ「beanshell」の Interpreter.eval() というメソッドを使用しています。これにより、文字列を使用して数学演算を行い、解を返すことができます。

正常に動作しますが、2 乗したい場合、またはこれから平方根を取得したい場合は、元の文字列を次のように変更する必要があります。

文字列 st ="x = 2+4*a*(b+c)^2"

"(b+c)^2""(b+c)*(b+c)"または"Math.pow((b+c),2)"に置き換えるには、括弧の間の文字を取得する必要があります。

これどうやってするの?前もって感謝します!

- - 編集 - -

最後に、解決策を見つけました。

    Interpreter interpreter = new Interpreter();
    String st = "x = 2+4*1*(2*(1+1)^2)^2 + (4+5)^2";

    int index = -2;
    char prev;
    char post;
    int openPar = -1;
    if (st.contains("^")) {

        index = st.indexOf("^");

        while (index != -1) {

            prev = st.charAt(index - 1);
            post = st.charAt(index + 1);

            if (prev == ')') {
                int match = 0;
                int i = index - 2;
                char check = '0';

                boolean contiunar = true;
                while (contiunar) {
                    check = st.charAt(i);

                    if (check == ')')
                        match++;

                    if (check == '(') {

                        if (match == 0) {

                            openPar = i;
                            contiunar = false;
                        } else
                            match = match - 1;
                    }

                    i = i - 1;

                }

                String rep = st.substring(openPar + 1, index - 1);
                String resultado = "";
                try {
                    interpreter.eval("r= Math.pow(" + rep + "," + post
                            + ")");
                    resultado = interpreter.get("r").toString();

                } catch (EvalError e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                st = st.replace("(" + rep + ")^" + post, resultado);

            } else {
                st = st.replace(prev + "^" + post, prev + "*" + prev);
            }

            index = st.indexOf("^");

        }

    }

これで、元の文字列x = 2+4*1*(2*(1+1)^2)^2+(4+5)^2を変更 しました (たとえば)

x=2+4*1*64+81

  1. 最初に最初の「^」を検索します
  2. 前の文字を取得する
  3. ")" がある場合
  4. 前の文字を検索しながら "(" を検索しますが、"(" の前に ")" を検索する場合は、2 つの "(" を見つける必要があります。

これは "(2+(3+4)+5)^2" の場合です ---> "3+4)+5" の代わりに "2+(3+4)+5" を返します。

ここで正しい式Math.pow("result","2")" に置き換え、ステップごとに計算します (1+1)^2 = 4 (2*4)^2 = 64

(4+5)^2 = 81

最後に、Interpreter.eval() で戻り値を計算できるようになりました。

答えてくれてありがとう!

4

3 に答える 3

1

次のコードを試してタスクを実行してください

String originalString ="x = 2+4*a*(b+c)^2";
String stringToInsert = "Math.pow((b+c),2)";

int startIndex = originalString.indexOf("(");
int endIndex = originalString.indexOf(")");

String firstPortion = originalString.substring(0,startIndex);
String lastPortion = originalString.substring(endIndex+1);
String finalString = firstPortion + stringToInsert + lastPortion;

System.out.println("finalString : "+finalString);
于 2012-07-16T11:44:11.757 に答える
0

You will need to parse the string. You could write a complete parser and go through it character by character.

Otherwise, if there is a specific flowchart you have in mind (using the operators), use the String.split(\"*(...) functionality where you can essentially create tokens based on whatever your delimeters are.

Hope this helps.

于 2012-07-16T09:35:27.067 に答える
0

もちろん、文字列を解析する必要があります。中間状態に変換できます (非常に迅速に実行できます)。次のリンクを見ることができますhttp://en.wikipedia.org/wiki/Reverse_Polish_notation

于 2012-07-16T09:46:53.593 に答える