Postfix表达式输出中缺少空格 - Java

时间:2017-10-21 23:20:37

标签: java postfix-notation infix-notation

我的代码成功将中缀表达式转换为后缀表达式。但是,当我输入一个大于1位的数字(例如546)时,我在这个数字和右操作数之间没有空格。

我的代码的测试运行: 输入:输入表达式:(24/4)/(15/3)* 10 - 4 + 2 输出:后缀表达式为:244/1515 / / 10 * 4 - 2 +

我希望Postfix Expression是:24 4/15 3 / / 10 * 4 - 2 +

这是我的代码:请建议任何允许我在输出中插入空格的更改。

import java.util.*;

public class PostfixConversion {

    public static void main(String args[]) {

        System.out.print("Enter an expression: ");
        String infix = new Scanner(System.in).nextLine();

        System.out.println(convertToPostfix(infix));

    }


   public static boolean precedence(char first, char second)
   {
      int v1 = 0, v2 = 0;
      //find value for first
      if(first == '-' || first == '+'){
         v1 = 1;
      }else if(first == '*' || first == '/'){
         v1 = 2;    
      }//end if

      //find value for second
      if(second == '-' || second == '+'){
         v2 = 1;
      }else if(second == '*' || second == '/'){
         v2 = 2;    
      }//end if

     if(v1 < v2){
        return false;
     }//end if

     return true;
  }//end precedence method

 //converts infix expression into postfix expression
 public static String convertToPostfix(String infixExp)
  {
     String postFix = "The Postfix Expression is: ";
     Stack<Character> stack = new Stack<Character>();
     char character = ' ';

     for(int i = 0; i < infixExp.length(); i++)
     {
         character = infixExp.charAt(i);

         //determine if character is an operator
         if(character == '*' || character == '-' || character == '/' || character == '+')
         {
             while(!stack.empty() && precedence(stack.peek(), character)){
                 postFix += stack.pop();
             }//end while
             stack.push(character);
         }
         else if(character == '(') //check for left parenthesis
         {
             stack.push(character);
         }
         else if (character == ')') 
         {
             while(!stack.peek().equals('(') && !stack.isEmpty()){ //add characters until left parenthesis
                 postFix += stack.pop();
             }//end while

             if(!stack.isEmpty() && stack.peek().equals('(')){
                 stack.pop(); // pop/remove left parenthesis
             }
         }
         else
         {
             postFix += character;
         }//end if
     }//end for
     while(!stack.empty()) //add the remaining elements of stack to postfix expression
     {
         if(stack.peek().equals('('))
         {
             postFix = "There is no matching right parenthesis.";
             return postFix;
         }
         postFix += stack.pop();
     }
         return postFix;
 }//end convertToPo
}

1 个答案:

答案 0 :(得分:0)

修复是在这里添加一行(参见代码注释):

if(character == '*' || character == '-' || character == '/' || character == '+')
{
    postFix += ' '; // <-- add space here
    while(!stack.empty() && precedence(stack.peek(), character)){
        postFix += stack.pop();
    }//end while
    stack.push(character);
}

应用修复后,我机器上的输出如下:

The Postfix Expression is: 24 4/   15 3/  / 10  * 4  - 2+