Question

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

Homework Answers

Answer #1

//Code Link => https://repl.it/@FAYAZPASHA/InnocentAgonizingQuote#Main.java

import java.util.Stack;

import java.util.*;

class Main

{

  

  static int Precdence(char ch) //returns the character priority

  {

    switch (ch)

    {

    case '+':

    case '-':

      return 1;

  

    case '*':

    case '/':

      return 2;

  

    case '^':

      return 3;

    }

    return -1;

  }

  

  static String infixToPostfix(String exp)

  {

    // initializing empty String for result

    String result = new String("");

    

    // initializing empty stack

    Stack<Character> stack = new Stack<>();

    

    for (int i = 0; i<exp.length(); ++i)

    {

      char c = exp.charAt(i);

      

      // If the scanned character is an operand, add it to output.

      if (Character.isLetterOrDigit(c)) //if we encounter a operand we append to our results

        result += c;

      

    

      else if (c == '(') //if we encountered a opening parenthesis we push it to the stack

        stack.push(c);

      

      else if (c == ')') // when we encounter a closing parenthesis we check for the priority of the operator residing inside the stack

      {

        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() && Precdence(c) <= Precdence(stack.peek())){

          if(stack.peek() == '(')

            return "Invalid Expression";

          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;

  }

  

  // Driver method

  public static void main(String[] args)

  {

Scanner sc = new Scanner(System.in);

String exp = sc.nextLine();

    // String exp = "a+b*(c^d-e)^(f+g*h)-i";

    System.out.println(infixToPostfix(exp));

  }

}

Know the answer?
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
Similar Questions
in Java In this exercise, you'll write a Java version of the infix-to-postfix conversion algorithm. These...
in Java In this exercise, you'll write a Java version of the infix-to-postfix conversion algorithm. These same mechanisms can be used as a part of writing a simple compiler. Write class InfixToPostfixConverter co convert an ordinary infix arithmetic expression (assume a valid expression is entered) with single-digit integers (to make things easier) such as (6 + 2) • 5 - 8 / 4 to a postfix expression. The postfix version (no parentheses are needed) of this infix expression is 6...
Convert the given infix expression to a postfix expression: a + b * c
Convert the given infix expression to a postfix expression: a + b * c
Convert the following infix expressions to postfix. a * b + c – d a +...
Convert the following infix expressions to postfix. a * b + c – d a + b / (c + d)
You are required to write a program in JAVA based on the problem description given. Read...
You are required to write a program in JAVA based on the problem description given. Read the problem description and write a complete program with necessary useful comment for good documentation. Compile and execute the program. ASSIGNMENT OBJECTIVES: • To introduce queue data structure. DESCRIPTIONS OF PROBLEM: Exercise : Write a program to reverse element of a stack. For any given word (from input), insert every character (from the word) into a stack. The output from the stack should be...
Write a program that reads three integer values from the keyboard using the Scanner class representing,...
Write a program that reads three integer values from the keyboard using the Scanner class representing, respectively, a number of quarters, dimes, and nickels. Convert the total coin amount to dollars and output the result.Write a program that reads three integer values from the keyboard using the Scanner class representing, respectively, a number of quarters, dimes, and nickels. Convert the total coin amount to dollars and output the result.
Write a Java program that reads words from a text file and displays all the words...
Write a Java program that reads words from a text file and displays all the words (duplicates allowed) in ascending alphabetical order. The words must start with a letter. 1. You must use one of following Concrete class to store data from the input.txt file. Vector, Stack, ArrayList, LinkedList 2. To sort those words, you should use one of existing interface methods available in Collection or List class.
Use C++ Your program should expect as input from (possibly re-directed) stdin a series of space-...
Use C++ Your program should expect as input from (possibly re-directed) stdin a series of space- separated strings. If you read a1 (no space) this is the name of the variable a1 and not "a" followed by "1". Similarly, if you read "bb 12", this is a variable "bb" followed by the number "12" and not "b" ,"b", "12" or "bb", "1" ,"2". Your program should convert all Infix expressions to Postfix expressions, including expressions that contain variable names. The...
Find is the final result of evaluating the following postfix expression using a stack. Show each...
Find is the final result of evaluating the following postfix expression using a stack. Show each push and pop operation. 85 5 / 4 * 5   6 +   10    5 -   * +
Using for loops: - write a Java program that adds numbers between 1 to 100 and...
Using for loops: - write a Java program that adds numbers between 1 to 100 and prints the result. - write a Java program that adds odd numbers between 1 to 100 and prints the result - write a Java program that averages even numbers between 5 to 30 and prints the result
Java Please [(1)] A palindrome is a string that reads the same forwards as backwards. Using...
Java Please [(1)] A palindrome is a string that reads the same forwards as backwards. Using only a fixed number of stacks and queues, the stack and queue ADT functions, and a fixed number of int and char variables, write an algorithm to determine if a string is a palindrome. Assume that the string is read from standard input one character at a time. The algorithm should output true or false as appropriate [(2)] Let Q be a non-empty queue,...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT