Question

Question: Write a Java program for Evaluating Postfix Expression 1. Input a postfix expression from user. 2. Evaluate express
0 1
Add a comment Improve this question Transcribed image text
Answer #1

3 public class StackMethods { public class node /node class double data; node next; OOOHN node top=null; public void push (do

3 public class mainClass2 { static double evaluate Postfix (String exp) Ocon WN //create a stack StackMethods stack = new Stai--; //push the number in stack stack.push(n): // If the scanned character is an operator, pop two // elements from stack appcase 1: stack.push(val2*vall); break; return stack.pop(); public static void main(String[] args) String exp = 2 10 + 9 6 - /

Il Problems @ Javadoc . Declaration Console <terminated> mainClass2 [Java Application) C:\Program Fil Result : 4.0

source code:

package preparation;

public class StackMethods {

   public class node //node class
   {
       double data;
       node next;
   }
  
node top=null;
  
   public void push(double d)
   { //push mwthod
       node newnode=new node();
       newnode.data=d;
       newnode.next=null;
       if(top==null)
           top=newnode;
       else
       {
           newnode.next=top;
       top=newnode;
       }
      
   }
   public double pop()
   {
       double temp=top.data; //pop method
       top=top.next;
       return temp;
      
   }
}

package preparation;

public class mainClass2 {
  
   static double evaluatePostfix(String exp)
{
//create a stack
StackMethods stack = new StackMethods();
  
// Scan 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),extract the number
// Push it to the stack.
else if(Character.isDigit(c))
{
int n = 0;
  
//extract the characters and store it in num
while(Character.isDigit(c))
{
n = n*10 + (int)(c-'0');
i++;
c = exp.charAt(i);
}
i--;
  
//push the number in stack
stack.push(n);
}
  
// If the scanned character is an operator, pop two
// elements from stack apply the operator
else
{
double val1 = stack.pop();
double 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;
}
}
}
return stack.pop();
}
  
  
   public static void main(String[] args)
{
String exp = "2 10 + 9 - /";
System.out.println("Result : "+evaluatePostfix(exp));
}

}

if you have nay doubts ask me..

Add a comment
Know the answer?
Add Answer to:
Question: Write a Java program for Evaluating Postfix Expression 1. Input a postfix expression from user....
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
  • 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...

  • You are to write a program name expressionTree.java that evaluates an infix expression entered by the...

    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...

  • JAVA, please You must write a robust program meaning that your program should not crash with...

    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...

  • This project is designed to practice with OOP, stack data structure, its applications, and C++/Java programming...

    This project is designed to practice with OOP, stack data structure, its applications, and C++/Java programming language. You will write a program that reads an infix expression, converts it to a postfix expression, evaluates the postfix expression, and prints out the answer. You must define and implement your own Stack class and a Calculator class. Your Stack class supports standard basic stack operations and you can implement it with an array or a linked list. You should create a class...

  • 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...

  • Programming in Java Q: Build an expression tree for a given postfix expression. Then compute the ...

    Programming in Java Q: Build an expression tree for a given postfix expression. Then compute the value of the expression by doing an in-order traversal of the expression tree. For example: Your output should print the following: o The input is: "Expression" o The inorder traversal is: "inorder traversal" o The output of the expression is: "output" Please print the output in the same format. (example shown below) The input is: 1 68+ The inorder traversal is: 168 The output...

  • Python Issue Postfix notation (also known as Reverse Polish Notation or RPN in short) is a...

    Python Issue Postfix notation (also known as Reverse Polish Notation or RPN in short) is a mathematical notation in which operators follow all of its operands. It is different from infix notation in which operators are placed between its operands. The algorithm to evaluate any postfix expression is based on stack and is pretty simple: Initialize empty stack For every token in the postfix expression (scanned from left to right): If the token is an operand (number), push it on...

  • For this assignment, you are to write a program, which will calculate the results of Reverse...

    For this assignment, you are to write a program, which will calculate the results of Reverse Polish expressions that are provided by the user. You must use a linked list to maintain the stack for this program. You must handle the following situations (errors): Too many operators (+ - / *) Too many operands (doubles) Division by zero The program will take in a Polish expression that separates the operators and operands by a single space, and terminates the expression...

  • (In Java) you will be creating a GUI for your calculator that evaluates the postfix expression. I...

    (In Java) you will be creating a GUI for your calculator that evaluates the postfix expression. I have already created the postfix class that converts the expression from infix to postfix. Here's the postFix class and its stack class (https://paste.ofcode.org/bkwPyCMEBASXQL4pR2ym43) ---> PostFix class (https://paste.ofcode.org/WsEHHugXB38aziWRrp829n)--------> Stack class Your calculator should have: Text field for the postfix expression Text field for the result Two buttons: Evaluate and Clear You can start with the code written below and improvise to work with this...

  • 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...

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