0

スタックオーバーフローさん、こんにちは。

私は後置電卓への中置を作成中です。計算機はファイルから入力を読み取ってから、スタックとキューを使用して後置記法を作成する必要があります。ファイルを読み取り、後置表記をキューに作成するためのすべてのコードがあります。私が読んでいるファイルには次が含まれています:

(4>3)+(3=4)+2

キューで後置記法に入れる私のコードは次のとおりです。

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


public class Proj1Main {

    public static void main(String[] args) {

        readMathFile();
        q.printQueue();

    }

    public static void readMath(char c, myStack s, myQueue q) {
        if (c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7' || c == '8' || c == '9') {
            System.out.println("NUMBER"); // <--for testing.
            int o = (int)c;
            q.enqueue(o);
        } else if(c == '+' || c=='-') {
            System.out.println("+ or -");
            Object x = s.pop();
            while( !s.isEmpty() ) {
                q.enqueue(x);
                x = s.pop();
            }
        } else if(c == '(' || c == ')' || c == '!' || c == '<' || c == '>' || c == '&' || c == '|' || c == '=') {
            System.out.println("other operator"); // <--for testing.
            Object x = s.pop();
            char y = x.toString().charAt(0);
            while( !s.isEmpty() && (y != '\\' || y != '*') ) {
                q.enqueue(y);
                y = (Character)s.pop();
                if(y != '\\' || y != '*') {
                    q.enqueue(y);
                    s.push(x);
                }
            }
        } else if(c=='\\' || c == '*') {
            System.out.println("divide or multiply"); // <--for testing.
            Object x = s.pop();
            while( !s.isEmpty() ) {
                q.enqueue(x);
                x = s.pop();
            }
        } else if(c == ')') {
            System.out.println("close paren"); // <--for testing.
            Object x = s.pop();
            while( !s.isEmpty() && x != "(" ) {
                q.enqueue(x);
                x = s.pop();
            }
        }
    }

    public static myStack s;
    public static myQueue q;

    // the file reading code was borrowed from:
    // http://www.java2s.com/Code/Java/File-Input-Output/Readfilecharacterbycharacter.htm
    public static void readMathFile() {
        s = new myStack();
        q = new myQueue();
        File file = new File("test.txt");
        if (!file.exists()) {
            System.out.println(file + " does not exist.");
            return;
        }
        if (!(file.isFile() && file.canRead())) {
            System.out.println(file.getName() + " cannot be read from.");
            return;
        }
        try {
            FileInputStream fis = new FileInputStream(file);
            char current;
            // in this while loop is where all of the reading happens
            while (fis.available() > 0) {
                current = (char) fis.read();
                readMath(current, s, q);
            }
            if(fis.available() == 0) {
                Object x = s.pop();
                while(!x.equals("empty stack"))
                    q.enqueue(s.pop());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

コードを実行した後、次の出力を出力します。

キュー: 52 51 51 52 50

52、51 などがどこから来ているのかわかりません。「4>33=4+2+」と表示されるはずです (私はそう思います) 誰かが私の問題を特定できるかどうか疑問に思っていましたか? または、それを修正する方法についてのヒントを教えてください。

4

1 に答える 1

1

52 51 51 52 50... は、それぞれ文字「4」、「3」、「3」、「4」、「2」の ASCII コードです。

あなたがしているとき:

current = (char) fis.read();

あなたはキャラクター自身を手に入れています。

後で readMath():

int o = (int)c;

整数に変換してキューに入れています。おそらく、キューを印刷すると、それはまだ整数であり、ASCII コードとして出力されます。

これを行うことで、数字の char をそれが表す整数に変換できます。

Character.getNumericValue(c);
于 2013-02-23T02:11:59.810 に答える