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 Expression | Prefix Expression | Postfix Expression |
---|---|---|
A + B | + A B | A B + |
A + B * C | + A * B C | A 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); } }
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 –
- Alphabet from A-Z or a-Z
- Numerical Digit from 0-9
- Operator
- Opening And Closing Braces ( , )
(2) If Entered Character is Alphabet then Following Action Should be taken-
- Print Alphabet as Output
(3) If Entered Character is Digit then Following Action Should be taken-
- Print Digit as Output
(4) If Entered Character is Opening Bracket then Following Action Should be taken-
- Push ‘(‘ Onto Stack
(5) If any Operator Appears before ‘)’ then
- Push it onto Stack.
- 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-
- Check Whether There is any Operator Already present in Stack or not.
- If Stack is Empty then Push Operator Onto Stack.
- If Present then Check Whether Priority of Incoming Operator is greater than Priority of Topmost Stack Operator.
- If Priority of Incoming Operator is Greater then Push Incoming Operator Onto Stack.
- 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.
- If character is alphabet or digit then print that as output.
- If get "(" then push in into stack and when get any ")" then search upto ")" and removing value from this to stack and remove the "("
- 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.
0 comments:
Post a Comment