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

In this project, you will develop a program that receives infix expressions, con

ID: 646776 • Letter: I

Question

In this project, you will develop a program that receives infix expressions, convert them to postfix expressions, and then evaluate the produced post expressions. Use a stack and a queue in your program.

If you have the time and inclination, you may submit, in addition to the specified keyboard input calculator, a version that uses a graphical interface. This interface should have button for each of the numeric digits and decimal, operators, clear and quit, and a enter key to be used between adjacent floating point inputs. The GUI version of the program should have its main() method in a separate class called RPNCalcGUI. Thus, the two version are executed as two separate programs. The GUI version of the program will be worth up to an addition 50% in the grading process. You may use the java.io and java.lang class libraries (and the java.awt or javax.swing library if you are doing the extra credit GUI - you should write any such GUI code yourself, rather than using generators in some IDE).

Using the following expressions to test your program:

1. 10 * (5

Explanation / Answer

package demo;

import java.io.DataInputStream;
import java.io.IOException;
import java.util.Scanner;

public class InToPost {
   private Stack2 theStack;
   private String input;
   private String output = "";
   public InToPost(String in) {
      input = in;
      int stackSize = input.length();
      theStack = new Stack2(stackSize);
   }
   public String doTrans() {
      for (int j = 0; j < input.length(); j++) {
         char ch = input.charAt(j);
         switch (ch) {
            case '+':
            case '-':
            gotOper(ch, 1);
            break;
            case '*':
            case '/':
            gotOper(ch, 2);
            break;
            case '(':
            theStack.push(ch);
            break;
            case ')':
            gotParen(ch);
            break;
            default:
            output = output + ch;
            break;
         }
      }
      while (!theStack.isEmpty()) {
         output = output + theStack.pop();
      }
      System.out.println(output);
      return output;
   }
   public void gotOper(char opThis, int prec1) {
      while (!theStack.isEmpty()) {
         char opTop = theStack.pop();
         if (opTop == '(') {
            theStack.push(opTop);
            break;
         }
         else {
            int prec2;
            if (opTop == '+' || opTop == '-')
            prec2 = 1;
            else
            prec2 = 2;
            if (prec2 < prec1) {
               theStack.push(opTop);
               break;
            }
    else
            output = output + opTop;
         }
      }
      theStack.push(opThis);
   }
   public void gotParen(char ch){
      while (!theStack.isEmpty()) {
         char chx = theStack.pop();
         if (chx == '(')
         break;
         else
         output = output + chx;
      }
   }
   public static void main(String[] args)
   throws IOException {
      String input = "";
     
     
      while(!input.matches("quit"))
      {
    System.out.println("Enter any infix or quit to exit");
    Scanner scan = new Scanner(System.in);
        input = scan.next();
     
     
     
      String output;
      InToPost theTrans = new InToPost(input);
      output = theTrans.doTrans();
      System.out.println("Postfix is " + output + ' ');
     
     
      if(output.equals(""))
          break;
        Evaluation e=new Evaluation();
        System.out.println("Result:- "+e.calculate(output));
      }
      }
   }
   class Stack2 {
      private int maxSize;
      private char[] stackArray;
      private int top;
      public Stack2(int max) {
         maxSize = max;
         stackArray = new char[maxSize];
         top = -1;
      }
      public void push(char j) {
         stackArray[++top] = j;
      }
      public char pop() {
         return stackArray[top--];
      }
      public char peek() {
         return stackArray[top];
      }
      public boolean isEmpty() {
         return (top == -1);
     }
   }
  
  
   class Stack1
   {
      private int[] a;
      private int top,m;
      public Stack1(int max)
      {
        m=max;
        a=new int[m];
        top=-1;
      }
      public void push(int key)
      {
        a[++top]=key;
      }
      public int pop()
      {
        return(a[top--]);
      }
   }
   class Evaluation{
      public int calculate(String s)
      {
        int n,r=0;
        n=s.length();
        Stack1 a=new Stack1(n);
        for(int i=0;i<n;i++)
        {
          char ch=s.charAt(i);
          if(ch>='0'&&ch<='9')
            a.push((int)(ch-'0'));
          else
          {
            int x=a.pop();
            int y=a.pop();
            switch(ch)
            {
              case '+':r=x+y;
                 break;
              case '-':r=y-x;
                 break;
              case '*':r=x*y;
                 break;
              case '/':r=y/x;
                 break;
              default:r=0;
            }
            a.push(r);
          }
        }
        r=a.pop();
        return(r);
      }
      public static String getString()throws IOException
      {
        DataInputStream inp=new DataInputStream(System.in);
        String s=inp.readLine();
        return s;
      }
   }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote