zoukankan      html  css  js  c++  java
  • 回忆四则运算堆栈

    四则运算用到了堆栈的原理,计算机更喜欢先字符后运算符号的方法,也就是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);
          }
    }
  • 相关阅读:
    css做中划线与文字排版
    修复ios上第三方输入法弹出时输入键盘盖住网页没有进行相应滚动从而盖住表单输入框的问题
    一般活动页面之类简单的背景图内容布局方式
    compass的使用
    nodejs与sqlite
    ftp命令
    shell变量详解
    Vue CLI 3 使用百度地图
    centos7中安装python3
    redis集群安装问题/usr/share/rubygems/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- redis (LoadError)
  • 原文地址:https://www.cnblogs.com/chaogehahaha/p/15273416.html
Copyright © 2011-2022 走看看