【java堆栈获取后缀表达式】

类中存在好多System.out,是调试作用的,可能看起来会很多余,自己可以删掉,代码看起来就干净多了。java中本身也有内置好了的堆栈,也可以看下

  [java]

  package com.example.hanhan;

  import java.util.ArrayList;

  public class Stack {

  String[] code=new String[100];   //数组用来实现栈

  int top=0;                       //栈顶的表示下表

  String result="";              //结果后缀表达式

  /**

  * @param editText <span style="color:#ff0000;"><strong>获取到文本框的text后条用下面的

  * getArray方法获取到数字和操作符的数组进行循环遍历操作

  </strong></span>     */

  public String getHuZhui(String editText)

  {

  System.out.println("进入方法");

  if(editText!=""&&editText.length()>0)

  {

  System.out.println(editText);

  //将string转为数组

  ArrayList<String> str=getArray(editText);

  System.out.println("进入操作");

  System.out.println("调用的大小:"+str.size());

  //传来的字符串长度不为零才有效,遍历所有的操作符,放入堆栈中

  for(int i=0;i<str.size();i++)

  {

  System.out.println("操作符号:"+str.get(i));

  System.out.println("进入循环");

  if(str.get(i)。equals("("))       //碰到左括号就入栈

  {

  code[++top]="(";

  System.out.println("栈值:"+code[top]);

  System.out.println("左括号");

  }

  else if(str.get(i)。equals("*"))  //碰到乘号,如果栈顶是除号则推出除号

  {

  while("/".equals(code[top]))

  {

  result+=code[top]+",";      //操作符出栈

  top--;

  }

  code[++top]="*";    //乘号入栈

  System.out.println("栈值:"+code[top]);

  System.out.println("乘号");

  }

  else if(str.get(i)。equals("/"))  //碰到乘号,如果栈顶是除号就出栈

  {

  while("*".equals(code[top]))

  {

  result+=code[top]+",";      //操作符出栈

  top--;

  }

  code[++top]="/";    //除号入栈

  System.out.println("栈值:"+code[top]);

  System.out.println("除号");

  }

  else if(str.get(i)。equals("+"))

  {

  while(("*".equals(code[top]))||("/")。equals(code[top])||("-")。equals(code[top]))

  {

  result+=code[top]+",";      //操作符出栈

  top--;

  }

  code[++top]="+";    //加号入栈

  System.out.println("栈值:"+code[top]);

  System.out.println("加号处理后:"+result);

  }

  else if(str.get(i)。equals("-"))

  {

  while(("*".equals(code[top]))||("/")。equals(code[top])||("+")。equals(code[top]))

  {

  result+=code[top]+",";     //操作符出栈

  top--;

  }

  code[++top]="-";   //减号入栈

  System.out.println("栈值:"+code[top]);

  System.out.println("减号打印结果:"+result);

  }

  else if(str.get(i)。equals(")"))

  {

  System.out.println(top);

  System.out.println("---------");

  System.out.println(code[top]);

  System.out.println("---------");

  //碰到右括号时就出栈直到退到左括号

  while(!"(".equals(code[top]))

  {

  result+=code[top]+",";

  top--;

  }

  top--;  //去掉左括号的栈顶值

  System.out.println("栈值:"+code[top]);

  System.out.println("右括号打印结果:"+result);

  }

  else

  {

  //时数值的话就直接保存到字符串中

  result+=str.get(i)+",";

  System.out.println("数字");

  }

  //System.out.println(result);

  }

  //字符串循环遍历结束后,如果堆栈的值不为空则输出结果

  System.out.println(top);

  while(top>=1)   //栈值是从1开始的 所以遍历是到一就可以结束了

  {

  System.out.println("最后循环的结果:"+code[top]);

  result+=code[top]+",";

  top--;

  }

  System.out.println("操作最后结果:"+result);

  return result;

  }

  else

  {

  return "";

  }

  }

  /**

  * @param text

  * @return<span style="color:#ff0000;"><strong> 参数为文本框的string。更多精彩教程请关注:ghost xp系统下载之家

posted on 2013-08-02 14:31  潇洒kman  阅读(301)  评论(0编辑  收藏  举报