• 回忆四则运算堆栈


    四则运算用到了堆栈的原理,计算机更喜欢先字符后运算符号的方法,也就是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);
          }
    }
  • 相关阅读:
    socket的几大异常以及其出现的原因
    将文件打包成apk
    putty失活不挂起运行
    天气免费接口
    内存泄漏
    The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build
    009 预处理 #pragma once; typedef; #ifdef
    008 区别 : NULL nullptr
    007 头文件 : 作用/组织/包含
    006 整数/浮点数/cpu频率
  • 原文地址:https://www.cnblogs.com/chaogehahaha/p/15273416.html
Copyright © 2020-2023  润新知