江的福从零开始冲算法(栈,计算器的实现)

在这里插入图片描述

一、使用数组模拟栈。

package ACStudy;

public class StackDemo {
    public static void main(String[] args) {
        Stack stack = new Stack(4);
        stack.push(9);
        stack.push(2);
        stack.push(3);
        stack.pop();
        stack.show();
    }
}
class Stack{
    private int maxSize;
    public int top =-1;
    public int[] arr;
    public Stack(int maxSize) {
        this.maxSize = maxSize;
        arr=new int[maxSize];
    }

    //判断是否为空

    public boolean isNull(){
        boolean flag=false;
        if (top==-1){
            flag=true;
            System.out.println("栈空");
        }
        return flag;

    }

    //判断栈满
    public boolean isFull(){
        boolean flag =false;
        if (top==maxSize-1){
            flag=true;
            System.out.println("栈满");
        }
        return flag;
    }

    public void push(int i){
        if (isFull()){
            throw new RuntimeException("栈满");
        }
        top++;
        arr[top]=i;
    }
    public int pop(){
        if (isNull()){
            throw new RuntimeException("栈空");
        }
        int temp = top;
        top--;
        return arr[temp];
    }
    public void show(){
        int temp =top;
        for (int i = 0; i <=top; i++) {
            System.out.println(arr[temp]);
            temp--;
        }
    }


}

前缀,中缀、后缀表达式

在这里插入图片描述


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Stack;

public class PolandNotation {
    public static void main(String[] args) {
        //定义逆波兰表达式
        String str ="3 4 + 5 * 6 - ";
        String[] s = str.split(" ");
        List<String> list =new ArrayList<>();
        list.addAll(Arrays.asList(s));
        int res = clculate(list);
        System.out.println("计算的结果是"+res);

    }

     public static int clculate(List<String> ls){
        //创建,只需要一个栈
         Stack<String> stack =new Stack<>();
         for (String l : ls) {
             if (l.matches("\\d+")){//匹配的是多位数
                 //入栈
                 stack.push(l);
             }else {
                 //pop出两个数,先运算,再入栈
                 int num2 =Integer.parseInt(stack.pop());
                 int num1= Integer.parseInt(stack.pop());
                 int res =0;
                 if (l.equals("+")){
                    res =num1+num2;
                 }else if (l.equals("-")){
                     res=num1-num2;
                 }else if (l.equals("*")){
                     res=num1*num2;
                 }else if (l.equals("/")){
                     res=num1/num2;
                 }else {
                     throw new RuntimeException("运算符异常");
                 }
                 //把res入栈
                 stack.push(""+res);
             }
         }
         //最后留在stack中的数据是运算结果
         return Integer.parseInt(stack.pop());

     }
}

中缀转后缀:

在这里插入图片描述
实现计算器功能:


import java.util.*;
import java.util.Stack;

public class PolandNotation {
    public static void main(String[] args) {
        //定义逆波兰表达式
        //String str ="3 4 + 5 * 6 - ";
        //String[] s = str.split(" ");
        //List<String> list =new ArrayList<>();
        //list.addAll(Arrays.asList(s));
        //int res = clculate(list);
        //System.out.println("计算的结果是"+res);


        Scanner scanner =new Scanner(System.in);
        String next = scanner.next();
        char[] chars = next.toCharArray();
        List<String> list1 =new ArrayList<>();
        for (int i = 0; i <chars.length ; i++) {
            StringBuilder s1 = new StringBuilder();
            boolean flag =false;
            while (i<chars.length){
                //判断是否为数字
                if (chars[i]<48||chars[i]>57){
                    if (flag){
                        i--;
                        break;
                    }else {
                        s1.append(chars[i]);
                        break;
                    }
                }else {
                    //数字考虑多位
                    s1.append(chars[i]);
                    flag=true;
                    i++;
                }
            }
            list1.add(s1.toString());
        }
        Iterator<String> iterator = list1.iterator();
        while (iterator.hasNext()) {
            String next1 = iterator.next();
            System.out.print(next1);
        }
        System.out.println();
        List<String> list = parseSuffixExpressionList(list1);
        Iterator<String> iterator1 = list.iterator();
        while (iterator1.hasNext()) {
            String next1 = iterator1.next();
            System.out.println(next1);
        }
        System.out.println();
        int clculate = clculate(list);
        System.out.println(clculate);

    }

    //中缀转后缀

    public static List<String> parseSuffixExpressionList(List<String> list){
            Stack<String> stack =new Stack<>();
            List<String> list1 =new ArrayList<>();
        for (String s : list) {
            //数字直接加到list1
            if (s.matches("\\d+")){
                list1.add(s);
            }else if (s.equals("(")){
                //左括号直接加栈
                stack.push(s);
            }else if (s.equals(")")){//右括号知道下一个括号出现出栈
                while (!stack.peek().equals("(")){
                    list1.add(stack.pop());
                }
                stack.pop();
            }else {
                //比较运算符的优先级
                if (stack.size()!=0) {
                    while (!stack.peek().equals("(")) {
                    	//
                        if (getValue(s)<= getValue(stack.peek())) {
                            list1.add(stack.pop());
                        }else {
                            break;
                        }
                        if (stack.size()==0){
                            break;
                        }
                    }
                }
                stack.push(s);
            }
        }
        while (stack.size()!=0){
            list1.add(stack.pop());
        }
        return list1;
    }


    public static int getValue(String str){
        int result=0;
        switch (str) {
            case "+":
                result=1;
                break;

            case "-":
                result=2;
                break;
            case "*":
                result=3;
                break;
            case "/":
                result=3;
        }
        return result;
    }


     public static int clculate(List<String> ls){
        //创建,只需要一个栈
         Stack<String> stack =new Stack<>();
         for (String l : ls) {
             if (l.matches("\\d+")){//匹配的是多位数
                 //入栈
                 stack.push(l);
             }else {
                 //pop出两个数,先运算,再入栈
                 int num2 =Integer.parseInt(stack.pop());
                 int num1= Integer.parseInt(stack.pop());
                 int res =0;
                 if (l.equals("+")){
                    res =num1+num2;
                 }else if (l.equals("-")){
                     res=num1-num2;
                 }else if (l.equals("*")){
                     res=num1*num2;
                 }else if (l.equals("/")){
                     res=num1/num2;
                 }else {
                     throw new RuntimeException("运算符异常");
                 }
                 //把res入栈
                 stack.push(""+res);
             }
         }
         //最后留在stack中的数据是运算结果
         return Integer.parseInt(stack.pop());

     }
}

优先级大于等于时就要pop and add

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值