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. nextUntil([exp|ele][,fil]) 查找当前元素之后所有的同辈元素,直到遇到匹配的那个元素为止。

    nextUntil([exp|ele][,fil]) 概述 查找当前元素之后所有的同辈元素,直到遇到匹配的那个元素为止. 如果提供的jQuery代表了一组DOM元素,.nextUntil()方法也能让 ...

  2. BIOS之于系统启动

    #BIOS之于操作系统 操作系统从开机通电到系统启动成功(执行main函数)分为3个步骤 启动BIOS,准备实模式下中断向量表和中断服务程序 从启动盘加载操作系统程序(包括boot镜像和root文件系 ...

  3. Codevs 1137 计算系数 2011年NOIP全国联赛提高组

    1137 计算系数 2011年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 给定一个多项式(ax + by ...

  4. 7.19T3

    小 X 的图 题目背景及题意 小 X 有一张图,有 n 个点(从 0 开始编号),一开始图里并没有 边,他有时候会向其中添加一条双向边(x 到 y).小 X 会时不时想知 道某两个点是否联通,如果连通 ...

  5. MySQL树

    树 树状图是一种数据结构,它是由n(n>=1)个有限结点组成一个具有层次关系的集合.把它叫做“树”是因为它看起来像一棵倒挂的树,也就是说它是根朝上,而叶朝下的. 它具有以下的特点:每个结点有零个 ...

  6. 什么是挂载?mount的用处在哪?

    关于挂载的作用一直不是很清楚,今天在阅读教材时看见了mount这个命令,发现它的用处很隐晦但非常强大.奈何教材说的不明朗,因此在网上整合了一些优秀的解释,看完之后豁然开朗. 1.提一句Windows下 ...

  7. POJ3764

    题目 POJ3764 The xor-longest Path 原题传送门 主要思路: 1.求出每个点到根节点(这里是树,所以直接取0)路径上所有权值xor和为d[i],则任意两点间路径xor和则为 ...

  8. Dubbo系列(一)dubbo的产生背景与原理概述

    一.Dubbo框架的产生背景        大规模服务化之前,应用只是通过RMI或Hessian等工具,简单的暴露和引用远程服务,通过配置服务的URL地址进行调用,通过F5等硬件进行负载均衡. (1) ...

  9. windows下安装MongoDB服务

    1,参考:https://www.cnblogs.com/lecaf/p/mongodb.html 2,要设置环境变量 3,设置用户 use admin  注:MongoDB安装好以后由默认的admi ...

  10. mysql 创建++删除 数据表

    创建表:CREATE TABLE IF NOT EXISTS `runoob_tbl`( `runoob_id` INT UNSIGNED AUTO_INCREMENT, `runoob_title` ...