中缀表达式转后缀表达式的一点想法

具体思路是按顺序读取字符串,然后
对读取的字符执行相关操作:
将角色带入​​堆栈,直到正确
括号,然后在右括号之后
采取,它将不会被推入堆栈
堆栈执行取消堆栈操作,遇到
存储在数字堆栈中的数字,并遇到
运算符存储在符号堆栈中,直到左侧
括号被取出,操作结束。
然后,将数据放入数字堆栈中的堆栈中
依次取出,放入最终堆栈,
然后将符号堆栈中的所有数据放出
堆栈并存储在此堆栈中。然后阅读下
字符串的字符,直到结尾。此时,
最终堆栈的数据顺序取出并存储
在应用程序堆栈中,然后将
遍历应用程序堆栈以获得我们需要的结果。
 
public class InToSuf {
    /*
     * Create five characters,one is to retrieve data,one is to retrieve number, one
     * is to retrieve character,one characters to retrieve the last data,the last is
     * to output easily
     */
    public static void main(String[] args) {
        String arr = "(((1+((2+3)*4))-5)+4)";
        StackTwo retrievedata = new StackTwo(2000);
        StackTwo number = new StackTwo(2000);
        StackTwo charcter = new StackTwo(2000);
        StackTwo lastdata = new StackTwo(2000);
        StackTwo output = new StackTwo(2000);
        // define several data
        int index = 0;
        // change to 'char',make the expression is easy
        char ch = ' ';
        while (true) {
            // get characters
            ch = arr.substring(index, index + 1).charAt(0);
            // judge the character and then perform related operators
            if (retrievedata.isRight(ch))
            {
                while (true) 
                {
                    char a = retrievedata.outStack();
                    if (retrievedata.isLeft(a)) break;
                    if (retrievedata.ifOper(a)) charcter.push(a);
                    else number.push(a);
                }
                /*
                 * The purpose of these two methods is to use the two stacks as relay stations,
                 * placing numbers and characters on the other stack in order
                 */
                while (true) 
                {
                    if (number.isEmpty()) break;
                    char b = number.outStack();
                    lastdata.push(b);
                }
                while (true) 
                {
                    if (charcter.isEmpty()) break;
                    char c = charcter.outStack();
                    lastdata.push(c);
                }
            } 
            else retrievedata.push(ch);
            index++;
            if (index >= arr.length()) break; // judging the end of cycle
        }
        while (true) // Output characters in the required order
        {
            if (lastdata.isEmpty())
                break;
            char d = lastdata.outStack();
            output.push(d);
        }
        output.showStack();
    }

}

class StackTwo {
    // Stack structure properties
    private int maxSize;
    private int top;
    private char[] array;

    // Initialize the stack structure
    public StackTwo(int maxsize) 
    {
        this.maxSize = maxsize;
        this.top = -1;
        array = new char[this.maxSize];
    }

    // Determine the stack is full
    public boolean isFull() 
    {
        return top == this.maxSize - 1;
    }

    // Determine the stack is empty
    public boolean isEmpty() 
    {
        return top == -1;
    }

    // Push
    public void push(char arr) 
    {
        if (isFull()) 
        {
            System.out.print("This stack is full!");
            return;
        }
        top++;
        array[top] = arr;
    }

    // Unstack
    public char outStack() 
    {
        if (isEmpty()) 
        {
            throw new RuntimeException();
        }
        char a = array[top];
        top--;
        return a;
    }

    public void showStack() 
    {
        if (isEmpty()) 
        {
            System.out.print("this stack is empty.");
            return;
        }
        while (true) 
        {
            System.out.print(array[top] + "\t");
            top--;
            if (top == -1)
                return;
        }
    }

    // The purpose of this method is to determine whether it is parentheses.
    public boolean isRight(char bracket) 
    {
        return bracket == ')';
    }

    public boolean isLeft(char bracket) 
    {
        return bracket == '(';
    }

    public boolean ifOper(char oper) 
    {
        return oper == '+' || oper == '-' || oper == '*' || oper == '/';
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值