从上一次到今天的总结1

在这么长的一段时间中,完成很多事情

第一件事是完成了耗费时间最长的栈运算,要求是把一个字符串算式将数字和算号分开分别放到链表里面,然后通过栈实现后缀表达式

虽然整个代码拆分的还不够细致,但是能够完成四则运算。下面附上源代码

package GOGOGO;
import java.util.*;
public class Stack2 {
   public static double getResult(String n,double num1,double num2)
     {   
                 double result = 0;
                 switch (n) {
                 case "+":
                     result = num1+num2;
                     break;
                 case "-":
                     result = num1-num2;
                     break;
                 case "*":
                     result = num1*num2;
                     break;
                 case "/":
                     result = num1/num2;
                     break;
                 default:
                     break;
                 }
                 return result;
         }
   public int priority(String oper){
         if(oper.contentEquals("*")||oper.contentEquals("/")){
             return 1;
         }else if (oper.contentEquals("+")||oper.contentEquals("-")) {
             return 0;
         } else {
             return -1; // 假定目前的表达式只有 +, - , * , /
         }
     } 
   public ArrayList<String> getArraylist(String n){
       String str = "";
       int j=0;
       ArrayList<String> list = new ArrayList<String>();
       for(char a:n.toCharArray()) {
     if(Character.isDigit(a)) {
                 str += a;
     }
     else {
      if(str.contentEquals("")) {
       list.add(a+"");
      }
      else {
       list.add(str);
       list.add(a+"");
      }
      str = "";
      j= n.lastIndexOf(a);
    } 
    
    }
    list.add(n.substring(j+1));
   
    return list;
   }
   public double getAnswer(ArrayList<String> list) {
      Stack<String> stack1 = new Stack<String>();
      Stack<String> stack2 = new Stack<String>();
       double num1;
    double num2;
    int index = 0;
         while(true) {
     String a2 = list.get(index);;
        char ch[] = a2.toCharArray();
      if(Character.isDigit(ch[0])) {
       stack2.push(a2);
      
      }
      else {
       if(stack1.isEmpty()) {
        stack1.push(a2);
       
       }
       else {
        if(a2.contentEquals("(")){
                           stack1.push(a2);
                       
                       }
        else if(a2.contentEquals(")")){            
                               num1 = Double.parseDouble(stack2.pop());
                               num2 = Double.parseDouble(stack2.pop());
                              
                               stack2.push(""+getResult(stack1.pop(),num2,num1));
                         
                               stack1.pop();
       }
                   else {
                         if(priority(a2)<=priority(stack1.peek())) {
                          num1 = Double.parseDouble(stack2.pop());
                             num2 = Double.parseDouble(stack2.pop());
                               
                             stack2.push(""+getResult(stack1.pop(),num2,num1));
                             stack1.push(a2);
                         }
                         else {
                         stack1.push(a2);
                        
                         }
                       }          
                        }
      
}
      index++;
      if(index==list.size()){
                   break;
               }
   System.out.println(stack1);
   System.out.print(stack2);
}
      
         while(true) {
       
          if(stack1.isEmpty()){
                    //当我们的符号栈为空的时候则计算到最后的结果,数栈中只有一个结果那就是我们的结果
                    break;
                }
          num1 = Double.parseDouble(stack2.pop());
                         num2 = Double.parseDouble(stack2.pop());
                           
                         stack2.push(""+getResult(stack1.pop(),num2,num1));
                       
         }
         return Double.parseDouble((stack2.peek()));
}
   public int getRandom(int n){                                   //get Random
       Random a = new Random();
       int b = a.nextInt(n);
       return b;
     }
   public ArrayList<String> makeArraylist(ArrayList<String> list,int m){
       int f;
     
     f= getRandom(m);
     if(f== 0) {
      list.add(f,"(");
      list.add(f+4,")");
     }
     else if(f==2){
      list.add(f,"(");
      list.add(f+4,")");
     }
     else if(f==4) {
      list.add(f,"(");
      list.add(f+4,")");
     }
     else if(f==6) {
      list.add(f,"(");
      list.add(")");
     }
    return list;
   }
   public static void main(String[] args) {
    Stack2 stack = new Stack2();
    String a = "36+4*3+4/2";
    ArrayList li = new ArrayList();
    li = stack.makeArraylist(stack.getArraylist(a), 6);
    System.out.println(li);
    double b = stack.getAnswer(li);
    System.out.print(b);
   }
}
原文地址:https://www.cnblogs.com/chaogehahaha/p/13893696.html