中缀转后缀(栈的应用三)

来源:互联网 发布:excel统计空单元格数据 编辑:程序博客网 时间:2024/06/06 03:56
public class TestStack {      private String testStr;      private Stack<Character> stack;            public TestStack(String testStr,Stack<Character> stack){      this.testStr = testStr;      this.stack = stack;      }       //中缀转后缀    (   )   +  -   *  /        public ArrayList<Character> zzzhz(){      ArrayList<Character> list = new ArrayList<Character>();       for(int i =0;i<testStr.length();i++){      Character c = testStr.charAt(i);      if(c=='+' || c=='-'){      if(stack.isEmpty() || stack.peek()=='('){      stack.push(c);      }else{      while(!stack.isEmpty() &&(stack.peek()=='*' || stack.peek()=='/' ||stack.peek()=='+' ||stack.peek()=='-' )){       list.add(stack.pop());      }      stack.push(c);      }      }else if(c=='*' || c=='/'){      if(stack.isEmpty() || stack.peek()=='('){      stack.push(c);      }else{      while(!stack.isEmpty() && (stack.peek()=='*' || stack.peek()=='/')){      list.add(stack.pop());       }      stack.push(c);      }      }else if(c=='('){      stack.push(c);      }else if(c==')'){      while(!stack.isEmpty() &&stack.peek()!='('){      list.add(stack.pop());      }      }else{      list.add(c);      }                 }      if(!stack.isEmpty()){  while(!stack.isEmpty()){  {  char c = stack.pop();  if(c!='('){  list.add(c);  }  }    }  }  return list;      }         }

0 0
原创粉丝点击