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]+))?)$");
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章