Question

write a java program that takes infix notation equations and changes it to postfix notation. you...

write a java program that takes infix notation equations and changes it to postfix notation. you will have to solve the postfix equation after. (make sure you are making your own stack class)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Java Program

import java.util.ArrayList;
import java.util.Scanner;

class SStack<T>{
   private ArrayList<T> list;
   private int top;
  
   public SStack() {
       list = new ArrayList<T>();
       top = -1;
   }
   public boolean isEmpty() {
       return top==-1;
   }
   public T peek() {
       return list.get(top);
   }
   public T pop() {
       T temp = list.get(top);
       top--;
       return temp;
   }
   public void push(T data) {
       top++;
       list.add(top, data);
   }
}
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("Postfix Expression : "+ postfix);

System.out.println("The final result after evaluating the expression is:"+evaluatePostfix(postfix));
in.close();
}
  

public static String infixToPostfix(String exp)
   {
   String result = new String("");
  
   SStack<Character> stack = new SStack<>();
  
   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 double evaluatePostfix(String expression)
   {
   SStack<Double> stack=new SStack<>();
  
   for(int i=0;i<expression.length();i++)
   {
   char c=expression.charAt(i);
  
   if(Character.isDigit(c)) {
         
   stack.push((double)(c-'0')); }
     
   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;
  
   case '^':
       stack.push(Math.pow(val2, val1));
       break;
   }
   }
   }
   return stack.pop();   
   }
}
//sample output

Add a comment
Know the answer?
Add Answer to:
write a java program that takes infix notation equations and changes it to postfix notation. you...
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 that will implement a stack object to convert from either infix notation...

    Write a Java program that will implement a stack object to convert from either infix notation to postfix notation or postfix notation to infix notation.  The program will also implement a link list data structure to track all the conversions done. The Program should have a menu like the following as its output: "Please select what type of conversion you would like to do: Infix to postfix Postfix to infix Print Equations Exit"

  • Using Java- Write a program that takes an arithmetic expression in an infix form, converts it...

    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

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

  • C++ Write a program that takes an infix expression as an input and produces a postfix...

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

  • Complete the following java program in which infix expressions should be converted to postfix expressions /**...

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

  • In C programming Language Write a version of the infix-to-postfix conversion algorithm. Write a program that converts an...

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

  • Code should be written in java Write a program to convert an infix expression to postfix...

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

  • Write a java code to implement the infix to postfix algorithm as described below: Algorithm convertTo...

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

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