java簡單實現中綴轉後綴

缺點:目前只實現10之內的四則運算java

學到的知識點:數組

StringBuffer若是想轉char數組,首先轉String,而後裝Charapp

String tem1=st.toString();

    char tem[]=tem1.toCharArray();

如何將字符轉數字Float要使用包裝類型ui

Float.valueOf(tem[i]-'0')code

import java.nio.CharBuffer;字符串

import java.util.Stack;get

public class ZhongZhui {io

static Stack<Character> stack = new Stack();ast

public static void main(String[] args) {

 String s="5+2*(3*(2-1))";
 
 StringBuilder out1=getLast(s);
 
   float result= calsum(out1);
   
    System.out.println(result);
	
}
public static float getSum(char op,float n1,float n2){

    if(op=='+')
	
        return n1+n2;
		
    if(op=='-')
	
        return n2-n1;
		
    if(op=='*')
	
        return n1*n2;
		
    if(op=='/')
	
        return n2/n1;
		
    return 0;
}

public static float calsum(StringBuilder st){

    Stack<Float> sum = new Stack();
	
    String tem1=st.toString();
	
    char tem[]=tem1.toCharArray();
	
    for(int i=0;i<tem.length;i++){
	
        if(tem[i]>='0'&&tem[i]<='9'){
		
            System.out.println("temp"+tem[i]);
			
            sum.push(Float.valueOf(tem[i]-'0'));
			
        }else {
		
            float t=getSum(tem[i],sum.pop(),sum.pop());
			
            System.out.println("t"+t);
			
            sum.push(t);
        }
    }
    
    return sum.pop();
}
public static StringBuilder getLast(String s){

   char st[]=s.toCharArray();
   
    StringBuilder out =new StringBuilder();
	
    for(int i=0;i<st.length;i++){
	
        char temp =st[i];
		
        if(temp==' ')
		
            continue;
			
        //若是是數字,添加到輸出字符串
		
         if(temp>='0'&&temp<='9'){
		 
             System.out.println("hell0");
			 
             out.append(temp);
			 
             continue;
         }
		 
         //若是碰到的是左括號,壓入棧中
		 
         if(temp=='('){
		 
             stack.push(temp);
			 
             System.out.println("hello(");
			 
             continue;
         }

         //若是符號是+或者-,當符號棧非空而且棧定元素不爲(,
		 
        //則將棧頂符號出棧,重複上述步驟,直到while循環不成立,退出,而後把符號壓入
		
        //理解也很簡單,由於+-的優先級最低,因此符號棧的符號只要不遇到
		
        //左括號就出棧

        if(temp=='+'||temp=='-'){
		
            while((!stack.empty())&&(stack.peek()!='(')){
			
                System.out.println("jia");
				
                out.append(stack.pop());
				
            }
			
            stack.push(temp);
			
            continue;
        }
		
        //如何遇到右括號,將左括號以後的元素都出棧輸出,將左括號出棧不輸出
		
       if(temp==')'){
	   
           while((!stack.empty())&&(stack.peek()!='(')){
		   
               System.out.println("you");
			   
               out.append(stack.pop());
			   
           }
		   
           stack.pop();
		   
           continue;
       }
	   
       //如遇到* /將符號棧的* /出棧並輸出
	   
       if(temp=='*'||temp=='/'){
	   
           while ((!stack.empty())&&(stack.peek()=='*'||stack.peek()=='/')){
		   
               System.out.println("cheng");
			   
               out.append(stack.pop());
           }
		   
           stack.push(temp);
		   
           continue;
		   
       }
    }
    //字符串遍歷完,再檢查符號棧,將其中的都輸出
	
    while (!(stack.empty())) {
	
        System.out.println("last");
		
        out.append(stack.pop());
		
    }
	
    return out;
}

}class

相關文章
相關標籤/搜索