Question

In: Computer Science

Write in Java Postfix notation is a way of writing expressions without using parentheses. For example,...

Write in Java

Postfix notation is a way of writing expressions without using parentheses. For example, the expression (1 + 2) * 3 would be written as 1 2 + 3 *. A postfix expression is evaluated using a stack. Scan a postfix expression from left to right. A variable or constant is pushed into the stack. When an operator is encountered, apply the operator with the top two operands in the stack and replace the two operands with the result.. Write a program to evaluate postfix expressions. Pass the expression as a command-line argument in one string.

Command line argument:

“1 2 + 3 *”

Correct output (3 points):

9

Solutions

Expert Solution

import java.util.Scanner;
import java.util.Stack;

public class PostFixEval
{
// Method to evaluate value of a postfix expression
public static int evaluateExpression(String exp)
{
//create a stack
Stack<Integer> expressionStack=new Stack<>();
  
// iterate all characters one by one
for(int i=0;i<exp.length();i++)
{
char c=exp.charAt(i);
if(c==' ')
   continue;
// If the scanned character is an operand (number here),
// push it to the stack.
if(Character.isDigit(c))
expressionStack.push(c - '0');
  
// If the scanned character is an operator, pop top 2 and apply operation
else
{
int num1 = expressionStack.pop();
int num2 = expressionStack.pop();
  
switch(c)
{
case '+':
expressionStack.push(num2+num1);
break;
  
case '-':
expressionStack.push(num2- num1);
break;
  
case '/':
expressionStack.push(num2/num1);
break;
  
case '*':
expressionStack.push(num2*num1);
break;
}
}
}
return expressionStack.pop();   
}
  
public static void main(String[] args)
{
   Scanner sc = new Scanner(System.in);
   System.out.println("Enter expression");
String exp=sc.nextLine();
System.out.println("postfix evaluation: "+evaluateExpression(exp));
}
}

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me


Related Solutions

(Convert infix to postfix) Note: Postfix notation is a way of writing expression without using parentheses....
(Convert infix to postfix) Note: Postfix notation is a way of writing expression without using parentheses. For example, the expression ( 11 + 12 ) * 13 would be written as 11 12 + 13 * Assume that ALWAYS there is a space between operands and operators in the input expression. Use two stacks, one to store the operands and one to store the operators. Your program only accpets following operators : ( ) + - / * Write a...
CS 209 Data Structure (Postfix notation) Postfix notation is a way of writing expressions without using...
CS 209 Data Structure (Postfix notation) Postfix notation is a way of writing expressions without using parentheses. For example, the expression (1 + 2) * 3 would be written as 1 2 + 3 *. A postfix expression is evaluated using a stack. Scan a postfix expression from left to right. A variable or constant is pushed into the stack. When an operator is encountered, apply the operator with the top two operands in the stack and replace the two...
Write a program that converts prefix expressions to postfix and postfix expressions to prefix. (java) The...
Write a program that converts prefix expressions to postfix and postfix expressions to prefix. (java) The program for this project should consist of three classes. -The main class should create a GUI that allows the user to input an expression in either prefix or postfix and convert it to postfix or prefix, respectively. -The other class should contain the code to perform the conversions. --->The pseudocode for prefix to postfix, which requires two stacks, is shown below: tokenize the string...
Using Java 8. Write a program that reads an expression in postfix notation, builds the expression...
Using Java 8. Write a program that reads an expression in postfix notation, builds the expression tree and prints the expression in prefix and infix notation and evaluates the expression. (Hint use a stack)
C++ Data Structure Write a program to change following infix expressions to postfix expressions using a...
C++ Data Structure Write a program to change following infix expressions to postfix expressions using a stack a) D-B+C b) C*D+A*B c) (A*B)*C+D*F-C d) (A-4*(B-C)-D/E)*F
write a java program tht evaluates postfix expressions. args[0] = the output file the answer will...
write a java program tht evaluates postfix expressions. args[0] = the output file the answer will print to
Your assignment for this program is to evaluate a numeric expression in postfix notation using a...
Your assignment for this program is to evaluate a numeric expression in postfix notation using a dynamic (pointer based) stack. As stated in the handout, in order to evaluate a numeric expression, a compiler converts an infix numeric expression to postfix notation and then it uses an algorithm and a stack to evaluate the expression. Your program should implement the pseudocode algorithm described in the attached handout. Your program will read and evaluate expressions stored in an input file (infile.txt)....
Write a program to check the mathematical expressions containing the following parentheses for validity:                 (         &n
Write a program to check the mathematical expressions containing the following parentheses for validity:                 (                 [                 { Hint: {(6 – 2) * 8} is a valid expression. [3 + (6 * 7) } is an invalid expression.
Write a java class program to convert from INFIX TO POSTFIX Using stack operations
Write a java class program to convert from INFIX TO POSTFIX Using stack operations
1) Consider the following infix expressions. What is the equivalent postfix (reverse Polish notation) expression? 16/(5+3)b)...
1) Consider the following infix expressions. What is the equivalent postfix (reverse Polish notation) expression? 16/(5+3)b) A*B+C*Dc) X × Y + W × Z + V × U 2) Consider the postfix (reverse Polish notation) 10 5 + 6 3 - /. What is the equivalent infix expression?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT