Question

In: Computer Science

CS 209 Data Structure (Postfix notation) Postfix notation is a way of writing expressions without using...

CS 209 Data Structure

(Postfix notation)

Postfix notation is a way of writing expressions

without using parentheses.

For example, the expression (1 + 2) * 3

would be written as 1 2 + 3 *.

A postfix expression is evaluated using a stack.

Scan a postfix expression from left to right.

A variable or constant is pushed into the stack.

When an operator is encountered, apply the operator

with the top two operands in the stack and replace

the two operands with the result.

Write a program to evaluate postfix expressions.

Pass the expression as a command-line argument

in one string.

Solutions

Expert Solution

import java.io.*;

class St

{

   private int[] a;

   private int t,m;

   public St(int maxim)

   {

     m=maxim;

     a=new int[m];

     t=-1;

   }

   public void push(int keys)

   {

     a[++t]=keys;

   }

   public int pop()

   {

     return(a[t--]);

   }

}

class Eval{

   public int calculate(String s)

   {

     int k,r=0;

     k=s.length();

     St a=new St(k);

     for(int i=0;i<k;i++)

     {

       char ch=s.charAt(i);

       if(ch>='0'&&ch<='9')

         a.push((int)(ch-'0'));

       else

       {

         int c=a.pop();

         int d=a.pop();

         switch(ch)

         {

           case '+':r=c+d;

              break;

           case '-':r=c-d;

              break;

           case '*':r=c*d;

              break;

           case '/':r=c/d;

              break;

           default:r=0;

         }

         a.push(r);

       }

     }

     r=a.pop();

     return(r);

   }

}

public class Main

{

   public static void main(String[] args)throws IOException

   {

     String input;

     while(true)

     {

       System.out.println("Type the expression");

       input=getString();

       if(input.equals(""))

         break;

       Eval e=new Eval();

       System.out.println("Answer: "+e.calculate(input));

     }

   }

   public static String getString()throws IOException

   {

     DataInputStream i=new DataInputStream(System.in);

     String s=i.readLine();

     return s;

   }

}

Program:

Output:


Related Solutions

(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...
C++ Data Structure Write a program to change following infix expressions to postfix expressions using a...
C++ Data Structure Write a program to change following infix expressions to postfix expressions using a stack a) D-B+C b) C*D+A*B c) (A*B)*C+D*F-C d) (A-4*(B-C)-D/E)*F
CS 209 Data Structure 2. Create a method that takes a HashMap and returns the sum...
CS 209 Data Structure 2. Create a method that takes a HashMap and returns the sum of the keys of the HashMap. 3. Create a method that takes a HashMap and returns the sum of all keys and values of the HashMap. For example, if the input is [1=9, 3=6, 4=9, 6=8, 7=6] then the method should return 59.
CS 209 Data Structure 1. Create a method that takes an ArrayList of Integer and returns...
CS 209 Data Structure 1. Create a method that takes an ArrayList of Integer and returns a sorted copy of that ArrayList with no duplicates. Sample Input: {5, 7, 4, 6, 5, 6, 9, 7} Sample Output: {4, 5, 6, 7, 9}
CS 209 Data Structure 5. Consider the Pair class covered in class: class Pair {    ...
CS 209 Data Structure 5. Consider the Pair class covered in class: class Pair {     public A first;     public B second;     public Pair(A a, B b)     {         first = a;         second = b;     }     public void setFirst(A a)     {         first = a;     }     public A getFirst()     {         return first;     }     public void setSecond(B b)     {         second = b;     }     public...
Java Programming CS 209 Data Structure 1. Create a method that takes an ArrayList of String...
Java Programming CS 209 Data Structure 1. Create a method that takes an ArrayList of String and returns a copy of that ArrayList with no duplicates. The relative ordering of elements in the new ArrayList should be the same. Sample Input: {"qwerty", "asdfgh", "qwer", "123", "qwerty", "123", "zxcvbn", "asdfgh"} Sample Output: {"qwerty", "asdfgh", "qwer", "123", "zxcvbn"}
CS 209 Data Structure 1. show how to apply a selection sort on {45, 11, 50,...
CS 209 Data Structure 1. show how to apply a selection sort on {45, 11, 50, 59, 60, 2, 4, 7, 10}. 2. show how to apply a Insertion sort on {45, 11, 50, 59, 60, 2, 4, 7, 10}. 3. show how to apply a Bubble sort on {45, 11, 50, 59, 60, 2, 4, 7, 10}. 4. show how to apply a Merge sort on {45, 11, 50, 59, 60, 2, 4, 7, 10}. 5. show how to...
CS 209 Data Structure 3. a. Create a class named Point3D that contains 3 instance variables...
CS 209 Data Structure 3. a. Create a class named Point3D that contains 3 instance variables x, y, and z. b. Create a constructor that sets the variables. Also, create get and set methods for each variable. c. Create a toString() method. d. Make Point3D implement Comparable. Also, create a compareTo(Point3D other) method that compares based on the x-coordinate, then y-coordinate for tiebreakers, then z-coordinate for tiebreakers. For example, (1, 2, 5) comes before (2, 1, 4), which comes before...
Your assignment for this program is to evaluate a numeric expression in postfix notation using a...
Your assignment for this program is to evaluate a numeric expression in postfix notation using a dynamic (pointer based) stack. As stated in the handout, in order to evaluate a numeric expression, a compiler converts an infix numeric expression to postfix notation and then it uses an algorithm and a stack to evaluate the expression. Your program should implement the pseudocode algorithm described in the attached handout. Your program will read and evaluate expressions stored in an input file (infile.txt)....
1) Consider the following infix expressions. What is the equivalent postfix (reverse Polish notation) expression? 16/(5+3)b)...
1) Consider the following infix expressions. What is the equivalent postfix (reverse Polish notation) expression? 16/(5+3)b) A*B+C*Dc) X × Y + W × Z + V × U 2) Consider the postfix (reverse Polish notation) 10 5 + 6 3 - /. What is the equivalent infix expression?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT