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 methodimport 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
}

Write a java program to convert and print an infix expression to postfix expression. You can...
Write an efficient java program to implement BST. Your java program should read n 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 engtofren.java file To compile: javac engtofren.java To execute: java engtofren< any data file name Your main method should be as follow: public static void main(String args[]) { engtofren bst = new engtofren (); // try{...
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...
Write a program to convert an expression written in infix notation to be converted to postfix notation. The program must do the following: a. Read a string of characters representing an expression in infix notation. The '$' is to be added at the end of the string to mark its ending. Each character is a letter, digit, +,-,*, or /. If a character is any other character an error must be signaled and the program is terminated b. Use stacks...
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 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...
Convert infix to postfix, and evaluate postfix using custom Stack created using a singly linked list. This is only supposed to use THAT method, calling a normal Stack will give me a zero. I do have the conversion to postfix, but there may be error in there. But the main problem currently is the evaluation of postfix. I keep getting an error that I made for an empty stack, which I will include. For testing it is only supposed to...
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 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 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...
Write a program in c++ to convert an expression written in infix notation to be converted to postfix notation. The program must do the following: a. Read a string of characters representing an expression in infix notation. The '$' is to be added at the end of the string to mark its ending. Each character is a letter, digit, +,-,*, or /. If a character is any other character an error must be signaled and the program is terminated b....