中缀表达式转后缀表达式 求后缀表达式值

中缀表达式转后缀表达式

队列Q和栈S
Q存放后缀表达式结果
S存放操作符
设栈顶元素top
当前读取元素a

算法

如果S是空栈
a入栈
非空栈
a不是操作符,a插入队列Q;
如果a为(,a入栈;
否则,如果a为),把栈内操作符出栈依次插入队列Q,直至遇到得(元素出栈为止;
否则,如果a优先级大于top,a入栈;
如果top为(,a入栈;
否则,如果a<=top,top出栈,插入队列,a入栈。
读取完毕,如果栈不为空,则最后将栈内所有元素出栈依次插入队列Q。

后缀表达式求值

从左到右第一个操作符a,是a左边两个操作数做的a运算。
a+b-c的后缀表达式为ab+c-。第一个操作符+,左边两个数是a和b,因此a+b。设a+b的结果为d,dc-,因此d-c之差为最后结果。
用栈解决后缀表达式求值。

package com.simple.postfix;

/**
 * @ClassName Queue
 * @Author  gggirl
 * @Date 2020/8/25
 * @Description TODO
 * @Version 1.0
 */
public class Queue {
    private char[] array;
    int size;
    int head;
    int tail;
    int items;
    public Queue(int size){
        this.size = size;
        this.array = new char[this.size];
        this.tail = -1;
        this.head = 0;
        this.items = 0;
    }
    public void insert(char ch){
        if(this.tail == this.size - 1){
            this.tail = -1;
        }
        this.array[++tail] = ch;
        this.items++;
    }
    public char remove(){
        char ch = this.array[this.head++];
        this.items--;
        if(this.head == this.size){
            this.head = 0;
        }
        return  ch;
    }
    public boolean isEmpty(){
        return this.items == 0;
    }
    public boolean isFull(){
        return this.items == this.size;
    }
}

package com.simple.postfix;

/**
 * @ClassName PostAppMain
 * @Author gggirl
 * @Date 2020/8/25
 * @Description TODO
 * @Version 1.0
 */
public class SuffixArithmeticExpression {
    public static void main(String[] args) {
        String ex = "1*((2-1+2)*(3/1))";
        // a+b-c ab+c-
        // a*(b-c)  abc-*
        // d+a*(b-c) dabc-*+
        // d+a*(b-c*e) dabce*-*+
        // a+b*(c*(d-e)) abcde-**+
        // 1*((2-1+2)*(3/1))

        System.out.println(ex);
        String suffix = getSuffix(ex);
        System.out.println(suffix);
        int res = getSuffixValue(suffix);
        System.out.println(res);
        // 输出结果
        // 1*((2-1+2)*(3/1))
        // 121-2+31/**
       // 9
    }

    public static int getSuffixValue(String suffixExpress){

        Stack suffixStack = new Stack(suffixExpress.length());
        int res = 0;
        for(int i = 0; i < suffixExpress.length(); i++){
            char ch = suffixExpress.charAt(i);
            parse(ch, suffixStack);
        }
        if(!suffixStack.isEmpty()){
            char top = suffixStack.pop();
            res = Character.getNumericValue(top);
        }
        return res;
    }

    public static void parse(char ch, Stack stack){
        int res = 0;
        if(stack.isEmpty()){
            stack.push(ch);
        }else {

            char ch2 ;
            char ch1 ;
            int n1 = 0;
            int n2 = 0;
            String resS="";
            char chTop ;
            switch (ch){
                case '-':
                    ch2 = stack.pop();
                    ch1 = stack.pop();
                    n2 = Character.getNumericValue(ch2);
                    n1 = Character.getNumericValue(ch1);
                    res = n1 - n2; // char ascii字符串编码  res超过10,没有10的编码的
                    resS = String.valueOf(res);
                    chTop = resS.charAt(0);
                    stack.push(chTop);
                    break;
                case '+':
                    ch2 = stack.pop();
                    ch1 = stack.pop();
                    n2 = Character.getNumericValue(ch2);
                    n1 = Character.getNumericValue(ch1);
                    res = n1 + n2; // char ascii字符串编码  res超过10,没有10的编码的
                    resS = String.valueOf(res);
                    chTop = resS.charAt(0);
                    stack.push(chTop);
                    break;
                case '/':
                    ch2 = stack.pop();
                    ch1 = stack.pop();
                    n2 = Character.getNumericValue(ch2);
                    n1 = Character.getNumericValue(ch1);
                    res = n1 / n2; // char ascii字符串编码  res超过10,没有10的编码的
                    resS = String.valueOf(res);
                    chTop = resS.charAt(0);
                    stack.push(chTop);
                    break;
                case '*':
                    ch2 = stack.pop();
                    ch1 = stack.pop();
                    n2 = Character.getNumericValue(ch2);
                    n1 = Character.getNumericValue(ch1);
                    res = n1 * n2; // char ascii字符串编码  res超过10,没有10的编码的
                    resS = String.valueOf(res);
                    chTop = resS.charAt(0);
                    stack.push(chTop);
                    break;
                default:
                    stack.push(ch);
                    break;
            }
        }

    }


    public static String getSuffix(String ex){
        Stack operators = new Stack(ex.length());
        Queue operands = new Queue(ex.length());
        for(int i = 0; i < ex.length(); i++){
            char ch = ex.charAt(i);
            switch (ch){
                case '-':
                case '+':
                    translate(ch, 0, operators, operands);
                    break;
                case '*':
                case '/':
                    translate(ch, 1, operators, operands);
                    break;
                case '(':
                case ')':
                    translate(ch, 2, operators, operands);
                    break;
                default:
                    // 操作数入栈
                    operands.insert(ch);
                    break;
            }
        }
        while (!operators.isEmpty()){
            char ch = operators.pop();
            operands.insert(ch);
        }
        String res = "";
        while (!operands.isEmpty()){
            char ch = operands.remove();
            res = res+ch;
        }
//        System.out.println(res);
        return res;

    }

    public static void translate(char currCh, int priorityCurr, Stack operators, Queue operands){
        if(operators.isEmpty()){
            operators.push(currCh);
        }
        else{
            char top = operators.peek();// +-*/
            int priorityTop = 0;//+-默认0
            switch (top){
                case '*':
                case '/':
                    priorityTop = 1;
                    break;
                case '(':
                case ')':
                    priorityTop = 2;
                    break;
                default:
                    break;
            }
            if(currCh == '('){
                operators.push(currCh);
            }else {
                if(currCh == ')'){
                    while (!operators.isEmpty()){
                        char ch = operators.pop();
                        if(ch!='('){
                            operands.insert(ch);
                        }
                        if(ch == '('){
                            break;
                        }
                    }
                }else {
                    if(priorityCurr > priorityTop){
                        operators.push(currCh);
                    }else {
                        if(top == '('){
                            operators.push(currCh);
                        }else{
                            if(priorityCurr <= priorityTop){
                                char ch = operators.pop();
                                operands.insert(ch);
                                operators.push(currCh);//小于先pop出现再进栈
                            }
                        }
                    }
                }
            }

            }
        }

}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值