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 ...
随机推荐
- Remove Mapping
http://social.msdn.microsoft.com/forums/vstudio/en-US/ffa3aa15-1d61-4464-ac4a-7a812d073a67/remove-ma ...
- Java进阶知识10 Hibernate一对多_多对一双向关联(Annotation+XML实现)
本文知识点(目录): 1.Annotation 注解版(只是测试建表) 2.XML版 的实现(只是测试建表) 3.附录(Annotation 注解版CRUD操作)[注解版有个问题:插入值时 ...
- node中的koa2
创建koa2工程 首先,我们创建一个目录hello-koa并作为工程目录用VS Code打开.然后,我们创建app.js,输入以下代码: // 导入koa,和koa 1.x不同,在koa2中,我们导入 ...
- 用python实现的简易记牌器的demo
实现功能很简单: 初始时 1到10 以及 jkq各 4张,大小王 共两张 只要输入相应的牌号:1到10,例如 >>1 J.K.Q :例如>>j >> ...
- Django-权限管理与路径导航
1.url权限管理 设计表 1.设计表 系统一共有多少个路径: 有哪些用户使用: 用户在公司的角色: 对角色进行权限分配(什么样的角色可以访问什么样的路径): 2.往表中添加数据,分配角色权限 3.登 ...
- antd-mobile的DatePicker分钟精度半小时
项目要求,在时间选择上需要精确到分钟,且分钟只能半小时,既0分钟或者是30分钟. 前期引用的时间控件是antd-mobile的DatePicker组件,具体用法可参考:https://mobile.a ...
- 20175329&20175313&20175318 2019-2020 《信息安全系统设计基础》实验三
20175329&20175313&20175318 2019-2020 <信息安全系统设计基础>实验三
- 2018-2019-2 20165330《网络对抗技术》Exp8 Web基础
目录 基础问题 相关知识 实验内容 实验步骤 实验总结与体会 实验内容 Web前端HTML 能正常安装.启停Apache.理解HTML,理解表单,理解GET与POST方法,编写一个含有表单的HTML ...
- 2018icpc 徐州h题
题目大意: https://codeforces.com/gym/102012/problem/H?csrf_token=c9d0191a64a241166d54a565b1615125 区间[l , ...
- linux 搜索文件夹下的所有文件内容
find . -type f -name "*.*" | xargs grep "博客园"