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.
(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...
Write a class PostFix that has one method covert that converts an infix expression (as a...
Write a class PostFix that has one method covert that converts an infix expression (as a string input) to a postfix. Then Test it in a different class. code in java.
Write a C++ function that takes in an arithmetic expression in prefix notation and converts it...
Write a C++ function that takes in an arithmetic expression in prefix notation and converts it into a binary tree, such that each operation is stored in a node whose left subtree stores the left operand, and whose right subtree stores the right operand.
Using STL stack class, implement in C++ a function that converts an infix expression to postfix...
Using STL stack class, implement in C++ a function that converts an infix expression to postfix expression,
In Java, write class InfixToPostfixConverter co convert an ordinary infix arithmetic expression (assume a valid expression...
In Java, write class InfixToPostfixConverter co convert an ordinary infix arithmetic expression (assume a valid expression is entered) with single-digit integers (to make things easier) such as (6 + 2) • 5 - 8 / 4 to a postfix expression. The postfix version (no parentheses are needed) of this infix expression is 6 2 + 5 * 8 4 / - The program should read the expression into StringBuilder or String infix and use the Stack and Stack Interface class...
Question 1: Given the infix arithmetic expression as follows: Exp = Z * ((B + C...
Question 1: Given the infix arithmetic expression as follows: Exp = Z * ((B + C * D) / S + F)             Where the values of the variables are:                         Z = 3, B = 6, C = 2, D = 5, S = 4 , F = 21             Evaluate the expression using a stack. Shows the logical representation of the evaluation process. ..............................................................................
**write a java program infix to postfix**showing the expression tree*** I. Input All input data are...
**write a java program infix to postfix**showing the expression tree*** I. Input All input data are from a file "in.dat". The file contains a sequence of infix form expressions, one per line. The character '$' is an end mark. For example, the following file has four infix form expressions: 1 + 2 * 3 ^ ! ( 4 == 5 ) $ 3 * ( 6 - 4 * 5 + 1) + 2 ^ 2 ^ 3 $ 77...
: In this assignment you will write a C++ program that evaluates an arithmetic expression (represented...
: In this assignment you will write a C++ program that evaluates an arithmetic expression (represented with an infix notation), then outputs this expression in prefix form and also outputs the result of the calculation. The program will first convert the input infix expression to a prefix expression (using the Stack ADT) and then calculate the result (again, using the Stack ADT). The details are provided in the following sections.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT