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

Computer Java This is a very simple assignment. Look at the discussion of using

ID: 3693475 • Letter: C

Question

Computer Java

This is a very simple assignment. Look at the discussion of using a Stack to convert an infix expression like 32 + 3 * 7 to postfix like 32 3 7 * +

Write a program that from main();

1. Opens and reads a file called infix.txt which has an infix expression on each line, subject to these limitations:

a. Only integer and the four operators + - * / appear in the expression (no parentheses, no ^ operator)

b. There is one space between any integer and any operator

c. All expressions are legal (so you will not see 32 * * 5 or 32 17 + 1

2. For each line your program a. Prints the infix expression on the screen b. Uses a java.util.Stack to form a String holding the postfix form of the expression, again with spaces between each value and each operator c. Prints the postfix String to the screen d. Writes the postfix String to a file postfix.txt

3. From main() you should a. open the file infix.txt as you saw in AssignedMain.java from the Linked-list assignment b. Scan the entire line c. Implement the Stack-based conversion algorithm as above using a second Scanner to scan the String holding the infix expression

Explanation / Answer

import java.io.IOException;

public class InToPost {
   private Stack theStack;
   private String input;
   private String output = "";
   public InToPost(String in) {
      input = in;
      int stackSize = input.length();
      theStack = new Stack(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 {
      BufferedReader in;

             in = new BufferedReader(new FileReader("infix.txt"));
            String input = in.readLine();
  
      String output;
      InToPost theTrans = new InToPost(input);
      output = theTrans.doTrans();
      System.out.println("Postfix is " + output + ' ');
   }
   class Stack {
      private int maxSize;
      private char[] stackArray;
      private int top;
      public Stack(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);
     }
   }
}

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