Java - Postfix Notation Expression Evaluator

  • Comp Sci
  • Thread starter schapman22
  • Start date
  • Tags
    Java
In summary, when I enter an expression in postfix notation using a stack, I am getting incorrect numbers output. I am not sure how to debug the problem.
  • #1
schapman22
74
0

Homework Statement



I need to write a program that evaluates a postfix notation expression using a stack.

Homework Equations



5 3 2 * + 4 - 5 + = 12

The Attempt at a Solution



Code:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.colorado.collections;
import java.util.Scanner;

/**
 *
 * @author schapman2344
 */
public class PostfixEvaluation
{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        ArrayStack<Double> numbers = new ArrayStack();
        String input;
        double num1, num2, num3;

        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter an expression in postfix notation" +
                " then press <Enter>: ");
        input = keyboard.next();
        int i = 0;

        do
        {
            if(Character.isWhitespace(input.charAt(i)))
                i++;//go to next character
            else if(Character.isDigit(input.charAt(i)))
            {
                numbers.push((double)input.charAt(i));
                i++;
            }
            else
            {
                switch(input.charAt(i))
                {
                    case '*':
                        num2 = numbers.pop();
                        num1 = numbers.pop();
                        num3 = num1*num2;
                        numbers.push(num3);
                        break;

                    case '/':
                        num2 = numbers.pop();
                        num1 = numbers.pop();
                        num3 = num1/num2;
                        numbers.push(num3);
                        break;

                    case '+':
                        num2 = numbers.pop();
                        num1 = numbers.pop();
                        num3 = num1+num2;
                        numbers.push(num3);
                        break;

                    case '-':
                        num2 = numbers.pop();
                        num1 = numbers.pop();
                        num3 = num1-num2;
                        numbers.push(num3);
                        break;
                }
                i++;
            }
        }while(i<input.length());

        System.out.println("The result is: " + numbers.pop());
    }
}

When I input a postfix expression it is outputting an incorrect number. For example when I input "5 3 2 * + 4 - 5 +" which evaluates to 12, it outputs 53.
 
Physics news on Phys.org
  • #2
Changed the input to keyboard.nextLine(). But I am still getting wrong answers.
 
  • #3
I suggest you add some simple debugging output, like
Code:
        System.out.println(input.charAt(i) +  " op gave " + num3);
immediately after the operations switch statement.
 
  • #4
The values I'm getting back I can't make sense of. If I only enter 0 I get 48. If I enter any other single number I get 48+n. When I use an operator the numbers get even stranger.
 
  • #5
Sorry just realized that these are the ascii values of the input. But I am unsure how to get it to recognize the numerical value and not the ascii value.
 
  • #6
That's your problem right there. Easy enough to subtract off the ascii value of '0' from any digit character, but there's probably a valueOf or parseInt function available too.
 
Last edited:
  • #7
Cool I got it working now. Thanks for your help!
 
  • #8
The remaining question is: did the problem specify single digits for numerical input? otherwise you should be considering how to handle multi-character numbers.
 

Related to Java - Postfix Notation Expression Evaluator

1. What is a PostFix evaluator in Java?

A PostFix evaluator in Java is a computer program that evaluates mathematical expressions written in PostFix notation. PostFix notation, also known as Reverse Polish notation, is a way of writing mathematical expressions where the operators come after the operands. For example, the expression 2 + 3 would be written as 2 3 + in PostFix notation.

2. How does a PostFix evaluator work in Java?

A PostFix evaluator in Java works by using a stack data structure. It reads the expression from left to right and pushes operands onto the stack. When it encounters an operator, it pops the necessary operands from the stack, performs the operation, and pushes the result back onto the stack. Once the entire expression has been evaluated, the final result is at the top of the stack.

3. What are the advantages of using a PostFix evaluator in Java?

One advantage of using a PostFix evaluator in Java is that it eliminates the need for parentheses in mathematical expressions. This makes the expressions easier to read and reduces the chances of making mistakes. Additionally, PostFix evaluators are relatively easy to implement and efficient in terms of memory usage.

4. How can I implement a PostFix evaluator in Java?

To implement a PostFix evaluator in Java, you will need to use a stack data structure and a loop to read and evaluate each character in the expression. You can also use a switch statement to handle different types of operators. It is important to handle any potential errors, such as division by zero, while evaluating the expression.

5. What are some common applications of a PostFix evaluator in Java?

A PostFix evaluator in Java can be used in various applications, such as calculators, spreadsheets, and programming languages. It is particularly useful in situations where mathematical expressions need to be evaluated quickly and efficiently, without the use of parentheses. It can also be used for evaluating expressions in reverse Polish notation, such as in some computer science algorithms and data structures.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Programming and Computer Science
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
Back
Top