使用链表栈实现多位计算器

构建链表节点: 


class CalStack {
    public CalStack(int no) {
        this.no = no;
    }

    @Override
    public String toString() {
        return "CalStack{" +
                "no=" + no +
                '}';
    }

    public CalStack getNext() {
        return next;
    }

    public void setNext(CalStack next) {
        this.next = next;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    private CalStack next;
    private int no;
}

构建链表:

class SingleLinkedStack {
    private CalStack head = new CalStack(0);
    private int maxsize;

    public SingleLinkedStack(int maxsize) {
        this.maxsize = maxsize;
    }

    //判断栈中总共有多少元素
    public int size() {
        CalStack temp = head.getNext();
        if (isEmpty()) {
            return 0;
        }
        int length = 1;
        while (temp.getNext() != null) {
            length++;
            temp = temp.getNext();
        }
        return length;
    }

    //判断是不是一个运算符
    public boolean isOper(char val) {
        return val == '+' || val == '-' || val == '*' || val == '/';
    }

    public int peek(){
        CalStack temp = head.getNext();
        while (temp.getNext()!=null){
            temp = temp.getNext();
        }
        return temp.getNo();
    }

    public int priority(int oper){
        if(oper == '+'|| oper == '-'){
            return 0;
        }else if(oper == '*'|| oper =='/'){
            return 1;
        }else{
            //假定目前只有运算符 + - * /
            return -1;
        }
    }

    public int cal(int num2,int num1,int oper){
        int res = 0 ;
        switch (oper){
            case '+':
                res = num2+num1;
                break;
            case '-':
                res = num1  -num2;
                break;
            case '*':
                res = num1 * num2;
                break;
            case '/':
                res = num1/num2;
                break;
            default:
                break;
        }
        return res;
    }

    //判断是否栈空
    public boolean isEmpty() {
        return head.getNext() == null;
    }

    //判断是否栈满
    public boolean isFull() {
        return maxsize == size();
    }

    //入栈操作
    public void push(CalStack newNode) {
        CalStack temp = head; //将临时指针指向头节点
        if (isFull()) {
            System.out.println("栈满,不能继续添加元素");
            return;
        }
        while (temp.getNext() != null) {
            temp = temp.getNext();
        }
//        System.out.printf("元素 %d 入栈\n", newNode.getNo());
        temp.setNext(newNode);
    }

    //出栈操作
    public int pop() {
        CalStack temp = head; //将临时指针指向头节点
        CalStack next = head.getNext(); //将临时指针指向第一个元素
//        System.out.println("元素出栈操作...");
        if (isEmpty()) {
            throw new RuntimeException("栈空,无可用元素出栈");
        }
        while (next.getNext() != null) {
            //将两个指针同步向后移动
            temp = temp.getNext();
            next = next.getNext();
        }
//        System.out.printf("元素 %d 出栈\n", next.getNo());
        temp.setNext(null);
        return next.getNo();
    }
}

Calculate类:

public class LinkedCalculator {
    public static void main(String[] args) {
        //完成表达式的运算
        String expression = "30+2*6-20";
        //创建两个栈,数栈,符号栈
        SingleLinkedStack numStack = new SingleLinkedStack(10);
        SingleLinkedStack operStack = new SingleLinkedStack(10);
//定义相关的变量
        int index = 0; //用于扫描
        int num1 = 0;
        int num2 = 0;
        String colNum = "";
        int oper = 0;
        int res = 0;
        char ch = ' '; //将每次扫描得到的char保存到ch
        //开始扫描expression
        while (true) {
            //依次得到 expression 的每一个字符
            ch = expression.substring(index, index + 1).charAt(0);
            //判断ch是数字还是字符
            if (operStack.isOper(ch)) {
                //判断当前的符号栈是否为空
                if (!operStack.isEmpty()) {
                    /* 如果符号栈有操作符,就进行比较,如果 当前的操作符的优先级 <= 栈中的操作符
                     * 就需要从数栈中pop出两个数字
                     * 再从符号栈中pop出一个符号,进行运算,将得到结果,入数栈,然后将当前的操作符入符号栈
                     */
                    if (operStack.priority(ch) <= operStack.priority(operStack.peek())) {
                        num1 = numStack.pop();
                        num2 = numStack.pop();
                        oper = operStack.pop();
                        res = numStack.cal(num1, num2, oper);
                        //运算后,把运算结果存入到数栈中
                        numStack.push(new CalStack(res));
                        //把当前得到的运算符存入到operStack中
                        operStack.push(new CalStack(ch));
                    } else {
                        //如果当前的操作符的优先级大于栈中的操作数,就直接入符号栈
                        operStack.push(new CalStack(ch));
                    }
                } else {
                    //如果为空直接入栈
                    operStack.push(new CalStack(ch));
                }
            } else { //如果是数,则直接入栈
                //如果ch以及是expression的最后一位,就直接入栈
                /**
                 * 双位计算器的实现过程
                 * 取数时,判断两次取到的数字都是数字时,将其结合起来,作为一个元素进行压栈操作
                 *
                 * 多位计算器的实现
                 * 取数时,直到遇到下一次取到的数字才会入栈,否则一直进行取数操作
                 */
                colNum += ch;
                //判断最后一个一定是数字,直接入栈
                if (expression.length() - 1 == index) {
                    numStack.push(new CalStack(Integer.parseInt(colNum)));
                } else {
                    //判断下一个字符是不是数字,如果是数字就直接入栈
                    if (operStack.isOper(expression.substring(index + 1, index + 2).charAt(0))) {
                        numStack.push(new CalStack(Integer.parseInt(colNum)));
                        //将临时合成的colNum清空,否则,会连下一次一起压入栈中
                        colNum = "";
                    }
                }
            }
            //让 index + 1,并判断是否扫描到expression的最后
            index++;
            if (index >= expression.length()) {
                break;
            }
        }
        //最后一个数栈中的元素就是所求的结果
        while (true) {
            //如果符号栈为空,则计算结束,此时数栈中只有一个元素
            if (operStack.isEmpty()) {
                break;
            }
            num1 = numStack.pop();
            num2 = numStack.pop();
            oper = operStack.pop();
            res = numStack.cal(num1, num2, oper);
            numStack.push(new CalStack(res));
        }
        System.out.printf(expression + " = %d\n", numStack.pop());
    }
}

运行结果:

相关文章:使用数组栈的实现多位计算器 

  • 7
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值