Question
import java.io.IOException; import java.util.Scanner; import java.util.Stack; /** * * @author */ public class InfixToPostfix { public static void main(String argv[]) throws IOException { String infix;
import java.io.IOException;
import java.util.Scanner;
import java.util.Stack;
/**
*
* @author
*/
public class InfixToPostfix {
public static void main(String argv[]) throws IOException
{
String infix;
Stack stack = new Stack();
//create an input stream object
Scanner sc=new Scanner(System.in);
//get input from user
System.out.print(" Enter the algebraic expression in infix: ");
infix =sc.next();
if(infix.charAt(0)=='q'&&infix.length()==1){
System.out.println("Shutting down... Goodbye!");
System.exit(-1);//When the user enters the letter q instead of a number, the program terminates.
}
//output as postfix
String postExp=toPostfix(infix);
System.out.println("The expression in postfix is:" + postExp);
}
private static String toPostfix(String infix)
//converts an infix expression to postfix
{
char symbol;
String postfix = "";
return postfix;
}
static int prec(char x)
{
if (x == '+' || x == '-')
return 1;
if (x == '*' || x == '/' || x == '%')
return 2;
return 0;
}
}
Java ,thanks!
Instructions: A) Goal: To gain experience with the Stack Data Structure B) Task: You are to write a program that converts an infix expression entered by the user to a postfix expression. The expression may contain the following tokens: (1) Integer constants (2) Binary operators (+,-, *, / and %) (3) Parentheses Spaces between tokens are allowed but not required. The program will convert the expression to postfix (RPN) form and display the converted expressiorn When the user enters only one single letter q, the program terminates Here are some other additional requirements for this program (1) (2) You must use stack objects during the translation from infix to postfix Operators must have the correct precedence C) Procedure 1. Download the attached class 2. Hints: You only need to implement the method of toPostfix. In this method: First, build a instance of stack with datatype of element "Character" Second, using a for loop to get the individual symbols If the symbol is a letter or digit, attach to the postfix(assume postfix is your output), for example, you can use postfix+=symbol to attach this symbol I. II. IV. If this symbol', push to stack V.else if symbo)push everything back to postfix, here you need a while loop The condition is stack.peek()(' Else...in this case you have another operator, using the prec() to get the precedence Vi. Finally, check the stack is empty or not, if yes, pop all to postfixStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started