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 ). Your program is required to handle at least five mathematic operations, +, -, * and /, and ^(exponential). When you perform division, please do integer division. For example, 7 / 2 is 3. Note that in this program, the input infix expression contains one-digit operands and produces one-digit result. In other words, you do not worry about the infix expressions that involve multiple-digit operands and produce multiple-digit results.
Note that you have to implement your own Stack class in this program. The built-in Java Stack is not allowed in this program.
Please use Java Object type or the generic type E for the data element in your stack implementation. Please catch any potential errors, so that your program will not crash. You do not need to check whether all parentheses in the input infix expression are correctly matched or not. You can safely assume the format of the infix expression is correct. Each run of your program will evaluate only one infix expression.
Sample Run One
Please enter the infix expression to process: (((1+2)-(3-4))/(6-5))
The postfix expression for the input infix is: 1 2 + 3 4 - - 6 5 - /
The final result after evaluating the postfix is: 4
Sample Run Two
Please enter the infix expression to process: 2 * 4 - 2 ^ 2 ^ 1
The postfix expression for the input infix is: 2 4 * 2 2 1 ^ ^ -
The final result after evaluating the postfix is: 4
//Java program
import java.util.Stack;
import java.util.Scanner;
public class InfixtoPostfix {
public static void main(String[] args)
{
String expression;
Scanner in = new Scanner(System.in);
System.out.print("Please enter the infix expression to
process:");
expression = in.nextLine();
String postfix = infixToPostfix(expression);
System.out.println("The postfix expression for the input infix
is:"+postfix);
System.out.println("The final result after evaluating the postfix
is:"+evaluatePostfix(postfix));
in.close();
}
public static String infixToPostfix(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(c==' ')continue;
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 // an operator is encountered
{
while (!stack.isEmpty() && Prefer(c) <=
Prefer(stack.peek()))
result += stack.pop();
stack.push(c);
}
}
// pop all the operators from the stack
while (!stack.isEmpty())
result += stack.pop();
return result;
}
public static int Prefer(char ch)
{
switch (ch)
{
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
}
return -1;
}
public static int evaluatePostfix(String
expression)
{
Stack<Integer> stack=new Stack<>();
for(int i=0;i<expression.length();i++)
{
char c=expression.charAt(i);
if(Character.isDigit(c)) {
stack.push(c-'0'); }
else
{
int val1 = stack.pop();
int val2 = stack.pop();
switch(c)
{
case '+':
stack.push(val2+val1);
break;
case '-':
stack.push(val2- val1);
break;
case '/':
stack.push(val2/val1);
break;
case '*':
stack.push(val2*val1);
break;
case '^':
stack.push((int)Math.pow(val2,
val1));
break;
}
}
}
return stack.pop();
}
}
//sample output
![<terminated> InfixtoPostfix [Java Application] CAProgram FilesJavaljdk1.8.0_151\bin avaw.exe Please enter the infix expressio](http://img.homeworklib.com/questions/ca526060-3359-11ec-a4fa-ffbe18b6143d.png?x-oss-process=image/resize,w_560)
![sterminated> InfixtoPostfix [Java Application] C:AProgram FilesJavajdk1.8.0_151\binjavaw.exe (Feb 25 Please enter the infix e](http://img.homeworklib.com/questions/cad53020-3359-11ec-b5b8-3bfd56fda04a.png?x-oss-process=image/resize,w_560)
Write a java program for the following: Your program reads an infix expression represented by a...
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...
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...
Using Java- Write a program that takes an arithmetic expression in an infix form, converts it to a postfix form and then evaluates it. Use linked lists for the infix and postfix queues and the operator and value stacks. You must use sperate Stack and Queue classes with a driver class to run the program. example Input : 111++ Output : (1+ (1+ 1)) Answer: 3
We as humans write math expression in infix notation, e.g. 5 + 2 (the operators are written in-between the operands). In a computer’s language, however, it is preferred to have the operators on the right side of the operands, i.e. 5 2 +. For more complex expressions that include parenthesis and multiple operators, a compiler has to convert the expression into postfix first and then evaluate the resulting postfix. Write a program that takes an “infix” expression as input, uses...
You are to write a program name expressionTree.java that evaluates an infix expression entered by the user. The expression may contain the following tokens: (1) Integer constants (a series of decimal digits). (2) One alphabetic character - "x" (representing a value to be supplied later). (3) Binary operators (+, -, *, / and % (modulo)). (4) Parentheses You will parse the input expression creating an expression tree with the tokens, then use the postOrder tree traversal algorithm to extract...
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 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...
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...
JAVA, please You must write a robust program meaning that your program should not crash with any given data. Data validation must be done any time that user enters an input. Write a program that 1. Gets an infix expression form the user and evaluate the expression using stack ADT a. Finds the postfix equivalent of the given infix expression b. Evaluate the created postfix expression. c. Note: your program should not crash when you enter an invalid expression such...
Total point: 15 Introduction: For this assignment you have to write a c program that will take an infix expression as input and display the postfix expression of the input. After converting the postfix expression, the program should evaluate the expression from the postfix and display the result. What should you submit? Write all the code in a single file and upload the .c file. Problem We as humans write math expression in infix notation, e.g. 5 + 2 (the...