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

write a program name calc.java that evaluates an infix expression entered by the

ID: 3681359 • Letter: W

Question

write a program name calc.java that evaluates an infix expression entered by the user. The expression may contain the following tokens:
(1) Integer constants (a series of decimal digits).
(2) x (representing a value to be supplied later).
(4) Binary operators (+, -, *, / and %).
(5) Parentheses

additional requirements for this program:

(1) You must use stack objects during the translation from infix to postfix.

(2) Operators must have the correct precedence and associativity. Binary operators * , / and % takes precedence over binary + and -. And processing is always done from left to right.

If the infix expression contains an error of any kind, the program must display the message Error in expression (with an optional explanation) and then terminate.

Here is my code so far but it doesn't work for some reason.

import java.util.* class term public term next; public Object data; public termO data next null public term(Object val) (data val next = null; public class intopost private term top public intopost(){ top =null; } public boolean emptyO return top public boolean fullO returnfalse: public void push(Object e) null; term tmp = newterm(e); tmp.next = top; top = tmp: public Object pop0 top = top.next Object e top.data; return e public Object peek0 Object e top.data; return e public void matching(String x) Stack s new Stack); { char c=x.charAt( 1 ); s.push(c); forint 1-0;.

Explanation / Answer

import java.util.Scanner;
import java.util.Stack;

/**
* @author Srinivas Palli
*
*/
class term {

   public term next;
   public Object data;

   /**
   *
   */
   public term() {
       // TODO Auto-generated constructor stub
       data = "";
       next = null;
   }

   /**
   * @param val
   */
   public term(Object val) {
       this.data = val;
       next = null;
   }

}

/**
* @author Srinivas Palli
*
*/
public class intopost {

   private term top;

   public intopost() {
       // TODO Auto-generated constructor stub
       top = null;
   }

   /**
   * @return
   */
   public boolean empty() {
       return top == null;

   }

   /**
   * @return
   */
   public boolean full() {
       return false;

   }

   /**
   * @param e
   */
   public void push(Object e) {

       term tmp = new term(e);
       tmp.next = top;
       top = tmp;
   }

   /**
   * @return
   */
   public Object pop() {

       Object e = top.data;
       top = top.next;
       return e;

   }

   /**
   * @return
   */
   public Object peek() {
       Object e = top.data;
       return e;

   }

   public static boolean matching = true;

   /**
   * @param x
   */
   public void maching(String x) {

       Stack s = new Stack();
       for (int i = 0; i < x.length(); i++) {

           char c = x.charAt(i);
           if (c == '(')
               s.push(c);

           else {

               if (c == ')')
                   if (s.empty())
                       matching = false;
                   else
                       s.pop();

           }

       }
       if (!s.empty())
           matching = false;

   }

   /**
   * @param x
   */
   public void Evaluation(String x) {
       int i, z, u;

       char c;
       intopost S = new intopost();
       for (i = 0; i < x.length(); i++) {

           c = x.charAt(i);

           String s = "0" + c;
           if (c == '+') {
               z = Integer.parseInt((String) S.pop())
                       + Integer.parseInt((String) S.pop());
               S.push(Integer.toString(z));

           }

           else if (c == '*') {
               z = Integer.parseInt((String) S.pop())
                       * Integer.parseInt((String) S.pop());
               S.push(Integer.toString(z));

           }

           else if (c == '/') {
               u = Integer.parseInt((String) S.pop());
               z = Integer.parseInt((String) S.pop()) / u;
               S.push(Integer.toString(z));

           } else if (c == '-') {
               u = Integer.parseInt((String) S.pop());
               z = Integer.parseInt((String) S.pop()) - u;
               S.push(Integer.toString(z));

           } else
               S.push(s);
       }

       System.out.println("THE POSTFIX = " + x);
       System.out.println("THE RESULT = " + S.pop());

   }

   /**
   * @param x
   * @return
   */
   public String postfix(String x) {
       String output = "";

       intopost S = new intopost();
       for (int i = 0; i < x.length(); i++) {
           char c = x.charAt(i);
           if (c == '+' || c == '*' || c == '-' || c == '/') {

               while (!S.empty() && priority(S.peek()) >= priority(c))
                   output += S.pop();

               S.push(c);

           } else if (c == '(')
               S.push(c);
           else if (c == ')') {

               while (!S.peek().equals('('))
                   output += S.pop();
               S.pop();
           } else
               output += c;

       }
       while (!S.empty())
           output += S.pop();

       return output;

   }

   /**
   * @param x
   * @return
   */
   public int priority(Object x) {
       if (x.equals('+') || x.equals('-'))
           return 1;
       else if (x.equals('*') || x.equals('/'))
           return 2;
       else
           return 0;
   }

   public static void main(String[] args) {
       try {
           intopost e = new intopost();
           String infix, post;
           System.out.println("Enter equation in infixform");
           Scanner in = new Scanner(System.in);
           infix = in.next();
           post = e.postfix(infix);
           System.out.println("THE POSTFIX = " + post);
           e.maching(infix);
           e.Evaluation(post);
       } catch (Exception e) {
           // TODO: handle exception
           e.printStackTrace();
       }

   }
}

OUTPUT:

Enter equation in infixform
3+2
THE POSTFIX = 32+
THE POSTFIX = 32+
THE RESULT = 5

Enter equation in infixform
(5+4)
THE POSTFIX = 54+
THE POSTFIX = 54+
THE RESULT = 9