Need help with infixToPostFix converter class, My driver should be done. Thanks
ID: 3764340 • Letter: N
Question
Need help with infixToPostFix converter class, My driver should be done. Thanks in advance. // Description: Assignment 11 class displays a menu of choices to a user // and performs the chosen task. It will keep asking a user to // enter the next choice until the choice of 'Q' (Quit) is entered. import java.io.*; public class Assignment11 { public static void main (String[] args)throws IOException { char input1; String inputInfo; String line = new String(); printMenu(); InputStreamReader isr = new InputStreamReader(System.in); BufferedReader stdin = new BufferedReader(isr); do // will ask for user input { System.out.print("What action would you like to perform? "); line = stdin.readLine(); input1 = line.charAt(0); input1 = Character.toUpperCase(input1); if (line.length() == 1) { // matches one of the case statements switch (input1) { case 'E': //Enter String System.out.print("Please enter a string. "); inputInfo = stdin.readLine().trim(); System.out.print(InfixToPostfixConverter.convertToPostfix(inputInfo)+" "); break; case 'Q': //Quit break; case '?': //Display Menu printMenu(); break; default: System.out.print("Unknown action "); break; } } else { System.out.print("Unknown action "); } } while (input1 != 'Q' || line.length() != 1); } /** The method printMenu displays the menu to a user**/ public static void printMenu() { System.out.print("Choice Action " + "------ ------ " + "E Enter String " + "Q Quit " + "? Display Help "); } } // Description: This is a utility class that provide a static method that // takes an infix string, checked and determine if parentheses // are matching, if matching, returns a postfix string. import java.util.Stack; public class InfixToPostfixConverter { //********************************************************************** //The precedence method determines the precedence between two operators. //If the first operator is of higher or equal precedence than the second //operator, it returns the value true, otherwise it returns false. //*********************************************************************** public static boolean precedence(char first, char second) { } //************************************************************************* //The static convertToPostfix method will convert the infixString //into the corresponding postfix string. Check the algorithm on //assignment #11's description page. Mark each case clearly inside the code //************************************************************************* public static String convertToPostfix(String infixString) { //initialize the resulting postfix string String postfixString = ""; //initialize the stack Stack<Character> stack1 = new Stack<Character>(); //Obtain the character at index i in the string for (int i=0; i < infixString.length(); i++) { char currentChar = infixString.charAt(i); //Case A: //Case B: //Case C: //Case D: //Case E: } //end of for loop //Case F: }//end of convertToPostfix method }//end of the InfixToPostfixConverter class
Explanation / Answer
import java.io.*;
public class Assignment11 {
public static void main(String[] args) throws IOException {
char input1;
String inputInfo;
String line = new String();
printMenu();
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader stdin = new BufferedReader(isr);
do // will ask for user input
{
System.out.print("What action would you like to perform? ");
line = stdin.readLine();
input1 = line.charAt(0);
input1 = Character.toUpperCase(input1);
if (line.length() == 1) {
// matches one of the case statements
switch (input1) {
case 'E': //Enter String
System.out.print("Please enter a string. ");
inputInfo = stdin.readLine().trim();
System.out.print(InfixToPostfixConverter
.convertToPostfix(inputInfo)
+ " ");
break;
case 'Q': //Quit
break;
case '?': //Display Menu
printMenu();
break;
default:
System.out.print("Unknown action ");
break;
}
} else {
System.out.print("Unknown action ");
}
} while (input1 != 'Q' || line.length() != 1);
}
/** The method printMenu displays the menu to a user**/
public static void printMenu() {
System.out.print("Choice Action " + "------ ------ "
+ "E Enter String " + "Q Quit "
+ "? Display Help ");
}
}
import java.util.Stack;
public class InfixToPostfixConverter {
/**
* Perform the conversion
*
* @param expression Arithmetic infix format
*/
public static String convertToPostfix(String expression) {
// Use StringBuilder to append string is faster than
// String concatenation
StringBuilder sb = new StringBuilder();
// Use a stack to track operations
Stack<Character> op = new Stack<Character>();
// Convert expression string to char array
char[] chars = expression.toCharArray();
// The length of expression character
int N = chars.length;
for (int i = 0; i < N; i++) {
char ch = chars[i];
if (Character.isDigit(ch)) {
// Number, simply append to the result
while (Character.isDigit(chars[i])) {
sb.append(chars[i++]);
}
// Use space as delimiter
sb.append(' ');
} else if (ch == '(') {
// Left parenthesis, push to the stack
op.push(ch);
} else if (ch == ')') {
// Right parenthesis, pop and append to the result until meet the left parenthesis
while (op.peek() != '(') {
sb.append(op.pop()).append(' ');
}
// Don't forget to pop the left parenthesis out
op.pop();
} else if (isOperator(ch)) {
// Operator, pop out all higher priority operators first and then push it to the stack
while (!op.isEmpty() && precedence(op.peek()) >= precedence(ch)) {
sb.append(op.pop()).append(' ');
}
op.push(ch);
}
}
// Finally pop out whatever left in the stack and append to the result
while (!op.isEmpty()) {
sb.append(op.pop()).append(' ');
}
return sb.toString();
}
/**
* Check if a character is an operator
*/
private static boolean isOperator(char ch) {
return ch == '^' || ch == '*' || ch == '/' || ch == '+' || ch == '-';
}
/**
* Define the operator priority
*/
private static int precedence(char operator) {
switch (operator) {
case '^':
return 3;
case '*':
case '/':
return 2;
case '+':
case '-':
return 1;
}
return 0;
}
}
OUTPUT:
Choice Action
------ ------
E Enter String
Q Quit
? Display Help
What action would you like to perform?
E
Please enter a string.
1 * ( 12 + 23 ) - ( 4 / 5 )
1 12 23 + * 4 5 / -
What action would you like to perform?
?
Choice Action
------ ------
E Enter String
Q Quit
? Display Help
What action would you like to perform?
E
Please enter a string.
1 * ( 12 + 23 ) - ( 4 / 5 )
1 12 23 + * 4 5 / -
What action would you like to perform?
q