使用堆栈从文件实现Postfix计算器(java)

时间:2015-02-25 08:54:35

标签: java file stack

我很难在我的中缀方法中编写完成此操作所需的算法编码。我使用pop / push方法。下面是我的代码的预期输入和输出。以下是算法中所需的确切步骤:

  1. 输入是一个包含中缀整数表达式

  2. 的字符串
  3. 从输入的第0位置到最后位置循环
    字符串

    a)从输入字符串中隔离单个字符串

    b)如果字符串是左括号"(&#34 ;,将它推到 堆栈

    c)如果字符串是数字(整数),请将其添加到 输出字符串的结尾

    d)如果字符串是运算符(*,/,%,+, - ),则将其推入堆栈

    e)如果字符串是右括号")",则弹出
    每个操作员离开堆栈,并将每个操作符添加到结尾 输出字符串,直到你弹出左括号"("离开堆栈

    1. 循环后,输出字符串是后缀表达式
    2. 所以我的问题是,如何将c和c部分实现到我的代码中。

      非常感谢任何帮助。

      文件本身包含:

      (5)(1+1)((9/3)-2)(9-(3/2))(1+(4-(9*(6/(5%7)))))((1+4)-(((9*6)/5)%7))(((1+(4-9))*6)/(5%7)) ((1+(2-3))(1+(2-3)))(1+(A-3))(1@1)
      

      并且应该输出:

      Reading from file: input13.txt
      
      Infix expression   = (5)
      Postfix expression = 5
      
      Infix expression   = (1+1)
      Postfix expression = 11+
      
      Infix expression   = ((9/3)-2)
      Postfix expression = 93/2-
      
      Infix expression   = (9-(3/2))
      Postfix expression = 932/-
      
      Infix expression   = (1+(4-(9*(6/(5%7)))))
      Postfix expression = 149657%/*-+
      
      Infix expression   = ((1+4)-(((9*6)/5)%7))
      Postfix expression = 14+96*5/7%-
      
      Infix expression   = (((1+(4-9))*6)/(5%7))
      Postfix expression = 149-+6*57%/
      
      Infix expression   =      
      ERROR:java.lang.RuntimeException: Bad character input: " "
      
      Infix expression   = ((1+(2-3))
      ERROR:java.lang.RuntimeException: Data still on stack: "("
      
      Infix expression   = (1+(2-3)))
      ERROR:java.util.EmptyStackException
      
      Infix expression   = (1+(A-3))
      ERROR:java.lang.RuntimeException: Bad character input: "A"
      
      Infix expression   = (1@1)
      ERROR:java.lang.RuntimeException: Bad character input: "@"  
      

      这是我目前的代码:

      equation是BTW文件中的整行。

             public static String infix(String equation){
             //instantiate a stack of Strings
               StackInterface<String> stack = new ArrayStack<String>();
               for(int i=0;i<equation.length();i++){
      
                  String character = equation.substring(i, i+1);
                  //Having trouble with this algorithm!
                 if (character == "(" ) {
                   stack.push(character);
      
                 }
                  else if (character.isDigit()) {
                     //add to end of outputstream
                  }
                  else if (character == "*") {
                    stack.push(character);
                  }
                  else if (character == "/") {
                    stack.push(character);
                  }
                  else if (character == "%") {
                    stack.push(character);
                  }
                  else if (character == "+") {
                    stack.push(character);
                  }
                  else if (character == "-") {
                    stack.push(character);
                  }
                  /* If the character string is a right parenthesis ")", 
                    pop each operator off the stack, and add each operator to the
                    end of the output string, until you pop a left parenthesis
                    "("  off the stack */
      
              }
          }
      }
      

0 个答案:

没有答案