Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 堆栈实现问题/未打印某些字符_Java_Postfix Notation - Fatal编程技术网

Java 堆栈实现问题/未打印某些字符

Java 堆栈实现问题/未打印某些字符,java,postfix-notation,Java,Postfix Notation,我正在尝试写一个中缀到后缀计算器。我正在读取包含以下内容的文件: (4>3)+(3=4)+2 当我运行代码时,我应该得到一个包含输入后缀符号的字符串, 但是我什么也得不到。我的代码似乎没有到达最终的打印语句。另外,当我修改代码以使其至少打印(尽管符号不正确)时,它只打印数字,而不打印运算符(+、-、&,等等)。我搞不懂为什么会这样!我在下面的代码中标记了打印语句的位置: public static void main(String[] args) { readMathFile(

我正在尝试写一个中缀到后缀计算器。我正在读取包含以下内容的文件:

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

当我运行代码时,我应该得到一个包含输入后缀符号的字符串, 但是我什么也得不到。我的代码似乎没有到达最终的打印语句。另外,当我修改代码以使其至少打印(尽管符号不正确)时,它只打印数字,而不打印运算符(+、-、&,等等)。我搞不懂为什么会这样!我在下面的代码中标记了打印语句的位置:

public static void main(String[] args) {

        readMathFile();
}

static String PF = "";

    public static void postfix(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') {
            String cc = Character.toString(c);
            PF.concat(cc);
        } else if(c == '!' || c == '*' || c == '/' || c == '+' || c == '-' || 
                c == '<' || c == '>' || c == '=' || c == '&' || c == '|') {
            if(s.isEmpty())
                s.push(c);
            else {
                char top = s.peek();
                while ((precedence(top) > precedence(c)) && !s.isEmpty()) {
                    String cd = Character.toString(s.pop());
                    PF.concat(cd);
                }
                s.push(c);
            }
        }


    }


    public static myStack s;
    public static myQueue q;
    public static int i = 1;

    // 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);
                postfix(current, s, q);
            }
            if(fis.available() == 0) {
                char x = s.pop();
                while(!s.isEmpty()) {
                    //q.enqueue(s.pop());
                    String ce = Character.toString(s.pop());
                    PF.concat(ce);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("\n\n"+PF); // <----CODE NEVER REACHES THIS POINT! (and when i modify so it does, it will not print the operators!)
    }
publicstaticvoidmain(字符串[]args){
readMathFile();
}
静态字符串PF=“”;
公共静态void后缀(char c、myStack s、myQueue q){
如果(c='0'| | c='1'| | c='2'| | c='3'| | c='4'||
c='5'| | c='6'| | c='7'| | c='8'| | c='9'){
字符串cc=字符.toString(c);
concat(cc);
}如果(c='!'| | c='*'| | c='/'| | c='+'| | c='-'| |
c=''| | c='=''| | c='&'| | c='='|'){
如果(s.isEmpty())
s、 推(c);
否则{
char-top=s.peek();
while((优先级(top)>优先级(c))&&&!s.isEmpty()){
字符串cd=Character.toString(s.pop());
concat(cd);
}
s、 推(c);
}
}
}
公共静态myStack;
公共静态myq队列;
公共静态int i=1;
//文件读取代码借用自:
// http://www.java2s.com/Code/Java/File-Input-Output/Readfilecharacterbycharacter.htm
公共静态void readMathFile(){
s=新的myStack();
q=新的myQueue();
File File=新文件(“test.txt”);
如果(!file.exists()){
System.out.println(文件+“不存在”);
返回;
}
if(!(file.isFile()&&file.canRead()){
System.out.println(文件.getName()+“无法从中读取”);
返回;
}
试一试{
FileInputStream fis=新的FileInputStream(文件);
炭流;
//在这个while循环中,所有的读取都发生在这里
而(fis.available()>0){
当前=(字符)fis.read();
//readMath(电流、s、q);
后缀(当前、s、q);
}
如果(fis.available()==0){
char x=s.pop();
而(!s.isEmpty()){
//q、 排队(s.pop());
字符串ce=Character.toString(s.pop());
孔卡律师事务所(ce);
}
}
}捕获(IOE异常){
e、 printStackTrace();
}

System.out.println(“\n\n”+PF);/”我正在读取一个文件,其中包含:“…什么?:-)字符串是不可变的,因此
PF.concat(cc)
不会修改原始字符串,而是返回一个新字符串。
PF=PF.concat(cc)可以更好地工作。但是,当将许多字符聚合成一个字符串时,考虑使用<代码> StringBuilder <代码>。同时,在<代码>(FIS.Audiable()> 0)尝试打印<代码>当前< /代码>,看看会发生什么。