数据结构 栈

用数组实现栈

package arrayStack;

public class ArrayStack {
    public int max;
    public int top = -1;
    public int[] s;
    public ArrayStack(int max){
        this.max=max;
        s = new int[this.max];
    }
    public boolean isFull(){
        return top == max-1;
    }
    public boolean isEmpty(){
        return top==-1;
    }
    public void push(int value){
        if(isFull()){
            System.out.println("已满,不可再加数据");
        }
        top++;
        s[top]=value;
    }
    public int pop(){
        if (isEmpty()){
            System.out.println("空了");
        }
        int value = s[top];
        top--;
        return value;
    }
    //取栈顶元素
    public int getTop(){
        return s[top];
    }

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

用栈实现计算单位数的计算器(只有加减乘除)

在上一个类里添加一些需要用到的方法

package arrayStack;

public class ArrayStack2 {
    public int max;
    public int top = -1;
    public int[] s;
    public ArrayStack2(int max){
        this.max=max;
        s = new int[this.max];
    }
    public boolean isFull(){
        return top == max-1;
    }
    public boolean isEmpty(){
        return top==-1;
    }
    public void push(int value){
        if(isFull()){
            System.out.println("已满,不可再加数据");
        }
        top++;
        s[top]=value;
    }
    public int pop(){
        if (isEmpty()){
            System.out.println("空了");
        }
        int value = s[top];
        top--;
        return value;
    }
    //取栈顶元素
    public int getTop(){
        return s[top];
    }

    public void list(){
        for(int i=top;i>=0;i--){
            System.out.println(s[i]);
        }
    }
    public int priority(int oper){
        if(oper == '*' || oper == '/'){
            return 1;
        }else if(oper == '+' || oper == '-'){
            return 0;
        }else{
            return -1;
        }
    }
    public int cal(int num1,int num2,int oper){
        int res = 0;
        switch (oper){
            case'+':
                res =  num1+num2;
                break;
            case '-':
                res = num2-num1;
                break;
            case'*':
                res = num1*num2;
                break;
            case '/':
                res = num2/num1;
                break;
        }
        return res;
    }
    public boolean isOper(int x){
        return x =='+' || x =='-' || x =='*' || x == '/';
    }
}

实现

package arrayStack;

public class Calculator {
    public static void main(String[] args) {
        String expression = "3+2*5-9";
        int index = 0;
        int num1 = 0;
        int num2 = 0;
        int oper = 0;
        int result = 0;
        ArrayStack2 numStack = new ArrayStack2(5);
        ArrayStack2 operStack = new ArrayStack2(5);
        char ch = ' ';//必须有空格
        while (true) {
            //substring() 方法返回字符串的子字符串,charAt(0)检索str中的第一个字符(为了把字符串转换成字符才用这个)
            ch = expression.substring(index,index+1).charAt(0);
            if(operStack.isOper(ch)){
                if(!operStack.isEmpty()){
                    if(operStack.priority(ch)<=operStack.priority(operStack.getTop())){
                        num1 = numStack.pop();
                        num2 = numStack.pop();
                        oper = operStack.pop();
                        result = numStack.cal(num1,num2,oper);
                        numStack.push(result);
                        operStack.push(ch);
                    }else{
                        operStack.push(ch);
                    }
                }else{
                    operStack.push(ch);
                }

            }else{
                numStack.push(ch-48);
            }
            index++;
            if(index >= expression.length()){
                break;
            }
        }
        while(true){
            if(operStack.isEmpty()){
                break;
            }
            num1 = numStack.pop();
            num2 = numStack.pop();
            oper = operStack.pop();
            result = numStack.cal(num1,num2,oper);
            numStack.push(result);
        }
        System.out.println(numStack.pop());

    }

}

多位数明天再写

package arrayStack;

public class Calculator {
    public static void main(String[] args) {
        String expression = "30+2*5-9";
        int index = 0;
        int num1 = 0;
        int num2 = 0;
        int oper = 0;
        int result = 0;
        String keepNum= "";// 校验数字是否为多位数时用的
        ArrayStack2 numStack = new ArrayStack2(5);
        ArrayStack2 operStack = new ArrayStack2(5);
        char ch = ' ';//必须有空格
        while (true) {
            //substring() 方法返回字符串的子字符串,charAt(0)检索str中的第一个字符(为了把字符串转换成字符才用这个)
            ch = expression.substring(index,index+1).charAt(0);
            if(operStack.isOper(ch)){
                if(!operStack.isEmpty()){
                    if(operStack.priority(ch)<=operStack.priority(operStack.getTop())){
                        num1 = numStack.pop();
                        num2 = numStack.pop();
                        oper = operStack.pop();
                        result = numStack.cal(num1,num2,oper);
                        numStack.push(result);
                        operStack.push(ch);
                    }else{
                        operStack.push(ch);
                    }
                }else{
                    operStack.push(ch);
                }

            }else{
                keepNum+=ch;
                if(index==expression.length()-1){
                // 如果当前已经是最后一个字符了,用不着校验他后面是数字还是运算符了直接push
                    numStack.push(Integer.parseInt(keepNum));
                }else{
                    if(operStack.isOper(expression.substring(index+1,index+2).charAt(0))){
                        // 如果下一个字符是运算符,结束拼接!直接push
                        numStack.push(Integer.parseInt(keepNum));
                        // 下一次还要用,别忘记清零
                        keepNum="";
                    }
                }

            }
            index++;
            if(index >= expression.length()){
                break;
            }
        }
        while(true){
            if(operStack.isEmpty()){
                break;
            }
            num1 = numStack.pop();
            num2 = numStack.pop();
            oper = operStack.pop();
            result = numStack.cal(num1,num2,oper);
            numStack.push(result);
        }
        System.out.println(numStack.pop());
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值