Question

In: Computer Science

An arithmetic expression can be represented in three different formats: infix, prefix, and postfix. In the...

An arithmetic expression can be represented in three different formats: infix, prefix, and postfix. In the infix notation, the operator comes between two operands. In the postfix notation, the operator comes after its two operands. For example, an expression a/b can be transformed to ab/. The following are some examples of the postfix expressions:

2+6*4 Its postfix notation is 2 6 4 * +

2-3+5/4*9-1 Its postfix expression is 2 3 – 5 4 / 9 * + 1 -

For this assignment, write a JAVA program that reads an postfix expression and evaluates the postfix expression. Your program should display the following menu repeatedly:

1. Read an expression in postfix notation.

// Read an infix expression and print out the string for verification.

2. Evaluate the postfix expression.

0. Exit.

For the input string, you can make the following assumptions:

Input strings are error-free

+, -, *, / are the only operators used

All operand are positive integers consisting of one or more digits.

Space appears in the input string to separate the operands and operators.

Implement a class StackLink, which is a stack implemented by a linked list (Do not use the JAVA predefined class java.util.LinkedList). Your program should use stack operations to evaluate the postfix expression. The following figure shows how stacks can help.

The following are some results when your run the program:

C:'>java Homework4

Select from:

1. Read an expression in postfix notation.

2. Evaluate the postfix expression

0. Exit.

1

Enter a postfix expression: 32 15 2 * + 36 4 / -

The entered infix expression is: 32 15 2 * + 36 4 / -

Select from:

1. Read an expression in postfix notation.

2. Evaluate the postfix expression

0. Exit.

2

32 15 2 * + 36 4 / - = 53

Select from:

1. Read an expression in postfix notation.

2. Evaluate the postfix expression

0. Exit.

1

Enter a postfix expression: 3 4 / 15 * 9 / 6 8 * + 7 - 54 +

The entered infix expression is: 3 4 / 15 * 9 / 6 8 * + 7 - 54 +

Select from:

1. Read an expression in postfix notation.

2. Evaluate the postfix expression

0. Exit.

2

3 4 / 15 * 9 / 6 8 * + 7 - 54 + = 95

Select from:

1. Read an expression in infix notation.

2. Convert infix to postfix.

3. Evaluate the postfix expression

0. Exit.

0

Goodbye.

Solutions

Expert Solution

As per your requirement I have created two classes StackLink and Test. StackList internally using user defined linked list.

Class 1 : StackLink
class StackLink {
  
private class Node {
  
int data;
Node link;
}
Node top;
StackLink()
{
this.top = null;
}
  
public void push(int x)
{
Node temp = new Node();
  
temp.data = x;
  
temp.link = top;
  
top = temp;
}
  
public boolean isEmpty()
{
return top == null;
}
  
public int peek()
{
if (!isEmpty()) {
return top.data;
}
else {
return -1;
}
}
  
public int pop()
{
if (top == null) {
System.out.println("\nStack Underflow");
return -1;
}
int temp=top.data;
top = (top).link;
return temp;
}
  
public void display()
{
if (top == null) {
System.out.println("\nStack Underflow");
System.exit(1);
}
else {
Node temp = top;
while (temp != null) {
  
System.out.print(temp.data);
  
temp = temp.link;
}
}
}
}

Class 2 : Test

import java.util.Scanner;

public class Test
{
   static int evaluatePostfix(String exp)
   {
       String array[]=exp.split(" ");
       StackLink stack=new StackLink();

       for(int i=0;i<array.length;i++)
       {
           Integer data = null;
           char c='\0';
           try {
               data = new Integer(Integer.parseInt(array[i]));
   } catch (NumberFormatException e) {
       c=array[i].charAt(0);
   }

           if(data !=null)
               stack.push(data.intValue());

           else
           {
               int val1 = stack.pop();
               int val2 = stack.pop();

               switch(c)
               {
               case '+':
                   stack.push(val2+val1);
                   break;

               case '-':
                   stack.push(val2- val1);
                   break;

               case '/':
                   stack.push(val2/val1);
                   break;

               case '*':
                   stack.push(val2*val1);
                   break;
               }
           }
       }
       return stack.pop();   
   }

   public static void main(String[] args)
   {
       int input=0;
       String exp="";
       Scanner sc=new Scanner(System.in);
       System.out.println("Select from:");
       while(true){
           System.out.println("1. Read an expression in postfix notation.");
           System.out.println("2. Evalute the postfix expression.");
           System.out.println("0. Exit.");
           input=Integer.parseInt(sc.nextLine());
           switch (input) {
           case 1:
               System.out.print("Enter a postfix expression: ");
               exp=sc.nextLine();
               System.out.println("The entered postfix expression is: "+exp);
               break;
           case 2:
               System.out.println(exp+" = "+evaluatePostfix(exp));
               break;
           case 0:
               System.out.println("Goodbye");
               System.exit(0);
               break;
           }
       }
   }
}

Example output:

Select from:
1. Read an expression in postfix notation.
2. Evalute the postfix expression.
0. Exit.
1
Enter a postfix expression: 2 3 +
The entered postfix expression is: 2 3 +
1. Read an expression in postfix notation.
2. Evalute the postfix expression.
0. Exit.
2
2 3 +=5
1. Read an expression in postfix notation.
2. Evalute the postfix expression.
0. Exit.
0
0
Goodbye


Related Solutions

Using a stack, write a program that turns a simple infix arithmetic expression into a postfix...
Using a stack, write a program that turns a simple infix arithmetic expression into a postfix expression. For example, 1 + 2 * 3 becomes 2 3 * 1 +. Also, evaluate the expression to ensure the expression is correct.
C++ program that can take in EITHER "infix" or "prefix" (NOT POSTFIX) and transforms it into...
C++ program that can take in EITHER "infix" or "prefix" (NOT POSTFIX) and transforms it into the other one. Should use stack and have infixToPrefix and prefixToInfix functions with a simple main function for execution.
java Convert the following Infix expression to a Postfix expression. Show all work                            Infix Expres
java Convert the following Infix expression to a Postfix expression. Show all work                            Infix Expression                                                 Stack                             Postfix Expression ---------------------------------------------------------------------------------------------------------------------------- 8 - 9*(2 + 1/4) + 3*7                                                     (empty)                            (blank) Evaluate the following Postfix expression. Once again, show all work.            Postfix                                               Stack                     Result           ---------------------------------------    --------------------    --------------            2 7 + 12 4 / * 8 5 + -
Write a program that takes an infix expression as input and produces a postfix expression. The...
Write a program that takes an infix expression as input and produces a postfix expression. The infix and postfix expressions are in the form of vectors of strings. We will test with a main method that takes a file name as input from the command line. We will test your implementation using printPostFix() method.   There are sample input and output files in this folder. You may edit the header file. Example 1 infix expression = apple + banana * cat...
Implement an infix expression to postfix expression convertor in C++. Note: - Support +, -, *,...
Implement an infix expression to postfix expression convertor in C++. Note: - Support +, -, *, /, ( and ) in infix expressions. - Operands will be nonnegative only. - Assume infix expressions are valid and well formatted (one blank space to separate operand/operator) For example, - 3 * 4 + 5 ➔ 3 4 * 5 + - 3 + 4 * 5 ➔ 3 4 5 * + - (3 + 4) * (5 + 6) ➔ 3...
Convert an infix expression to its postfix representation in JAVA
Convert an infix expression to its postfix representation in JAVA
(Convert infix to postfix) Note: Postfix notation is a way of writing expression without using parentheses....
(Convert infix to postfix) Note: Postfix notation is a way of writing expression without using parentheses. For example, the expression ( 11 + 12 ) * 13 would be written as 11 12 + 13 * Assume that ALWAYS there is a space between operands and operators in the input expression. Use two stacks, one to store the operands and one to store the operators. Your program only accpets following operators : ( ) + - / * Write a...
2. Convert the following infix form expression into the postfix form expression by using a stack:...
2. Convert the following infix form expression into the postfix form expression by using a stack: A+(B*(C-D)+E)/F-G*H Show the stack after each push/pop.
Using java.util.Stack and java.util.StringTokenizer to write a program that converts an infix expression to a postfix...
Using java.util.Stack and java.util.StringTokenizer to write a program that converts an infix expression to a postfix expression using data from infix.dat. Make sure your program can handle negative number. If the input expression can’t be converted, display error message.(1.5 points) (Should remove parentheses) infix.dat 5 * 6 + -10 3 - 2 + 4 7 ( 3 * 4 - (2 + 5)) * 4 / 2 10 + 6 * 11 -(3 * 2 + 14) / 2 2...
implement the algorithm described in this chapter to convert a prefix expression to postfix form. involve...
implement the algorithm described in this chapter to convert a prefix expression to postfix form. involve the classes that programming problems 4 and 5 describe This is to be written in C++. I cannot provide anymore information, I cannot provide any class information, etc. This is all the problem that book gave me. This is for Data Structures and algorithms.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT