数据结构-栈

该博客介绍了一个用Java实现的计算器,它利用栈的数据结构处理带括号的四则运算表达式。通过两个栈,一个存储运算结果,另一个存储运算符,实现了从左到右的遍历并按运算优先级进行计算。示例代码展示了如何处理加减乘除以及括号的运算,最终得出正确的计算结果。
摘要由CSDN通过智能技术生成

定义

限制插入和删除只能在表的末端进行操作,也就是栈顶进行操作。符合先进后出的特点。Java中栈的实现是 通过数组。

例如:把1,2,3,4插入栈中,则存储结构如下:


栈的简单应用-计算器

代码如下:

import java.util.Stack;

/**
 * 实现带括号的四则运算
 */
public class CalculatorUtil {

    public static void main(String[] args) {
        System.out.println(CalculatorUtil.Calculator("1+2*3.0+(1+1*3)+5"));
    }

    public static double Calculator(String expression){
        char[] chars = expression.toCharArray();
        //存储计算结果
        Stack<Double> result=new Stack<>();
        //存储操作
        Stack<Character> oper=new Stack<>();
        //临时存储数值
        StringBuilder num=new StringBuilder();
        for(int i=0;i<chars.length;i++){
            if(chars[i]=='+' || chars[i]=='-'  || chars[i]=='*' || chars[i]=='/' ){
                //数值存入结果栈,并先进行乘除操作
                if(!oper.isEmpty() && (oper.peek()=='*' || oper.peek()=='/')){
                        dealSum(oper.pop(),result,result.pop(),Double.parseDouble(num.toString()));
                }else {
                    result.push(Double.parseDouble(num.toString()));
                }
                //清空临时数值
                num.setLength(0);
                oper.push(chars[i]);
            }else if(chars[i]=='('){
                //存储左括号
                oper.push(chars[i]);
            }else if(chars[i]==')'){
                char operLast;
                //一致进行出栈操作直到遇到左括号
                while ((operLast=oper.pop())!='('){
                    dealSum(operLast,result,result.pop(),result.pop());
                }
            }else {
                //拼接数值
                num.append(chars[i]);
            }
        }
        if(num.length()>0){
            result.push(Double.parseDouble(num.toString()));
        }
        //将剩余的操作出栈
        while (!oper.isEmpty()){
            dealSum(oper.pop(),result,result.pop(),result.pop());
        }
        return result.peek();
    }

    private static void dealSum(char oper,Stack<Double> result,double numFirst,double numSecond){
        switch (oper){
            case '+':
                result.push(numFirst+numSecond);
                break;
            case '-':
                result.push(numFirst-numSecond);
                break;
            case '*':
                result.push(numFirst*numSecond);
                break;
            case '/':
                result.push(numFirst/numSecond);
                break;
        }
    }

}

例如: 1+2.0*3+(1+1*3)+5

把输入的字符串转为字符数组,我们通过两个栈进行操作,result存储计算结果,operate存储操作。

第1轮操作

result: [1]

operate:[]

第2轮操作

result: [1]

operate:[+]

第3轮操作

result: [1,2.0]

operate:[+]

第4轮操作

result: [1,2.0]

operate:[+,*]

第5轮操作

result: [1,6.0]

operate:[+]

第6轮操作

result: [1,6.0]

operate:[+,+]

第7轮操作

result: [1,6.0]

operate:[+,+,(]

第8轮操作

result: [1,6.0,1]

operate:[+,+,(,+]

第9轮操作

result: [1,6.0,1,1]

operate:[+,+,(,+]

第10轮操作

result: [1,6.0,1,1]

operate:[+,+,(,+,*]

第11轮操作

result: [1,6.0,1,1,3]

operate:[+,+,(,+,*]

第12轮操作

result: [1,6.0,4]

operate:[+,+]

第13轮操作

result: [1,6.0,4]

operate:[+,+,+]

第14轮操作

result: [1,6.0,4,5]

operate:[+,+,+]

第15轮操作

result: [1,6.0,9]

operate:[+,+]

第16轮操作

result: [1,15.0]

operate:[+]

第17轮操作

result: [16.0]

operate:[]

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值