Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Stack;
- public class PostFix {
- public static Stack st;
- public static String postfix;
- public static void stackOrder( String s , int i , String postfix ) {
- if( st.empty() ||
- OperatorList.stronger( s.charAt( i ) , (char)st.firstElement() ) ) {
- return;
- }
- postfix += (char)st.pop();
- stackOrder( s , i , postfix );
- }
- public static void stackToPostFix() {
- while( !st.empty() ) {
- postfix += (char)st.pop();
- }
- }
- public static String convertInFixToPostFix( String s ) {
- st = new Stack();
- postfix = new String();
- for( int i = 0; i < s.length(); i++ ) {
- if( !OperatorList.isOperator( s.charAt( i ) ) ) {
- postfix += s.charAt( i );
- }
- if( OperatorList.isOperator( s.charAt( i ) ) ) {
- if( st.empty() ) {
- st.push( s.charAt( i ) );
- }
- if( OperatorList.stronger( s.charAt( i ) , (char)st.firstElement() ) ) {
- st.push( s.charAt( i ) );
- }
- else {
- stackOrder( s , i , postfix );
- }
- }
- }
- stackToPostFix();
- return postfix;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment