Infix to Postfix Expression
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的更多相关文章
- Infix to postfix conversion 中缀表达式转换为后缀表达式
Conversion Algorithm 1.操作符栈压入"#": 2.依次读入表达式的每个单词: 3.如果是操作数则压入操作数栈: 4.如果是操作符,则将操作符栈顶元素与要读入的 ...
- infix to postfix 完整版
#include<iostream> #include<stack> #include<string> #include<deque> using na ...
- Infix to postfix without '(' and ')'
#include<iostream> #include<stack> #include<string> #include<deque> using na ...
- Infix to postfix 用stack模板,表达式没有括号
#include<stack> #include<iostream> #include<string> using namespace std; //优先级判断 c ...
- Data Structure Stack: Infix to Postfix
http://geeksquiz.com/stack-set-2-infix-to-postfix/ #include <iostream> #include <vector> ...
- Postfix to Infix
Infix expression: The expression of the form a op b. When an operator is in-between every pair of op ...
- Postfix to Prefix Conversion & Prefix to Postfix Conversion
Postfix to Prefix Conversion Postfix: An expression is called the postfix expression if the operator ...
- sicily 中缀表达式转后缀表达式
题目描述 将中缀表达式(infix expression)转换为后缀表达式(postfix expression).假设中缀表达式中的操作数均以单个英文字母表示,且其中只包含左括号'(',右括号‘)’ ...
- 【JAVA】通过公式字符串表达式计算值,网上的一种方法
public class Test { public static void main(String[] args) { SimpleCalculator s=new SimpleCal ...
随机推荐
- java超大文件上传
上周遇到这样一个问题,客户上传高清视频(1G以上)的时候上传失败. 一开始以为是session过期或者文件大小受系统限制,导致的错误. 查看了系统的配置文件没有看到文件大小限制, web.xml中se ...
- learning strrchr func
函数名称: strrchr 函数原型:char *strrchr(const char *str, char c); 所属库: string.h 函数功能:查找一个字符c在另一个字符串str中末次出现 ...
- hdu 1133 卡特兰 高精度
Buy the Ticket Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- 秒的换算:ms(毫秒),μs(微秒),ns(纳秒),ps(皮秒)
皮秒 皮秒,符号ps(英语:picosecond ).1皮秒等于一万亿分之一秒(10-12秒) 1,000 皮秒 = 1纳秒 1,000,000 皮秒 = 1微秒 1,000,000,000 皮秒 = ...
- svn上误删除覆盖的文件简单恢复步骤
因为失误,不小心把同事提交到svn的文件给覆盖删除了.早上来和我说,吓死宝宝了... 还好,svn是个好东西,恢复到某个版本就行了.下面是简单步骤: 1.在本地的svn项目文件,右键---Toitoi ...
- windows下安装MongoDB服务
1,参考:https://www.cnblogs.com/lecaf/p/mongodb.html 2,要设置环境变量 3,设置用户 use admin 注:MongoDB安装好以后由默认的admi ...
- Python之Javascript
1.JavaScript 被设计用来向 HTML 页面添加交互行为. 2.JavaScript 是一种脚本语言(脚本语言是一种轻量级的编程语言). 3.JavaScript 由数行可执行计算机代码组成 ...
- LeetCode 240. 搜索二维矩阵 II(Search a 2D Matrix II)
题目描述 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. 示例: 现有矩阵 m ...
- UML建模综述
一.概念 UML-Unified Model Language 统一建模语言,又称标准建模语言.是用来对软件密集系统进行可视化建模的一种语言.作为一个支持模型化和软件系统开发的图形化语言,UML为软件 ...
- linux如何绑定域名和ip?
答: 通过在/etc/hosts中添加条目,如下: <ip_address> <domain_name> 如: 11.11.11.11 www.baidu.com