Could someone add notes to this code to help me understand what it is doing Exam
ID: 3813289 • Letter: C
Question
Could someone add notes to this code to help me understand what it is doing Example:
CODE:
public class InfixToPostfix {
Stack<Character> stack;
private String infix;
private String postfix = "";
public InfixToPostfix() {
stack = new Stack();
}
public String Convert() {
for (int j = 0; j < infix.length(); j++) {
char ch = infix.charAt(j);
switch (ch) {
case '+':
case '-':
Precdence(ch, 1);
break;
case '*':
case '/':
Precdence(ch, 2);
break;
case '(':
stack.push(ch);
break;
case ')':
gotParen(ch);
break;
default:
postfix = postfix + ch;
break;
}
}
while (!stack.isEmpty()) {
postfix = postfix + stack.pop();
}
return postfix;
}
public void Precdence(char opThis, int prec1) {
while (!stack.isEmpty()) {
char opTop = stack.pop();
if (opTop == '(') {
stack.push(opTop);
break;
} else {
int prec2;
if (opTop == '+' || opTop == '-')
prec2 = 1;
else
prec2 = 2;
if (prec2 < prec1) {
stack.push(opTop);
break;
}
else postfix = postfix + opTop;
}
}
stack.push(opThis);
}
public void gotParen(char ch) {
while (!stack.isEmpty()) {
char chx = stack.pop();
if (chx == '(')
break;
else
postfix = postfix + chx;
}
}
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Infix Expression:");
String input = sc.nextLine();
String output;
InfixToPostfix infixToPostfix = new InfixToPostfix();
infixToPostfix.infix = input;
output = infixToPostfix.Convert();
System.out.println("Postfix expression is: " + output + ' ');
}
}
Explanation / Answer
Code With Comments:
/* Class that converts Infix expression into Postfix expression */
public class InfixToPostfix {
//STL Stack to hold characters
Stack<Character> stack;
//For storing infix expression
private String infix;
//For storing postfix expression
private String postfix = "";
//Constructor that initializes Stack class object
public InfixToPostfix() {
stack = new Stack();
}
/* Method that converts Infix expression to Postfix Expression */
public String Convert() {
//Iterating over each character of infix expression
for (int j = 0; j < infix.length(); j++) {
//Fetching character present index j
char ch = infix.charAt(j);
/*
If current character is a Operator(+,-,*,/) or braces((,)) then based on operator precedence rules either push or write to postfix expression string
If it is operand, write to postfix expression string
*/
switch (ch) {
// For Operators, +, -
case '+':
case '-':
//Checking precedence
Precdence(ch, 1);
break;
// For Operators, *, /
case '*':
case '/':
//Checking precedence
Precdence(ch, 2);
break;
//If character is a open braces (, push to stack
case '(':
//Push operation on stack
stack.push(ch);
break;
//If character is a closing brace ), pop all elements from stack
case ')':
//Popping all elements from stack
gotParen(ch);
break;
//Current character is a Operand, write to postfix string
default:
postfix = postfix + ch;
break;
}
}
//Popping all elements and writing to postfix string
while (!stack.isEmpty()) {
postfix = postfix + stack.pop();
}
//Return postfix expression
return postfix;
}
//Method that operates based on operator precedence
public void Precdence(char opThis, int prec1) {
//Popping elements from Stack until it is empty
while (!stack.isEmpty()) {
//Popping an element from stack
char opTop = stack.pop();
//If top element is (, Push the element
if (opTop == '(') {
stack.push(opTop);
break;
} else {
int prec2;
//Operators +, - have precedence of 1
if (opTop == '+' || opTop == '-')
prec2 = 1;
//Operators *, / have precedence of 2
else
prec2 = 2;
/*
If operator on top of the stack has a higher precedence than current operator read, pop from stack and write to postfix expression string and then push the new operator on to stack
*/
if (prec2 < prec1) {
//Pushing elements onto stack
stack.push(opTop);
break;
}
//write to postfix expression string
else postfix = postfix + opTop;
}
}
//Pushing on to stack
stack.push(opThis);
}
//Method that pop all elements till open brace is encountered
public void gotParen(char ch) {
//Iterate till open brace ( is encountered or stack is empty
while (!stack.isEmpty()) {
//Pop an element from stack
char chx = stack.pop();
//If it is (, break the loop
if (chx == '(')
break;
else
//Writing to postfix string
postfix = postfix + chx;
}
}
//Main method
public static void main(String[] args) throws IOException {
//Scanner class object
Scanner sc = new Scanner(System.in);
//Prompting for infix expression
System.out.println("Enter Infix Expression:");
//Reading infix expression
String input = sc.nextLine();
String output;
//Creating a InfixToPostfix class object
InfixToPostfix infixToPostfix = new InfixToPostfix();
//Storing infix expression
infixToPostfix.infix = input;
//Getting Postfix expression
output = infixToPostfix.Convert();
//Displaying Postfix expression
System.out.println("Postfix expression is: " + output + ' ');
}
}