Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Could someone add notes to this code to help me understand what it is doing Example: //using the return variable as the recursive call perform

Could someone add notes to this code to help me understand what it is doing Example:

//using the return variable as the recursive call perform the step case. //and store in the return variable total = x + total; //then take the step towards the base case y = y - 1; 

CODE:

public class InfixToPostfix { Stack 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 + ' '); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M Kroenke, David J Auer

6th Edition

0132742926, 978-0132742924

More Books

Students also viewed these Databases questions

Question

Where is one place you've always dreamed of going?

Answered: 1 week ago

Question

16. Implement this MPEMP.

Answered: 1 week ago

Question

11. Store this politicalecological data in the data sets directory.

Answered: 1 week ago