java - 堆栈、队列和从文件中读取数学......奇怪的输出

标签 java stack queue infix-notation

你好,Stackoverflow,

我正在创建一个中缀到后缀计算器。计算器必须从文件读取输入,然后使用堆栈和队列创建后缀表示法。我有所有代码来读取文件并在队列中创建后缀表示法。我正在读取的文件包含:

(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();
        }
    }

}

运行代码后,我打印输出,结果是:

QUEUE: 52 51 51 52 50

我不知道 52、51 等是从哪里来的。它应该读为“4>33=4+2+”(我认为)我想知道是否有人能找出我的问题?或者给我一些如何修复它的提示?

最佳答案

52 51 51 52 50...分别是字符“4”、“3”、“3”、“4”、“2”的 ASCII 代码。

当你在做的时候:

current = (char) fis.read();

您正在获取角色本身。

稍后在 readMath() 中:

int o = (int)c;

您正在转换一个整数并将其放入队列中。可能当你打印队列时,它仍然是一个整数,并且以 ascii 代码的形式输出。

您可以通过执行以下操作将数字字符转换为其表示的整数:

Character.getNumericValue(c);

关于java - 堆栈、队列和从文件中读取数学......奇怪的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15036143/

相关文章:

python - 队列类,出队和入队? Python

redis - Redis "Pattern: Reliable queue"的代码实现

java - 由 : java. lang.RuntimeException 引起:Parcelable 读取 Serialized 对象时遇到 IOException

java - 如何基于堆栈等自定义数据结构创建 ObservableList

java - 需要从 JFrame 中的单选按钮获取输入,并在另一个类中使用选定的输入

c - 为什么需要在 stack.c 中包含 stack.h ?

带堆栈和链表的 C 语言计算器

api - 是否有某种服务来排队 api 调用?

java - 网格袋布局不会填充按钮

java - Java分布式Master-Worker架构仿真设计