• 回忆四则运算堆栈


    四则运算用到了堆栈的原理,计算机更喜欢先字符后运算符号的方法,也就是2+3变成23+

    运用堆栈可以很好的实现这种方法

    import java.util.ArrayList;
    import java.util.Stack;
    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));
                                       
                                         }
                                         else {
                                            stack1.push(a2);
                                        
                                         }
                                    }                                        
                               }
                             
    }
                         index++;
                         if(index-1==stack1.size()){
    
                                break;
    
                            }
    }
                         while(true) {
                             if(stack1.isEmpty()){
                                    //当我们的符号栈为空的时候则计算到最后的结果,数栈中只有一个结果那就是我们的结果
                                    break;
                                }
                             num1 = Double.parseDouble(stack2.pop());
    
                             num2 = Double.parseDouble(stack2.pop());
                                
                             stack2.push(""+getResult(stack1.pop(),num2,num1));
                         }
                         return Integer.parseInt(stack2.peek());
    }
          public static void main(String[] args) {
              Stack2 stack = new Stack2();
              String a = "36+4*3+2/2";
              double b = stack.getAnswer(stack.getArraylist(a));
              System.out.print(b);
          }
    }
  • 相关阅读:
    Fragment生命周期详解
    VS常用快捷键
    转载:C#中的泛型
    Vue 引入代码代码编辑器monacoeditor并自定义语法提示
    Monaco Editor 参数配置详解
    关于uniapp生成证书数字签名(.keystore)文件
    C#上传到FTP Server
    Linux下静态编译glib
    简单谈谈haskell
    hi,大家
  • 原文地址:https://www.cnblogs.com/chaogehahaha/p/15273416.html
Copyright © 2020-2023  润新知