Question

In: Computer Science

Please show solution and comments for this data structure using java.​ Implement a program in Java...

Please show solution and comments for this data structure using java.​

Implement a program in Java to convert an infix expression that includes (, ), +, -, *,     and / to postfix expression.

For simplicity, your program will read from standard input (until the user enters the symbol “=”) an infix expression of single lower case and the operators +, -, /, *, and ( ), and output a postfix expression.

Solutions

Expert Solution


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 { String input = "1+2*4/5-7+3/6"; String output; InToPost theTrans = new InToPost(input); output = theTrans.doTrans(); System.out.println("Postfix is " + output + '\n'); } 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); } } }


Related Solutions

Hello, Please write this program in java and include a lot of comments and please try...
Hello, Please write this program in java and include a lot of comments and please try to make it as simple/descriptive as possible since I am also learning how to code. The instructions the professor gave was: Create your own class Your own class can be anything you want Must have 3 instance variables 1 constructor variable → set the instance variables 1 method that does something useful in relation to the class Create a driver class that creates an...
Please do this in java program. In this assignment you are required to implement the Producer...
Please do this in java program. In this assignment you are required to implement the Producer Consumer Problem . Assume that there is only one Producer and there is only one Consumer. 1. The problem you will be solving is the bounded-buffer producer-consumer problem. You are required to implement this assignment in Java This buffer can hold a fixed number of items. This buffer needs to be a first-in first-out (FIFO) buffer. You should implement this as a Circular Buffer...
JAVA CODE BEGINNER , Please use comments to explain, please Repeat the calorie-counting program described in...
JAVA CODE BEGINNER , Please use comments to explain, please Repeat the calorie-counting program described in Programming Project 8 from Chapter 2. This time ask the user to input the string “M” if the user is a man and “W” if the user is a woman. Use only the male formula to calculate calories if “M” is entered and use only the female formula to calculate calories if “W” is entered. Output the number of chocolate bars to consume as...
****user comments: PLEASE READ INSTRUCTIONS THOROUGHLY AND KEEP THE STRUCTURE OF THE PROGRAM. JUST ADD THE...
****user comments: PLEASE READ INSTRUCTIONS THOROUGHLY AND KEEP THE STRUCTURE OF THE PROGRAM. JUST ADD THE ADDITIVES NEEDED IN ORDER TO SUFFICE THE PROGRAM. PLEASE MAKE SURE IT IS IN C++ AND WORKS! THANK YOU!**** Write a program that uses a structure to store the following information for a particular month at the local airport: Total number of planes that landed Total number of planes that departed Greatest number of planes that landed in a given day that month Least...
Please use Java language with comments! Thanks! Write a program that will display multiple dots move...
Please use Java language with comments! Thanks! Write a program that will display multiple dots move across the Frame and randomly change direction when they hit the edge of the screen. Do this by creating a Dot class and making each dot an object of that class. You may reuse code written in class for this part of the assignment. Create a subclass of the Dot class called PlayerDot that is controlled by the player through keyboard input (arrow keys)...
This is for a java program. Payroll System Using Inheritance and Polymorphism 1. Implement an interface...
This is for a java program. Payroll System Using Inheritance and Polymorphism 1. Implement an interface called EmployeeInfo with the following constant variables: FACULTY_MONTHLY_SALARY = 6000.00 STAFF_MONTHLY_HOURS_WORKED = 160 2. Implement an abstract class Employee with the following requirements: Attributes last name (String) first name (String) ID number (String) Sex - M or F Birth date - Use the Calendar Java class to create a date object Default argument constructor and argument constructors. Public methods toString - returning a string...
Need some assistance with a java program “Square,” that will implement a data type Square that...
Need some assistance with a java program “Square,” that will implement a data type Square that represents squares with the x and y coordinates of their upper-left corners and the length. The API should be as follows. Square(double x, double y, double length) //constructor double area() //returns the area of the square double perimeter() //returns the perimeter of the square boolean intersects(Square b) //does this square intersect b? Two squares would intersect if they share one or more common points...
JAVA Program 2: In Order Using an IF/ELSE IF/ELSE structure, write a program that will prompt...
JAVA Program 2: In Order Using an IF/ELSE IF/ELSE structure, write a program that will prompt the user for three numbers and displays them in ascending order. First, the program will prompt the user for each of the three numbers. Next, find the smallest value of the three. Then decide which of the other two is the next smallest. Then have the program display the three numbers in ascending order. Be sure to do the following: Determine what the input...
Write a complete Java program, including comments in each method and in the main program, to...
Write a complete Java program, including comments in each method and in the main program, to do the following: Outline: The main program will read in a group of three integer values which represent a student's SAT scores. The main program will call a method to determine if these three scores are all valid--valid means in the range from 200 to 800, including both end points, and is a multiple of 10 (ends in a 0). If the scores are...
Write a complete Java program, including comments in each method and in the main program, to...
Write a complete Java program, including comments in each method and in the main program, to do the following: Outline: The main program will read in a group of three int||eger values which represent a student's SAT scores. The main program will call a method to determine if these three scores are all valid--valid means in the range from 200 to 800, including both end points, and is a multiple of 10 (ends in a 0). If the scores are...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT