Question

Write a java program to convert and print an infix expression to postfix expression. You can...

Write a java program to convert and print an infix expression to postfix expression. You can use Java stack methods. (Must read input from System.in)

Your main method should be as follow:

public static void main(String args[]) {

intopost p = new intopost ();

String iexp, pexp; //infix postfix expression            

try{

Scanner inf = new Scanner (System.in);                    

// Read input from KB/ File

while(inf.hasNext()){

// read next infix expression                                

iexp = inf.next();

// Assume method name to convert infix to postfix is postfix

pexp = p.postfix(iexp);

System.out.printf("\nPosfix of %s is: %s", iexp, pexp);

}// end while

inf.close();// close input file

}catch (Exception e) {prt("\nException " + e + "\n");}

} //end main method
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;

import java.util.Stack;

class intopost {

int precedence(char ch)

{

switch (ch)

{

case '+':

case '-':

return 1;

case '*':

case '/':

return 2;

case '^':

return 3;

}

return -1;

}

public String postfix(String exp)

{

String result = new String("");

  

Stack<Character> stack = new Stack<>();

  

for (int i = 0; i<exp.length(); ++i)

{

char c = exp.charAt(i);

if (Character.isLetterOrDigit(c))

result += c;

else if (c == '(')

stack.push(c);

else if (c == ')')

{

while (!stack.isEmpty() && stack.peek() != '(')

result += stack.pop();

  

if (!stack.isEmpty() && stack.peek() != '(')

return "Invalid Expression"; // invalid expression

else

stack.pop();

}

else {

while (!stack.isEmpty() && precedence(c) <= precedence(stack.peek())) {

if(stack.peek() == '(')

throw new IllegalArgumentException("Invalid exprssion!!");

result += stack.pop();

   }

stack.push(c);

}

}

// pop all the operators from the stack

while (!stack.isEmpty()){

if(stack.peek() == '(')

return "Invalid Expression";

result += stack.pop();

   }

return result;

}

}

public class Main {

public static void main(String args[]) {

intopost p = new intopost ();

String iexp, pexp; //infix postfix expression

try{

Scanner inf = new Scanner(System.in);

// Read input from KB/ File

while(inf.hasNext()){

// read next infix expression

iexp = inf.next();

// Assume method name to convert infix to postfix is postfix

pexp = p.postfix(iexp);

System.out.printf("\nPosfix of %s is: %s", iexp, pexp);

}// end while

inf.close();// close input file

}catch (Exception e) {

System.out.println("\nException " + e + "\n");}

} //end main method

}

1e import java.util.Scanner; 2 import java.util.Stack; 4 class intopost { int precedence(char ch) switch (ch) 800 von case +

Add a comment
Know the answer?
Add Answer to:
Write a java program to convert and print an infix expression to postfix expression. You can...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Could you guys write an efficient java program to implement BST. Your java program should read...

    Could you guys write an efficient java program to implement BST. Your java program should read words and its corresponding French and store it in a BST. Then read every English word and print its corresponding French by searching in BST. Assume your java program is in xxxxx5.java file (5th java project), where xxxxx is the first 5 characters of your last name. To compile: javac xxxxx5.java To execute: java xxxxx5 < any data file name Your main method should...

  • Complete the following java program in which infix expressions should be converted to postfix expressions /**...

    Complete the following java program in which infix expressions should be converted to postfix expressions /** * An algebraic expression class that has operations such as conversion from infix * expression to postfix expression, * and evaluation of a postfix expression */ public class Expression { /** * The infix of this expression */ private String infix; /** * Constructs an expression using an infix. */ public Expression(String infix){ this.infix = infix; } /** * Converts an infix expression into...

  • Code should be written in java Write a program to convert an infix expression to postfix...

    Code should be written in java Write a program to convert an infix expression to postfix expression using stack. Algorithm: a) Create a stack b) For each character t in the input stream If(t is an operand) append t to the output. Else if (t is a right parenthesis) Pop and append to the output until a left parenthesis is popped (but do not append this parenthesis to the output). Else if(t is an operator or left parenthesis) Pop and...

  • C++ Write a program that takes an infix expression as an input and produces a postfix...

    C++ Write a program that takes an infix expression as an input and produces a postfix expression. Use stack to convert an infix expression into postfix expression. Include a function that evaluates a postfix expression.

  • Write a java program for the following: Your program reads an infix expression represented by a...

    Write a java program for the following: Your program reads an infix expression represented by a string S from the standard input (the keyboard). Then your program converts the infix expression into a postfix expression P using the algorithm. Next, your program evaluates the postfix expression P to produce a single result R. At last, your program displays the original infix expression S, the corresponding postfix expression P and the final result R on the standard output ( the screen...

  • Write a java code to implement the infix to postfix algorithm as described below: Algorithm convertTo...

    Write a java code to implement the infix to postfix algorithm as described below: Algorithm convertTo Post fix ( infix) // Converts an infix expression to an equivalent postfix expression operatorStack = a new empty stack postfix = anew empty string while (infix has characters left to parse) nextCharacter =next nonblank character of infix switch (nextCharacter) { case variable: Append nextCharacter to postfix break case 'A' operatorStack.push (nextCharacter) break case '+ case '-' : case '*' : case '/' while...

  • In C programming Language Write a version of the infix-to-postfix conversion algorithm. Write a program that converts an...

    In C programming Language Write a version of the infix-to-postfix conversion algorithm. Write a program that converts an ordinary infix arithmetic expression (assume a valid expression is entered) with single-digit integers For Example: Infix expression (6 + 2) * 5 - 8 / 4 to a postfix expression is  62+5*84/- The program should read the expression into character array infix and use the stack functions implemented in this chapter to help create the postfix expression in character array postfix. The...

  • Programming Assignment 2 – RPN Calculator – Infix to Postfix Conversion and The Evaluations of the Postfix Expression. You are to design and implement and algorithm in Java, to input an Infix expressi...

    Programming Assignment 2 – RPN Calculator – Infix to Postfix Conversion and The Evaluations of the Postfix Expression. You are to design and implement and algorithm in Java, to input an Infix expression , convert to a postfix expression and finally evaluate the postfix expression… Follow the examples done during class lectures… We are used to infix notation - ”3 + 4” - where the operator is between the operands. There is also prefix notation, where the operand comes before...

  • By using PYTHON language Postfix to Infix using Stack Develop a stack application that can convert...

    By using PYTHON language Postfix to Infix using Stack Develop a stack application that can convert Postfix notation to Infix notation using the following algorithm. In your stack application, you can use only two stacks, one for a stack that can store Postfix notation, and the other is a stack to store infix notation. Also, it would help if you had a function to distinguish between an operation or an operand. Input A B C * + D E /...

  • Question: Write a Java program for Evaluating Postfix Expression 1. Input a postfix expression from user....

    Question: Write a Java program for Evaluating Postfix Expression 1. Input a postfix expression from user. 2. Evaluate expression using double stack made of your own linked list. 3. Show the result of expression or error if incorrect. Evaluating Postfix Expression Input an expression: 2 10 + 9 6 - / Evaluating Postfix Expression Input an expression: 20 10 + 9 6 - 1 Evaluating Postfix Expression Input an expression: 2 10 + 9 - / Result = 4.0 Result...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT