Java data structure Day11 -- stack implementation inverse Polish expression (suffix expression) calculator

Keywords: Java data structure

Today, I happened to talk with some students. Let's share it
SimpleDateFormat has thread safety problems. After Java 8, thread safe LocalDate, LocalTime, or LocalDateTime can be used, but to be honest, this LocalDate is much more troublesome than SimpleDateFormat.. but you can write a tool class to encapsulate common methods. So it doesn't seem so difficult

 

Stop bragging, stop bragging, get down to business

catalogue

Prefix expression

Infix expression

Postfix Expression

Overall code (stack implementation suffix expression calculator)

Prefix expression

"(3 + 4) * 5-6" is represented by a prefix expression“  - * + 3 4 5 6  "
Calculate prefix expression: scan the prefix expression from right to left, push it into the stack when encountering a number, pop out the top and secondary top elements from the stack when encountering a symbol, calculate with the symbol (the top element of the stack is num1, and the secondary top element is num2), and then push the result value into the stack; this cycle until only one number is left, that is, the result

Infix expression

"(3 + 4) * 5-6" this type is infix expression. Although we can clearly analyze the calculation logic, for the computer, we also need to judge the priority. Therefore, during computer calculation, infix expression will be converted into other types of expressions

Postfix Expression

"(3 + 4) * 5-6" if expressed by suffix expression, it is "3 4 + 5 * 6 -"
Calculate suffix expression: scan the suffix expression from left to right, push it into the stack when encountering a number, pop out the top and secondary top elements from the stack when encountering a symbol, calculate with the symbol (the top element of the stack is num2, and the secondary top element is num1), and then push the result value into the stack; this cycle until only one number is left, that is, the result  

What are num1 and num2? What's the difference between another name? -- >   " Num1 operator num2 "this should be understood. In fact, the prefix expression puts the top element of the stack in front (as the subtracted and divisor), and the suffix expression puts the top element of the stack behind


A child will ask again: so, how can we convert the infix expression we wrote into a suffix expression?
This thing is troublesome and involves a lot of steps. I'll write it tomorrow!

Overall code (stack implementation suffix expression calculator)

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class PolandNotation {
    public static void main(String[] args) {
        System.out.println("Please enter a suffix expression");
        Scanner scanner = new Scanner(System.in);
        String question = scanner.next();
        List<String> listString = getListString(question);
        System.out.println(findResult(listString));
    }

    //Write a format that changes strings into individual strings
    public static List<String> getListString(String value) {
        ArrayList<String> arr = new ArrayList<>();
        for (int i = 0; i < value.length(); i++) {
            String single = value.substring(i, i + 1);
            arr.add(single);
        }
        return arr;
    }

    //Determine whether it is an operator
    public static boolean isOper(String val){
        return val.equals("*") || val.equals("/") || val.equals("+") || val.equals("-");
    }
    //Evaluates the list of incoming strings
    public static int findResult(List<String> list){
        int num1,num2,tmp;
        ArrayStack arrayStack = new ArrayStack(list.size());
        for (String s : list) {
            if (isOper(s)){
                //Yes operator, pop up number calculation
                num2 = arrayStack.pop();
                num1 = arrayStack.pop();
                tmp = calculate(num1, num2, s);
                arrayStack.push(tmp);
            }else{
                //It's a number. Put it on the stack
                arrayStack.push(Integer.parseInt(s));
            }
        }
        return arrayStack.pop();
    }

    //Computational logic
    public static int calculate(int num1,int num2, String s){
        switch(s) {
            case "+":
                return num1 + num2;
            case "-":
                return num1 - num2;
            case "*":
                return num1 * num2;
            case "/":
                return num1 / num2;
        }
        //s can only be the above four types. return 0 here is to avoid reporting errors
        return 0;
    }
}
class ArrayStack{
    int maxSize;
    int[] array;
    //The stack top pointer points to - 1 by default. When there is data push, the top + + points to bit 0
    int top = -1;
    public ArrayStack(int maxSize) {
        this.maxSize = maxSize;
        array = new int[maxSize];
    }
    public void push(int num){
        if (top == maxSize - 1){
            System.out.println("Stack full,Cannot add");
            return;
        }
        array[++top] = num;
    }
    public int pop(){
        if (top == -1){
            System.out.println("Stack empty,Unable to stack");
            return -9999999;
        }
        return array[top--];
    }
    public void popall(){
        if (top == -1){
            System.out.println("Stack empty,Unable to stack");
            return;
        }
        int i = 1;
        while (top != -1){
            System.out.println("The first" + i++ + "Gewei" + array[top--]);
        }
    }
    public void showall(){
        if (top == -1){
            System.out.println("Stack empty,Unable to stack");
            return;
        }
        for (int i = 0; i <= top; i++) {
            System.out.println("The first" + i + "Gewei" +array[i]);
        }
    }
}

Posted by NTGr on Sun, 05 Dec 2021 09:10:20 -0800