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 ...
随机推荐
- codeforces1209E2 状压dp,枚举子集
题意 给一个n行m列的矩阵,每一列可以循环移位,问经过任意次移位后每一行的最大值总和最大为多少. 分析 每一行的最大值之和最大,可以理解为每一行任意选择一个数使它们的和最大. 设\(dp[i][S]\ ...
- python 获取数字在内存的内容
#coding=utf- from struct import pack,unpack byte=pack('f',1.5) print(byte) print([i for i in byte]) ...
- Flask-websocket实现聊天功能
群聊无昵称 原生js代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...
- [Linux]虚拟机无法安装deepin15.9的解决方案
虚拟机deepin15.9无法安装 sda assuming drive cache write through 显示内存不行,重启仍然无法安装 解决方案: 选择全盘安装方式 如果有全屏问题,需安装v ...
- Lua unpack函数用法
unpack,接受一个table做个参数,然后按照下标返回数组的所有元素 unpack lua 版本 <= 5.1 local t = {nil , 3} retunrn unpack(t) / ...
- TCO14 Wildcard CountTables——斯特林反演
不知道咕了多长时间的题... 讲了3遍,还是自己搞懂了.. 暂时没有找到题目链接 题意: n×m的网格,每个格子填[1,x]的数,使得不存在两行两列同构. 先保证一个,行相同. 再容斥掉列. 枚举至多 ...
- 预处理、const、static与sizeof-使用const与#define的特点及区别
1:#define只是用来做文本替换的.例如: #define PI 3.1415926 float angle; angle=*PI/; 那么,程序进行编译的时候,编译器会首先将“#define P ...
- nginx部署前端项目
1.在阿里云服务器上安装nginx,推荐使用yum安装 yum install -y nginx // 命令安装 nginx 服务器 2.配置nginx 安装完成后,进入 nginx 配置文件目录 一 ...
- 介绍 14 个 JavaScript 的框架和库
Javascript 得到了众多的技术领导者的拥护和支持,其中一位就是 WordPress 的作者 Matt Mullenweg , 他表示 WordPress 开发者 应该学习 JavaScript ...
- 使用create-react-app创建项目(二)——引入ant方法(一)
扩展项目(需要创建git默认文件) 具体步骤如下: a.git init b.git add . c.git commit -m "..." n ...