一次在java中输入多个输入

时间:2013-09-28 20:29:39

标签: java

我编写了一个将中缀表达式转换为后缀表达式的程序。但是,当我找到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);

    }

}

任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分: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);
    }    
}

答案 1 :(得分:1)

尝试替换

    String s = input.readLine();

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

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