2021年3月18日星期四

Java: RPN calculator issue (using stack and scanner)

I'm trying to make an RPN calculator but with more operations such as unary minus, duplicating the top item of the stack, etc. I'm not sure what is wrong with it as when I try to calculate anything, it doesn't even show the result and asks if I want to evaluate another expression. I'm not sure if it's in the evaluator class or it's in the test class that I need to resolve this issue.

public class PostfixEvaluator  {      private Stack<Integer> stack;        /*       Evaluator is set up by creating a new stack.       */      public PostfixEvaluator()      {          stack = new Stack<Integer>();      }      /*       Evaluates the specific postfix expression. An operand       is pushed onto the stack. If an operator is encountered,       operands are popped and operation is       evaluated. The result is pushed onto the stack.       @param expr string represents a postfix expression       @return result of given expression.       */      public void evaluate(String expr)      {          String token;          Scanner parser = new Scanner(expr);            while (parser.hasNext())          {              token = parser.next();                if (isOperator(token))              {                  evaluateSingleOperator(token.charAt(0));                                }              else                  stack.push((Integer.parseInt(token)));          }                }      /*       * Determines if the specified token is an operator.       * @param token the token to be evaluated        * @return true if token is operator       */      private boolean isOperator(String token)      {          return ( token.equals("+") || token.equals("-") ||                   token.equals("*") || token.equals("/") ||                   token.equals("%") || token.equals("m") ||                   token.equals("r") || token.equals("d") ||                   token.equals("p") || token.equals("f") ||                   token.equals("c") || token.equals("q") ||                   token.equals("h"));      }        /*       Executes an integer evaluation on a single expression consisting of        * the specified operator and operands.       * @param operation operation to be performed       * @param op1 the first operand       * @param op2 the second operand       * @return value of the expression       */      private void evaluateSingleOperator(char operation)      {          int op1, op2 = 0;          switch (operation)          {              case '+':                  op2 = stack.pop();                  op1 = stack.pop();                                    stack.push(op2+op1);                                    break;                                case '-':                  op2 = stack.pop();                  op1 = stack.pop();                                    stack.push(op2-op1);                                    break;                                case '*':                  op2 = stack.pop();                  op1 = stack.pop();                                    stack.push(op2-op1);                                    break;                                case '/':                  op2 = stack.pop();                  op1 = stack.pop();                                    stack.push(op2/op1);                                    break;                                case '%':                  op2 = stack.pop();                  op1 = stack.pop();                                    stack.push(op2%op1);                                    break;                                                  case 'm':                  op2 = stack.pop();                  op1 = stack.pop();                                    stack.push(op1 * -1);                                    break;                                                  case 'r':                  op2 = stack.pop();                  op1 = stack.pop();                  stack.push(op2);                  stack.push(op1);                  break;                                                  case 'd':                  stack.push(stack.peek());                  break;                                                  case 'p':                  op1 = stack.pop();                  op2 = stack.pop();                  stack.push(op1);                  op1 = stack.peek();                  break;                                case 'n':                  op1 = stack.pop();                  op2 = stack.peek();                  break;                                case 'f':                  System.out.println(stack.toString());                  break;                                case 'c':                  stack.clear();                  break;                                case 'h':                  System.out.println("+ = add the top two items" +              "* = multiply the top two items" +              "- = subtract the top item from the next item" +              "/ = integer divide the second item" +              "% = find integer remainder when dividing the second item" +              "m = unary minus -- negate the top item" +              "r = exchange the top two items" +              "d = duplicate top item on stack" +              "p = print (to screen) the top item" +              "n = print and remove the top item" +              "f = print all the contents of the stack (left intact)" +              "c = clear the stack" +              "q = quit" +              "h = print a help message");          }            return;      }  }            public class PostFixTester      {      /*       * Reads and evaluates multiple postfix expressions.       */      public static void main(String[] args)      {          String expression, again;          int result;          Scanner in = new Scanner(System.in);                  do          {                PostfixEvaluator evaluator = new PostfixEvaluator();              System.out.println("Enter a valid post-fix expression one character " +                                 "at a time with a space between each character (e.g. 5 4 + 3 2 1 - + *)");              System.out.println("Each character must be an integer/operator (+,-,*,/,%," +                                 "m,r,d,p,n,f,c,q,h), enter q to quit");              expression = in.nextLine();                                       System.out.print("Evaluate another expression (Y/N)? ");              again = in.nextLine();              System.out.println();          }          while (again.equalsIgnoreCase("q"));     }      }  
https://stackoverflow.com/questions/66701230/java-rpn-calculator-issue-using-stack-and-scanner March 19, 2021 at 09:04AM

没有评论:

发表评论