JAVA利用栈实现四则运算

废话不多说直接上代码

在four_Operations方法中 a为符号栈,b为数字栈。

import java.util.*;

/**
 * 数字四则运算。
 */
public class stack {
    Integer operate(Integer a,Integer b,String c){
        if(c.equals("+"))
          return a+b;
        if(c.equals("-"))
          return b-a;
        if(c.equals("*"))
            return a*b;
        if (c.equals("/"))
            return b/a;
        return 0;
    }

    public void four_Operations(LinkedList<String> a,LinkedList<Integer> b,Map<String,Integer> map,String str){
         for(int i=0;i<str.length();i++){

              if (str.charAt(i)>='0'&&str.charAt(i)<='9'){       //将数字压入栈中去
                 int j=i;
                 int flag=0;
                 while (str.charAt(j)>='0'&&str.charAt(j)<='9'){
                     switch (flag){
                         case 0:
                             b.addLast((int)str.charAt(j)-48);
                             break;
                         case 1:
                             int temp=b.getLast();
                             b.removeLast();
                             b.addLast(((int)str.charAt(j)-48)+temp*10);
                             break;
                     }
                     flag=1;
                     j++;
                 }
                 i=j-1;
             }
             else {                       //如果为符号时
                 if (a.size()==0||str.charAt(i)=='(')   //符号栈为空时 或者为‘(’时  直接压入符号栈
                 a.add(String.valueOf(str.charAt(i)));
                 else {                   //如果符号栈不是空时
                     if (str.charAt(i)==')'){    //如果是“)”时就需要出栈
                             String c=a.getLast();
                             a.removeLast();    //操作符出栈
                             int num1=b.getLast();
                             b.removeLast();
                             int num2=b.getLast();
                             b.removeLast();
                             Integer result=operate(num1,num2,c);
                             b.add(result);   //数字进栈
                         if(!a.getLast().equals("(")){
                             String c1=a.getLast();
                             a.removeLast();    //操作符出栈
                             a.removeLast();   //“(”出栈
                             int num11=b.getLast();
                             b.removeLast();
                             int num21=b.getLast();
                             b.removeLast();
                             Integer result1=operate(num11,num21,c1);
                             b.add(result1);   //数字进栈
                         }
                         else
                             a.removeLast();//"C"出栈
                     }
                     else {
                         //如果为运算符时
                         if(map.get(String.valueOf(str.charAt(i)))<=map.get(a.getLast())){
                             while (map.get(String.valueOf(str.charAt(i)))<=map.get(a.getLast())){  //操作符一直出栈
                                 int num1=b.getLast();
                                 b.removeLast();
                                 int num2=b.getLast();
                                 b.removeLast();
                                 String c=a.getLast();
                                 a.removeLast();
                                 Integer result=operate(num1,num2,c);
                                 b.add(result);
                                 if(a.size()==0)
                                     break;
                             }
                             a.add(String.valueOf(str.charAt(i)));
                         }
                         else {                  //当前运算符的操作等级大于栈中的登记时
                             a.add(String.valueOf(str.charAt(i)));
                         }

                     }

                 }
             }
         }
         System.out.println(a);
         System.out.println(b);
     }


    public static void main(String[] args) {
        LinkedList<String> optList=new LinkedList<>();//符号栈
        LinkedList<Integer> numList=new LinkedList<>();//数字栈
        Map<String,Integer> map =new HashMap<>();  //定义几个符号的优先级。
        map.put("+",1);
        map.put("-",1);
        map.put("*",2);
        map.put("/",2);
        map.put("(",0);
        stack s=new stack();
        s.four_Operations(optList,numList,map,"((8-7)+(9*7-8*9-8-7-5-4))*((8-7)+(9*7-8*9-8-7-5-4))*((8-7)+(9*7-8*9-8-7-5-4))*((8-7)+(9*7-8*9-8-7-5-4))*((8-7)+(9*7-8*9-8-7-5-4))*((8-7)+(9*7-8*9-8-7-5-4))");
    }

}

这是最后的输出结果。
[* ]
[-33554432, -32]
符号栈里剩下最后的操作符号了
这时再进行一个出栈相加就可以得到最后的结果
如果不想再进行出栈操作,可以在多项式外面再加个(((8-7)+(97-89-8-7-5-4))((8-7)+(97-89-8-7-5-4))((8-7)+(97-89-8-7-5-4))((8-7)+(97-89-8-7-5-4))((8-7)+(97-89-8-7-5-4))((8-7)+(97-8*9-8-7-5-4));

这是加了()后的结果。
[ ]
[1073741824]

可能还有BUG没有发现,一些计算结果可能会出错。我测试了几个没有问题就发出来了,如果数据量大了样例多了,可能bug就出来了。

计算结果太大了也不行,不能大于2**15次方。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值