Maniruzzaman Akash's Blog

Maniruzzaman Akash, A programmer, A web programmer, a helpful person to share knowledge and everything..

  • Home
  • Programming
    • C Programming
    • Java Programming
    • C++ Programming
    • C# Programming
    • Python Programming
    • Data Structure
  • Web
    • HTML
    • CSS
    • Javascript
    • PHP
    • AJAX
    • XML
  • FrameWork
    • Bootstrap(CSS)
    • Laravel 5(PHP)
  • Database
    • MySQL
    • Oracle
    • SQL Server
  • Android
    • Android Development
  • Mathematics
    • Numerical Methods
  • Others
    • My Articles
    • Download Ebooks
    • Mathematics
    • Difference Between
  • Contact
Data Structure Java Programming

Infix to postfix code in java language : Data structures

Friday, February 3, 2017 By Maniruzzaman Akash 0 Comments
Infix to postfix code in java language : Data structures:

Description:
When we give any calculating instruction to our computer or mobile phones then, it calculate that by method or a converting method. When we give some input in computer it is in the infix expression. Look in the table.And finally we get the result.

Like, if we calculate 4 * 5 + 3 then, result is 23. And in this case this 4 * 5 + 3  is the infix notation, and finally we get the result by converting this expression to postfix and after evaluating that we can get the result of 23. So, let's look at the following table of infix to postfix expression.

Infix ExpressionPrefix ExpressionPostfix Expression
A + B+ A BA B +
A + B * C+ A * B CA B C * +

Code Demonstration of infix to postfix convert java code:



package infixtopostfix;



import java.util.Stack;

import java.util.Scanner;



public class InfixToPostfix {

    static int getPrecedence(char checkChar)

    {

        if(checkChar=='+'||checkChar=='-')

            return 1;

         if(checkChar=='*'||checkChar=='/')

            return 2;

         if(checkChar=='('||checkChar==')')

            return 0;

         return -1;   

    }



    public static void main(String[] args) {

       Stack<Character> stack=new Stack();

       Scanner scanner =new Scanner(System.in);

       String result="";

       String inputStr=scanner.nextLine();

       char[] inputCharArray=inputStr.toCharArray();

       for(char chrac:inputCharArray)

           System.out.println(chrac);

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

       {

           char checkChar=inputCharArray[i];

           if(checkChar!='+'&&checkChar!='-'&&checkChar!='/'&&checkChar!='*'&&checkChar!='('&&checkChar!=')')

           {

               result=result+checkChar;

           }

          

          

           else 

           {    

               if(checkChar!='('&&checkChar!=')')

               {

                   if(stack.isEmpty())

               {

                   stack.push(checkChar);

               }

               else

               {

                

                while(getPrecedence(stack.peek())>=getPrecedence(checkChar))

              {  

                  result=result+stack.pop();

                if(stack.isEmpty())

                    break;

              }

             stack.push(checkChar);

               }

              

               }

               else

               {

                   if(checkChar=='(')

                       stack.push(checkChar);

                   else

                   {

                       while(stack.peek()!='(')

                       {

                           result=result+stack.pop();

                       }

                       stack.pop();

                   }

               }

       }

          

          

    }

       while(!stack.isEmpty())

           result=result+stack.pop();

      

       System.out.println(result);

}

}



This is a simple code if mine of infix to postfix notation in java language.

Demonstrate the code of infix to postfix:

I've written a very simple code not so hard. We can understand it quickly hopefully.
First look at the infix to postfix algorithm.

In this Algorithm we are reading token from Left to Right and Postfix expression is generated.

(1) So Entered Token may be –

  1. Alphabet from A-Z or a-Z
  2. Numerical Digit from 0-9
  3. Operator
  4. Opening And Closing Braces ( , )

(2) If Entered Character is Alphabet then Following Action Should be taken-

  1. Print Alphabet as Output

(3) If Entered Character is Digit then Following Action Should be taken-

  1. Print Digit as Output

(4) If Entered Character is Opening Bracket then Following Action Should be taken-

  1. Push ‘(‘ Onto Stack

(5) If any Operator Appears before ‘)’ then

  1.  Push it onto Stack.
  2. If Corresponding ‘)’ bracket appears then Start Removing Elements [Pop] from Stack till ‘(‘ is removed.

(6) If Entered Character is Operator then Following Action Should be taken-

  1. Check Whether There is any Operator Already present in Stack or not.
  2. If Stack is Empty then Push Operator Onto Stack.
  3. If Present then Check Whether Priority of Incoming Operator is greater than Priority of Topmost Stack Operator.
  4. If Priority of Incoming Operator is Greater then Push Incoming Operator Onto Stack.
  5. Else Pop Operator From Stack again goto Step 6.

Domonstrate:
We may get a number or operator and we take our operation character by character.

  1. If character is alphabet or digit then print that as output.
  2. If get "(" then push in into stack and when get any ")" then search upto ")" and removing value from this to stack and remove the "("
  3. If any character entered then check the operator precedence of that character and do like as algorithm rule 6.


By this method,
    static int getPrecedence(char checkChar)

    {

        if(checkChar=='+'||checkChar=='-')

            return 1;

         if(checkChar=='*'||checkChar=='/')

            return 2;

         if(checkChar=='('||checkChar==')')

            return 0;

         return -1;   

    }
We've check the operator precedence of the character given.

And in this line,
       String inputStr=scanner.nextLine();

       char[] inputCharArray=inputStr.toCharArray();

       for(char chrac:inputCharArray)

           System.out.println(chrac);

Make an inputCharArray of character and by using foreach loop print all of them.

By this line:
for(int i=0;i<inputCharArray.length;i++)
Run the loop upto input character length.

By this line:
if(checkChar!='+'&&checkChar!='-'&&checkChar!='/'&&checkChar!='*'&&checkChar!='('&&checkChar!=')')

           {

               result=result+checkChar;

           }
If check character is Alphabet or digit then add this to result.

And by this line:
if(checkChar!='('&&checkChar!=')')

               {

                   if(stack.isEmpty())

               {

                   stack.push(checkChar);

               }

               else

               {

                

                while(getPrecedence(stack.peek())>=getPrecedence(checkChar))

              {  

                  result=result+stack.pop();

                if(stack.isEmpty())

                    break;

              }

             stack.push(checkChar);

               }

If check character is +, -, * or / then check the precedence. Pop the higher precedence from the output and add it to result and push it to stack.

Finall by this line:
       while(!stack.isEmpty())

           result=result+stack.pop();

      

       System.out.println(result);
Run the same procedure upto stack is empty. When stack empty add the stack values to the results.

That's the solution, if you face any problem in this code, please make a comment.


Data Structure
Share:

Maniruzzaman Akash
Maniruzzaman Akash, an enthusiastic programmer, a web developer

Related Articles


0 comments:

Post a Comment

Newer Post Older Post Home
Subscribe to: Post Comments ( Atom )

Popular Posts

  • Numerical Methods 20 Multiple Choice Questions and Answers
  • Consider a hypothetical 32-bit microprocessor having 32-bit instructions: Solutions
  • List and briefly define two approaches to dealing with multiple interrupts
  • The hypothetical machine has two I/O instructions: 0011= Load AC fro I/O 0111= Store AC to I/O Solutions
  • What are the characteristics of Digital IC's?-Solution
  • Mid Square Method Code implementation in C and MatLab
  • List and briefly define the possible states that define an instruction execution
  • BFS, DFS, DLS in Tree implementation in C
  • Download Laravel Offline Documentation as HTML
  • Simpson's 1/3 Code in Matlab

Category

Advanced PHP Android Developement Articles Artificial Intelligenece Blogger Tips Blogging Career Bootstrap Offline Documentation Bootstrap Templates C Programming Computer Architecture Data Structure Difference Between Download Documentation Download Ebook Download Free Blog Template Earning Money Electrical Electronics Guest Posts HTML Java Programming Laravel Laravel Bangla Tutorial MatLab Code My Videos MySQL Database Numerical Methods Offline Documentation Recent Topics Simulation and Modeling Unity Game Development Web Design Web Development

LIKE ME ON FACEBOOK

TAGS

  • Advanced PHP (3)
  • Android Developement (5)
  • Articles (6)
  • Artificial Intelligenece (3)
  • Blogger Tips (5)
  • Blogging Career (1)
  • Bootstrap Offline Documentation (1)
  • Bootstrap Templates (1)
  • C Programming (14)
  • Computer Architecture (5)
  • Data Structure (11)
  • Difference Between (1)
  • Download Documentation (2)
  • Download Ebook (3)
  • Download Free Blog Template (2)
  • Earning Money (1)
  • Electrical Electronics (1)
  • Guest Posts (1)
  • HTML (4)
  • Java Programming (2)
  • Laravel (3)
  • Laravel Bangla Tutorial (1)
  • MatLab Code (2)
  • My Videos (3)
  • MySQL Database (7)
  • Numerical Methods (9)
  • Offline Documentation (1)
  • Recent Topics (1)
  • Simulation and Modeling (2)
  • Unity Game Development (3)
  • Web Design (3)
  • Web Development (1)

Join Google+

Maniruzzaman Akash
View my complete profile

Join With Me

Site Visitors

Best Sites For a programmer

  • URI Online Judge Solution By Me
  • StackOverFlow
  • W3 School
  • Git Hub - Store your Every Document

Popular Posts

  • What are the characteristics of Digital IC's?-Solution
  • How to import Excel,CSV file in Laravel And insert data in database
  • Numerical Methods 20 Multiple Choice Questions and Answers
  • List and briefly define two approaches to dealing with multiple interrupts
  • What is Blue Whale Game ? Why people suicide?
  • The hypothetical machine has two I/O instructions: 0011= Load AC fro I/O 0111= Store AC to I/O Solutions
  • Mid Square Method Code implementation in C and MatLab
  • Depth First Search DFS code using Binary Tree in C language
  • Simpson's 1/3 Code in Matlab
  • Numerical method Codes simple MatLab implementation

Earn Money From Your site

Translate To your language

Categories

Advanced PHP (3) Android Developement (5) Articles (6) Artificial Intelligenece (3) Blogger Tips (5) Blogging Career (1) Bootstrap Offline Documentation (1) Bootstrap Templates (1) C Programming (14) Computer Architecture (5) Data Structure (11) Difference Between (1) Download Documentation (2) Download Ebook (3) Download Free Blog Template (2) Earning Money (1) Electrical Electronics (1) Guest Posts (1) HTML (4) Java Programming (2) Laravel (3) Laravel Bangla Tutorial (1) MatLab Code (2) My Videos (3) MySQL Database (7) Numerical Methods (9) Offline Documentation (1) Recent Topics (1) Simulation and Modeling (2) Unity Game Development (3) Web Design (3) Web Development (1)

Subscribe To This Site To Get Latest Article On Programming or web

Posts
Atom
Posts
Comments
Atom
Comments
© 2016 Maniruzzaman Akash's Blog | All rights reserved
Developed By Maniruzzaman Akash Created By Responsive Blogger Templates | Distributed By Gooyaabi Templates