0

この .txt ファイルを自分のプログラムに読み込もうとしています (手動入力の改善として)。入力 txt ファイルを受け入れるようにメソッドを変換するのに問題があります。"infix[--pos]='\0';" 行で arrayindexoutofboundsexception が発生します。

class Functions {
    void postfix(char infix[], char post[]) {
        int position, und = 1;
        int outposition = 0;
        char topsymb = '+';
        char symb;
        Stack opstk = new Stack();
        opstk.top = -1;
        for (position = 0; (symb = infix[position]) != '\0'; position++) {
            if (isoperand(symb))
                post[outposition++] = symb;
            else {
                if (opstk.isempty() == 1)
                    und = 1;
                else {
                    und = 0;
                    topsymb = opstk.pop();
                }
                while (und == 0 && precedence(topsymb, symb) == 1) {
                    post[outposition++] = topsymb;
                    if (opstk.isempty() == 1)
                        und = 1;
                    else {
                        und = 0;
                        topsymb = opstk.pop();
                    }
                }// end while
                if (und == 0)
                    opstk.push(topsymb);
                if (und == 1 || (symb != ')'))
                    opstk.push(symb);
                else
                    topsymb = opstk.pop();
            }// end else
        }// end for
        while (opstk.isempty() == 0)
            post[outposition++] = opstk.pop();
        post[outposition] = '\0';
    }// end postfix function

    int precedence(char topsymb, char symb) {
        /* check precedence and return 0 or 1 */
        if (topsymb == '(')
            return 0;
        if (symb == '(')
            return 0;
        if (symb == ')')
            return 1;
        if (topsymb == '$' && symb == '$')
            return 0;
        if (topsymb == '$' && symb != '$')
            return 1;
        if (topsymb != '$' && symb == '$')
            return 0;
        if ((topsymb == '*' || topsymb == '/') && (symb != '$'))
            return 1;
        if ((topsymb == '+' || topsymb == '-') && (symb == '-' || symb == '+'))
            return 1;
        if ((topsymb == '+' || topsymb == '-') && (symb == '*' || symb == '/'))
            return 0;
        return 1;
    } /* end precedence function */

    private boolean isoperand(char symb) {
        /* Return 1 if symbol is digit and 0 otherwise */
        if (symb >= '0' && symb <= '9')
            return true;
        else
            return false;
    }/* end isoperand function */

}

public class Driver {
    public static void main(String[] args) throws IOException {
        Functions f = new Functions();
        char infix[] = new char[80];
        char post[] = new char[80];
        int pos = 0;
        char c;
        System.out.println("\nEnter an expression is infix form : ");

        try {
            BufferedReader in = new BufferedReader(new FileReader("infix.txt"));
            String str;
            while ((str = in.readLine()) != null) {
                infix = str.toCharArray();
            }
            in.close();
        } catch (IOException e) {
        }

        infix[--pos] = '\0';
        System.out.println("The original infix expression is : ");
        for (int i = 0; i < pos; i++)
            System.out.print(infix[i]);
        f.postfix(infix, post);
        System.out.println("\nThe postfix expression is : ");
        for (int i = 0; post[i] != '\0'; i++)
            System.out.println(post[i]);

    }
}
4

2 に答える 2

0

決してこのようにすべきではありません:

try {
...
} catch (IOException e) {
}

コード実行に関するいくつかの重要な情報が失われます。

少なくとも、調査を追跡するためにスタックトレースを印刷する必要があります。

e.printStackTrace();

FileNotFound例外がある可能性があります。

さらに、配列のインデックスを-1に設定しようとするとinfix[--pos]、このステートメントの前にposが0に設定されます。

于 2012-09-27T16:50:38.010 に答える
0

1) 余談ですが、メインの行は System.out.println("\nEnter an expression in infix form : "); と読むべきだと思います。2) 同様に、catch ステートメントについても同意します。すでに IOExcpetion に絞り込んでいますが、catch System.err.println(e.getMessage()); 内で次のように表示することで、さらに多くの情報を見つけることができます。または e.printStackTrace()

今あなたの質問に答えます。pos を値 0 に初期化していますが、行 infix[--pos] = '\0'; で PREINCREMENT を実行しています。したがって、 pos は -1 になります (明らかに配列境界の範囲外です)。

それをポストインクリメントインフィックス[pos--] = '\0';に変更したいと思います。多分?

はい、あなたのコードはCのように見えます...

于 2012-09-27T17:24:14.017 に答える