数据结构与算法Java——栈/栈实现第一版计算器

栈的介绍

1)栈的英文为(stack)
2)栈是一个先入后 出(FILO-First In Last Out)的有序列表。
3)栈(stack)是限制线性表 中元素的插入和删除只能在线性表的同-端进行的一种特殊线性表。 允许插入和删除的一端,为变化的一端,称为栈顶(Top),另一端为固定的一端,称为栈底(Bottom)。
4)根据栈的定义可知,最先放入栈中元素在栈底,最后放入的元素在栈顶,而删除元素刚好相反,最后放入的元素最先删除,最先放入的元素最后删除
5)图解方 式说明出栈( pop)和入栈(push)的概念。

java代码实现栈:

package;


public class ArrayStack {
    int maxSize;  //栈的最大容量
    int[] res;
    int top;   //代表栈顶元素下标,初始-1

    public ArrayStack(int maxSize) {
        this.maxSize = maxSize;
        res = new int[this.maxSize];
        top = -1;
    }

    public int peek(){
        if(isEmpty()){
            throw new RuntimeException("栈是空的!!");
        }
        return res[top];
    }

    public boolean isFull(){
        return top == maxSize - 1;
    }

    public boolean isEmpty(){
        return top == -1;
    }

    public void push(int value){
        if(isFull()){
            System.out.println("栈已经满了!!");
        }
        res[++top] = value;
    }

    public int pop(){
        if(isEmpty()){
            throw new RuntimeException("栈空不能从栈顶删除!");
        }
        int temp = res[top--];
        return temp;
    }

    public void list(){
        for(int i=top;i>=0;i--){
            System.out.println(res[i]);
        }
    }
}

class Test{
    public static void main(String[] args) {
        ArrayStack arrayStack = new ArrayStack(10);
        arrayStack.push(1);
        arrayStack.push(2);
        arrayStack.push(3);
        arrayStack.list();
        System.out.println(arrayStack.peek());
    }
}

栈实现第一版计算器

即对于 3 + 2 * 6 - 2 可以直接计算出结果。

思路:

  • 我们可以创建两个栈,一个作为数栈,一个作为符号栈,接着遍历表达式,如果是数字那么直接进数栈,如果是符号的话,判断符号栈是否为空,为空则直接入栈,不为空则判断栈顶符号和栈外符号的优先级,如果栈内符号优先级大,,则从数栈pop出两个数,从符号栈pop出一个符号,进行计算,将计算后的结果入数栈,接着将原先栈外的符号入符号栈即可;
  • 对表达式迭代完全后,我们计算剩下的结果,显然可知,当符号栈为空时则表达式迭代完全只剩下了一个结果。

Java实现代码:

package;

import java.util.Scanner;

public class Calculator {

    public static void main(String[] args) {
        ArrayStack numStack = new ArrayStack(10);
        ArrayStack operStack = new ArrayStack(10);

        Scanner scanner = new Scanner(System.in);
        String expression = scanner.next();
        int index = 0;
        int num1 = 0;
        int num2 = 0;
        int oper = 0;
        int res = 0;

        while(true){
            char temp =  expression.substring(index,index+1).charAt(0);
            if(isOper(temp)){
                if(operStack.isEmpty()){
                    operStack.push(temp);
                }
                else{
                    if(priority(temp) >= priority((char) operStack.peek())){
                        operStack.push(temp);
                    }
                    else{
                        num1 = numStack.pop();
                        num2 = numStack.pop();
                        oper = operStack.pop();
                        res = cal(num1,num2, (char)oper);
                        numStack.push(res);
                        operStack.push(temp);
                    }
                }
            }
            else{
                numStack.push(temp-'0');
            }

            index++;
            if(index>=expression.length()){
                break;
            }
        }

        while(true){
            if(operStack.isEmpty()){
                System.out.println(numStack.peek());
                break;
            }
            else{
                num1 = numStack.pop();
                num2 = numStack.pop();
                oper = operStack.pop();
                res = cal(num1,num2,(char) oper);
                numStack.push(res);
            }
        }

    }

    public static boolean isOper(char oper){
        if(oper == '+' || oper =='-' || oper=='*' || oper=='/'){
            return true;
        }

        return false;
    }

    public static int priority(char oper){
        if(oper=='*' || oper=='/'){
            return 2;
        }

        return 1;
    }

    public static int cal(int num1,int num2,char oper){
        switch (oper){
            case '+':
                return num2+num1;
            case '-':
                return num2-num1;
            case '*':
                return num2*num1;
            case '/':
                return num2/num1;
        }

        return -1;
    }
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

向光.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值