2

我编写了一个将中缀表达式转换为后缀表达式的程序。但是,我需要进行多个输入并在找到 0 时停止。

我所拥有的是,

输入:(3+4)*(3-1)

输出:4 + 3 1 - *

我需要的是,

输入:

(3+4)*(3-1)

(3*4)-(3*1)

(3+4)*(3-1)

(3+2)*((3-3)

(3+4)*(3-1)

0

输出:

4 + 3 1 - *

4 * 3 1 * -

4 + 3 1 - *

语法错误

4 + 3 1 - *

结尾

代码是:

public static void main(String args[])throws IOException
{
    BufferedReader input = new BufferedReader (new InputStreamReader(System.in));
    String s = input.readLine();
    String pf = new String();
    int i=0, check=0;
    Stack<Character> s1 = new Stack<>();


    while(i<s.length())
    {
        if(isOperand(s.charAt(i)))
        {
            pf += s.charAt(i);
        }
        else if(isOperator(s.charAt(i)))
        {
            while (!s1.empty() && s1.peek() != '(' && compareOperators(s1.peek(),s.charAt(i)) <= 0)
            {
                pf += ' ';
                pf += s1.peek();
                s1.pop();
            }
            pf += ' ';
            s1.push(s.charAt(i));
        }
        else if (s.charAt(i) == '(') 
        { 
            s1.push(s.charAt(i));
        }
        else if (s.charAt(i) == ')') 
        {
            check++;
            while (!s1.empty()) 
            {
                if ((char)s1.peek() == '(') 
                { 
                    check--;
                    s1.pop(); 
                    break; 
                }
                pf += ' ';
                pf += s1.peek();
                s1.pop();
            }
        }
        i++;
    }


    while (!s1.empty()) {
        if(s1.peek()=='(')
            check--;
        pf += ' ';
        pf += s1.peek();
        pf += ' ';
        s1.pop();
    }
    if(check!=0)
        System.out.println("Syntax Error");
    else
    {
        System.out.println(pf);

    }

}

谁能帮我?

4

2 回答 2

2

将中缀到前缀转换的代码放在单独的函数中。

public void convert(String s) {
    String pf = new String();
    int i=0, check=0;
    Stack<Character> s1 = new Stack<>();


    while(i<s.length())
    {
        if(isOperand(s.charAt(i)))
        {
            pf += s.charAt(i);
        }
        else if(isOperator(s.charAt(i)))
        {
            while (!s1.empty() && s1.peek() != '(' && compareOperators(s1.peek(),s.charAt(i)) <= 0)
            {
                pf += ' ';
                pf += s1.peek();
                s1.pop();
            }
            pf += ' ';
            s1.push(s.charAt(i));
        }
        else if (s.charAt(i) == '(') 
        { 
            s1.push(s.charAt(i));
        }
        else if (s.charAt(i) == ')') 
        {
            check++;
            while (!s1.empty()) 
            {
                if ((char)s1.peek() == '(') 
                { 
                    check--;
                    s1.pop(); 
                    break; 
                }
                pf += ' ';
                pf += s1.peek();
                s1.pop();
            }
        }
        i++;
    }


    while (!s1.empty()) {
        if(s1.peek()=='(')
            check--;
        pf += ' ';
        pf += s1.peek();
        pf += ' ';
        s1.pop();
    }
    if(check!=0)
        System.out.println("Syntax Error");
    else
    {
        System.out.println(pf);

    }

}

然后在主函数中:

public static void main(String args[])throws IOException
{
    BufferedReader input = new BufferedReader (new InputStreamReader(System.in));
    String s;
    while(true) {
        s = input.readLine();
        if (s.equals("0"))
            break;
        else 
            convert(s);
    }    
}
于 2013-09-28T20:38:08.080 回答
1

尝试更换

    String s = input.readLine();

    String s;
    while(!(s = input.readLine()).equals("0")){

然后在最后放置一个额外的括号。现在,您正在阅读 with 中的每一行,并在每次使用之前(s = input.readLine()确保它不等于 0 。!s.equals(0)

于 2013-09-28T20:35:34.823 回答