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 ...
随机推荐
- SQL Update多表联合更新的方法
SQL Update多表联合更新的方法 (1) sqlite 多表更新方法 update t1 set col1=t2.col1 from table1 t1 inner join table2 t2 ...
- 小米oj 反向位整数(简单位运算)
反向位整数 序号:#30难度:一般时间限制:1000ms内存限制:10M 描述 输入32位无符号整数,输出它的反向位. 例,输入4626149(以二进制表示为00000000010001101001 ...
- 在Postman脚本中发送请求(pm.sendRequest)
Postman的Collection(集合)/Folder(集合的子文件夹)/Request(请求)都有Pre-request script和Tests两个脚本区域, 分别可以在发送请求前和请求后使用 ...
- 安装完Pycharm,启动时碰到"failed to load jvm dll"的解决方案
今天安装完系统,配置pycharm的环境的时候,启动pycharm时,碰到"failed to load jvm dll"的错误, 下面给出其解决方案: 安装Microsoft V ...
- 定时从linux获取信息放到windows上
环境:windows上代码路径下存放:WinSCP-5.13.8-Setup.exe 第一步:test.txt 拉取脚本的txt文本 解析:存放从linux路径下拉取所需源文件zyy_count. ...
- mysqldump 命令
[参考文章]:mysqldump命令详解 mysql数据库中备份工具,用于将MySQL服务器中的数据库以标准的sql语言的方式导出,并保存到文件中. 1. 参数介绍 默认为 true:表示默认情况下 ...
- 获取当前页面的webview ID
代码: A页面 <script type="text/javascript"> var ws = null; mui.plusReady(function(){ ws ...
- mac安装phpmysql
1.百度搜“phpmadmin”,还是一样,第二个因为是PC版本,不能用,点击第一个连接,去phpmyadmin的官网. 2.下载完毕后,进入到下载文件保存目录,双击压缩包,压缩包则会自动解压. 3. ...
- 使用Jsp/Js/Ajax/Json/Jquery/Easyui + Servlet + JDBC + Lucene/Mysql/Oracle完成数据库分页
package loaderman.action; import java.io.IOException; import java.io.PrintWriter; import java.util.L ...
- mvc导出数据到pdf
using iTextSharp;using iTextSharp.text;using iTextSharp.text.pdf; , , ") { System.Collections.A ...