Example :

Infix : (A+B) * (C-D) )

Postfix: AB+CD-*

算法:

1. Scan the infix expression from left to right.

2. If the scanned character is an operand, append it to result.

3. Else

  3.1 If the precedence of the scanned operator is greater than the precedence of the operator in the stack(or the stack is empty or the stack contains a ‘(‘ ), push it.

  3.2 Else, Pop all the operators from the stack which are greater than or equal to in precedence than that of the scanned operator. After doing that Push the scanned operator to the stack. (If you encounter parenthesis while popping then stop there and push the scanned operator in the stack.)

4. If the scanned character is an ‘(‘, push it to the stack.

5. If the scanned character is an ‘)’, pop the stack and and output it until a ‘(‘ is encountered, and discard both the parenthesis.

6. Repeat steps 2-6 until infix expression is scanned.

7. Append the remaing items int the stack.

public class InfoxToPostfix {
private int order(char ch) {
switch (ch) {
case '+':
case '-':
return ;
case '*':
case '/':
return ;
case '^':
return ;
default:
return -;
}
} private String infixToPostfix(String exp) {
StringBuilder result = new StringBuilder();
Stack<Character> stack = new Stack<>(); for (int i = ; i < exp.length(); ++i) {
char c = exp.charAt(i);
if (Character.isLetterOrDigit(c)) {
result.append(c);
} else if (c == '(') {
stack.push(c);
} else if (c == ')') {
while (!stack.isEmpty() && stack.peek() != '(') {
result.append(stack.pop());
}
stack.pop();
} else {
while (!stack.isEmpty() && order(c) <= order(stack.peek())) {
result.append(stack.pop());
}
stack.push(c);
}
}
while (!stack.isEmpty()) {
result.append(stack.pop());
}
return result.toString();
}
}

Infix to Postfix Expression的更多相关文章

  1. Infix to postfix conversion 中缀表达式转换为后缀表达式

    Conversion Algorithm 1.操作符栈压入"#": 2.依次读入表达式的每个单词: 3.如果是操作数则压入操作数栈: 4.如果是操作符,则将操作符栈顶元素与要读入的 ...

  2. infix to postfix 完整版

    #include<iostream> #include<stack> #include<string> #include<deque> using na ...

  3. Infix to postfix without '(' and ')'

    #include<iostream> #include<stack> #include<string> #include<deque> using na ...

  4. Infix to postfix 用stack模板,表达式没有括号

    #include<stack> #include<iostream> #include<string> using namespace std; //优先级判断 c ...

  5. Data Structure Stack: Infix to Postfix

    http://geeksquiz.com/stack-set-2-infix-to-postfix/ #include <iostream> #include <vector> ...

  6. Postfix to Infix

    Infix expression: The expression of the form a op b. When an operator is in-between every pair of op ...

  7. Postfix to Prefix Conversion & Prefix to Postfix Conversion

    Postfix to Prefix Conversion Postfix: An expression is called the postfix expression if the operator ...

  8. sicily 中缀表达式转后缀表达式

    题目描述 将中缀表达式(infix expression)转换为后缀表达式(postfix expression).假设中缀表达式中的操作数均以单个英文字母表示,且其中只包含左括号'(',右括号‘)’ ...

  9. 【JAVA】通过公式字符串表达式计算值,网上的一种方法

    public class Test {    public static void main(String[] args) {     SimpleCalculator s=new SimpleCal ...

随机推荐

  1. 【转】pe结构详解

    (一)基本概念 PE(Portable Execute)文件是Windows下可执行文件的总称,常见的有DLL,EXE,OCX,SYS等, 事实上,一个文件是否是PE文件与其扩展名无关,PE文件可以是 ...

  2. conda 激活环境失败解决办法

    https://stackoverflow.com/questions/41746137/conda-environment-is-discoverable-but-not-activateable- ...

  3. bash脚本中使用选项 getopts

    原文链接 : http://note.youdao.com/noteshare?id=0cf08484c7308c763726e63e9a638ff5&sub=EF6A110E2F3345E6 ...

  4. 安装docker以及常规操作

    一.安装 docker对内核版本是有要求的,反正建议用7以上的版本,少坑 如果需要卸载旧版本(凡是卸载删除操作都要谨慎!): yum remove docker \ docker-client \ d ...

  5. SQL Labs刷题补坑记录(less54-less65)

    LESS54: 只有10次尝试,dump处secret key 直接union 查就可以,括号为单引号闭合 LESS55: 尝试出来闭合的方式为)括号,后面操作与54相同 LESS56: 尝试出来括号 ...

  6. LC 638. Shopping Offers

    In LeetCode Store, there are some kinds of items to sell. Each item has a price. However, there are ...

  7. LC 641. Design Circular Deque

    Design your implementation of the circular double-ended queue (deque). Your implementation should su ...

  8. css滚动条美化

    /*定义滚动条高宽及背景 高宽分别对应横竖滚动条的尺寸*/ ::-webkit-scrollbar { width: 5px; height: 5px; background-color: #F5F5 ...

  9. Python的并行求和例子

    先上一个例子,这段代码是为了评估一个预测模型写的,详细评价说明在 https://www.kaggle.com/c/how-much-did-it-rain/details/evaluation, 它 ...

  10. python之scrapy爬取某集团招聘信息以及招聘详情

    1.定义爬取的字段items.py # -*- coding: utf-8 -*- # Define here the models for your scraped items # # See do ...