java堆栈获取后缀表达式

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

package com.example.hanhan;

import java.util.ArrayList;

public class Stack {
	String[] code=new String[100];   //数组用来实现栈
	int top=0;                       //栈顶的表示下表
	String result="";              //结果后缀表达式
	/**
     * @param editText 获取到文本框的text后条用下面的
     * getArray方法获取到数字和操作符的数组进行循环遍历操作
     */
    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 参数为文本框的string
     * 调用getHuZhui()方法获取到后缀表达式,最后再计算结果
     */
    public String countString(String text)
    {
    	String resultString=getHuZhui(text);
    	top=0;
    	String[] result=resultString.split(",");
    	System.out.println("结果字符串数组长度:"+result.length);
    	for(int i=0;i<result.length;i++)
    	{
    		System.out.println("----"+result[i]);
    		if(isNum(result[i]))      
    		{
    			code[++top]=result[i];   //如果是数字就入栈
    		}
    		else if("+".equals(result[i]))  //加号处理
    		{
    			code[top-1]=String.valueOf(Double.valueOf(code[top-1])+Double.valueOf(code[top]));   //取出栈的第一个和第二个相加再赋给第二个
    			top=top-1;
    			System.out.println(code[top]);
    		}
    		else if("-".equals(result[i]))  //减号处理
    		{
    			code[top-1]=String.valueOf(Double.valueOf(code[top-1])-Double.valueOf(code[top]));
    			top=top-1;
    			System.out.println(code[top]);
    		}
    		else if("*".equals(result[i]))  //减号处理
    		{
    			code[top-1]=String.valueOf(Double.valueOf(code[top-1])*Double.valueOf(code[top]));
    			top=top-1;
    			System.out.println(code[top]);
    		}
    		else if("/".equals(result[i]))  //减号处理
    		{
    			code[top-1]=String.valueOf(Double.valueOf(code[top-1])/Double.valueOf(code[top]));
    			top=top-1;
    			System.out.println(code[top]);
    		}
    		
    	}
    	System.out.println("运算结果:"+code[top]);
    	return code[top];
    }
    
    /**
     * @param text 
     * @return  获取第一步需要操作的数组 
     * 比如(1.2+3)*5-4 转化为集合后为  (,1.2,+,3,),*,5,-,4
     * 主要还是将有小数点的数字保存在一个整体里面
     */
    public ArrayList<String> getArray(String text)
    {
    	String demo="";
    	System.out.println("数字函数:"+text); 
    	ArrayList<String> list=new ArrayList<String>();
    	for(int i=0;i<text.length();i++)
    	{
    		String s=text.substring(i,i+1);
    		System.out.println("截取:"+s);
    		//System.out.println(s+"?");
    	    if(isNum(s))
      		{
      			demo+=s;        //如果时数字就加到临时变量中
      		}
    	    else if(".".equals(s))  
    		{
    			demo+=s;        //如果时点号就加到临时变量中
    		}
    		else 
    		{
    			if(!"".equals(demo))
    			{
    				list.add(demo);  //当遍历到操作符时,查看临时变量中是否有值
    				list.add(s);     //添加操作符到集合中
    			}
    			else 
    			{
    				list.add(s);
    			}
    			demo="";         //使用后清空临时变量
    		}
    	}
    	if(demo!="")
    		list.add(demo);
    	System.out.println("循环结束");
    	for(int i=0;i<list.size();i++)
    	{
    		System.out.println(list.get(i)+">");
    	}
    	System.out.println("函数中的大小:"+list.size()); 
    	return list;
    }
    
    /**
     * @param str  
     * @return 判断是否时数字
     */
    public boolean isNum(String str) {
    	return str.matches("^(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值