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...
JAVA CODE Write a program which has a 3D array and prints the integers from the...
JAVA CODE Write a program which has a 3D array and prints the integers from the array. Then then convert the output into a file using filewriter
Stack Queue Program Implement a Stack class using the List Class you developed earlier. Test your...
Stack Queue Program Implement a Stack class using the List Class you developed earlier. Test your Stack class by determining if the following strings have matching open and closing ( ) and/or [ ] and/or { }. To test matches, push open (, [, { onto the stack. Input a close char, pop off the stack and see if input matches symbol form stack.   Use the following data to test your code:                               ( )                               [ ] ( )...
Using Java, in the most simple algorithm please Implement a static stack class of char. Your...
Using Java, in the most simple algorithm please Implement a static stack class of char. Your class should include two constructors. One (no parameters) sets the size of the stack to 10. The other constructor accepts a single parameter specifying the desired size of the stack a push and pop operator an isEmpty and isFull method . Both return Booleans indicating the status of the stack
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 prompts the user to input a word (String). The program must...
Write a Java program that prompts the user to input a word (String). The program must print the reversed word with all consecutive duplicate characters removed. The program must contain the following classes: - The StackX class (you can use the Java Stack class). - The Reverse class which must contain a private data field called “word” of type string, a constructor, and a void method called revNoDup(). The revNoDup() method must reverse the word and remove the consecutive duplicate...
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.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT