Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1)write code that read the input.txt and then using a Stack class to evaluate si

ID: 3804343 • Letter: 1

Question

1)write code that read the input.txt and then using a Stack class to evaluate simple postfix expressions having one public method in that class, main(String[] args).

2) I want to make sure that I have the right implementation stack using linked list public class Stack :

public class Stack {

   /**

   * Create an empty Stack

   */

   public Stack()

   {

       mStack = new Stack();

   }

   /**

   * Make item the Top of the Stack

   * @param item the item to push

   */

   public void push(String item)

   {

       mStack.mlist.addToFront(item);

   }

   /**

   * Remove the Top of the Stack

   */

   public void pop ()

   {

       if(mStack.isEmpty())

       {

           System.out.println("the list is empty");

       }

       else

       {

       mStack.mlist.removeFront();

       }

   }

  

   /**

   * return the Top of the Stack –do not remove it

   * @return the item at the top of the stack

   */

   public String getTop()

   {

       return mStack.mlist.getFront();

   }

   /**

   * Return true if Stack is empty, false otherwise

   * @return true if the empty or false if the stack is not empty

   */

   public boolean isEmpty()

   {

       if(mStack.mlist.askCount() == 0)

       {

           return true;

       }

       else

           return false;

      

   }

private Stack mStack;

private List mlist;

}

Note : I have list class and it is correct

the input.txt:

5 2 - 6 7 + * 3 /
20 2 / 2 / 2 /
6 2 + 3 - 4 * 7 /
90 sin
180 cos
270 sin
45 tan
135 tan
225 tan

Explanation / Answer

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Stack;

public class PostfixEvaluation {

public static void main(String[] args) {

  try {
   File file = new File("C:\Users\aditya\Desktop\input.txt");
   FileReader fileReader = new FileReader(file);
   BufferedReader bufferedReader = new BufferedReader(fileReader);
   String line;
   double result = 0;
   while ((line = bufferedReader.readLine()) != null) {

    result = evaluate(line);
    System.out.println("The expression :" + line
      + " is evaluated to :" + result);

   }
   fileReader.close();

  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }

}

public static double evaluate(String expression) {
  Stack<Double> stack = new Stack<Double>();
  String regex = "\d+";
  String arr[] = expression.split(" ");
  double result = 0;
  for (int i = 0; i < arr.length; i++) {
   if (arr[i].matches(regex)) {
    stack.push(Double.parseDouble(arr[i]));
   } else {
    double x = 0;
    double y = 0;
    try {
     x = stack.pop();
     y = stack.pop();
    } catch (Exception e) {

    }
    if ("+".equals(arr[i])) {
     result = x + y;
    } else if ("-".equals(arr[i])) {
     result = y - x;
    } else if ("*".equals(arr[i])) {
     result = y * x;
    } else if ("/".equals(arr[i])) {
     result = y / x;
    } else if ("sin".equals(arr[i])) {
     result = Math.sin(Math.toRadians(x));
    } else if ("cos".equals(arr[i])) {
     result = Math.cos(Math.toRadians(x));
    } else if ("tan".equals(arr[i])) {
     result = Math.tan(Math.toRadians(x));
    } else {

    }

    stack.push(result);

   }
  }
  return stack.pop();

}
}