Postfix to Prefix Conversion & Prefix to Postfix Conversion
Postfix to Prefix Conversion
Postfix: An expression is called the postfix expression if the operator appears in the expression after the operands. Simply of the form (operand1 operand2 operator).
Example : AB+CD-* (Infix : (A+B) * (C-D) )
Prefix : An expression is called the prefix expression if the operator appears in the expression before the operands. Simply of the form (operator operand1 operand2).
Example : *+AB-CD (Infix : (A+B) * (C-D) )
Given a Postfix expression, convert it into a Prefix expression.
分析:
- Read the Postfix expression from left to right
- If the symbol is an operand, then push it onto the Stack
- If the symbol is an operator, then pop two operands from the Stack
- Create a string by concatenating the two operands and the operator before them. Like: string = operator + operand2 + operand1, and push the resultant string back to Stack
- Repeat the above steps until end of Prefix expression.
class Solution {
static boolean isOperator(char x) {
switch (x) {
case '+':
case '-':
case '/':
case '*':
return true;
default:
return false;
}
}
static String postToPre(String exp) {
Stack<String> s = new Stack<>();
int length = exp.length();
for (int i = ; i < length; i++) {
if (isOperator(exp.charAt(i))) {
String op1 = s.pop();
String op2 = s.pop();
String temp = exp.charAt(i) + op2 + op1;
s.push(temp);
}
else {
s.push(exp.charAt(i) + "");
}
}
return s.peek();
}
}
Prefix to Postfix Conversion
Prefix : An expression is called the prefix expression if the operator appears in the expression before the operands. Simply of the form (operator operand1 operand2).
Example : *+AB-CD (Infix : (A+B) * (C-D) )
Postfix: An expression is called the postfix expression if the operator appears in the expression after the operands. Simply of the form (operand1 operand2 operator).
Example : AB+CD-* (Infix : (A+B * (C-D) )
Given a Prefix expression, convert it into a Postfix expression.
分析:
- Read the Prefix expression in reverse order (from right to left)
- If the symbol is an operand, then push it onto the Stack
- If the symbol is an operator, then pop two operands from the Stack
- Create a string by concatenating the two operands and the operator after them.
- string = operand1 + operand2 + operator
- And push the resultant string back to Stack
- Repeat the above steps until end of Prefix expression.
class Solution {
boolean isOperator(char x) {
switch (x) {
case '+':
case '-':
case '/':
case '*':
return true;
default:
return false;
}
}
String preToPost(String pre_exp) {
Stack<String> s = new Stack<>();
int length = pre_exp.length();
for (int i = length - ; i >= ; i--) {
if (isOperator(pre_exp.charAt(i))) {
String op1 = s.pop();
String op2 = s.pop();
String temp = op1 + op2 + pre_exp.charAt(i);
s.push(temp);
} else {
s.push(pre_exp.charAt(i) + "");
}
}
return s.peek();
}
}
Postfix to Prefix Conversion & Prefix to Postfix Conversion的更多相关文章
- C++ Knowledge series Conversion & Constructor & Destructor
Everything has its lifecycle, from being created to disappearing. Pass by reference instead of pass ...
- Linux+postfix+extmail+dovecot打造基于web页面的邮件系统
原文地址:http://blog.csdn.net/deansrk/article/details/6717720 最终效果图: 准备阶段:需要手动下载的软件包: postfix-2.6.5.tar. ...
- Postfix 电子邮件系统精要
来源: http://sery.blog.51cto.com/10037/45500 Postfix 电子邮件系统精要 作者:田逸(sery@163.com) from [url]http://ww ...
- CentOS 系统中安装postfix+dovecot+openwebmail <转>
一.先卸载sendmain[root@ser ~]# yum remove sendmail 二.安装postfix ,dovecot,cyrus-sasl[root@ser ~]# yum -y ...
- CentOS 6.4 x64 postfix + dovecot + 虚拟用户认证
第一, 首先必须安装 apacache mysql php CentOS 直接使用 yum 安装 yum -y install httpd httpd-devel mysql php-mysql ...
- Centos7搭建邮件服务器-Postfix+Cyrus-sasl+Courier-authlib+Dovecot+ExtMail+Centos7
1.环境介绍 MTA: Postfix 3.1.4 SASL: Cyrus-sasl 2.1.26 ; Courier-authlib 0.66.1(Cyrus-sasl使用Courier-authl ...
- Linux下开源邮件系统Postfix+Extmail+Extman环境部署记录
一.基础知识梳理MUA (Mail User Agent) MUA 既是"邮件使用者代理人",因为除非你可以直接利用类似 telnet 之类的软件登入邮件主机来主动发出信件,否则您 ...
- Postfix - Extmail 邮箱系统
Postfix Dovecot Extmail 邮箱系统早前的内部邮箱系统重新整理下:现在Extmail官方有集成镜像的EMOS_1.6_x86_64免费版:可直接下载安装: 系统环境: linux ...
- postfix邮件服务器搭建02-安装篇
本文接着上文的环境,进行postfix邮件发信端和dovecot邮件收信端的部署,之后部署基于浏览器的extmail图形管理端,使管理员可以通过网页对邮件虚拟用户进行管理,对邮件服务器进行管控 1.p ...
随机推荐
- Mac中iterm2显示彩色
Mac中iterm2显示彩色 2016年08月23日 18:09:37 Sun7_She 阅读数:1974 参考网址:https://segmentfault.com/q/101000000065 ...
- C#中如何去掉字"/0"
string str = "you/0are/0sweet/0"; str = str.replace("/0","")); 备忘一下
- windos系统下使tomcat按天生成控制台日志catalina.out
windos系统下的tomcat默认不会记录控制台catalina.out日志,只有访问日志,不便于排错 修改启动文件 1.打开bin下面的startup.bat文件,把 call "%EX ...
- Linux命令(用户管理、组和时间管理)
用户管理 Linux系统是一个多用用户的系统 用户分为三类: 超级用户(root)用户的id是0 伪用户 用户的id是1----499,虽然存在,但不能被登录 ...
- Innodb内存结构
聚集索引与非聚集索引: 聚集索引:主键,有序,存储顺序与内存一致 非聚集索引:非主键,无序 聚集索引在叶子节点存储的是表中的数据 非聚集索引在叶子节点存储的是主键和索引列 使用非聚集索引查询出数据 ...
- golang——写文件和读文件
之前聊过,操作文件——读写文件,直接调用接口即可. 如果是一直写入操作,写入操作一直进行的,免不了会有,有时一大批数据过来,有时没有一条数据. 鉴于此场景,选择用select....channel 的 ...
- LeetCode 200. 岛屿的个数(Number of Islands)
题目描述 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. 示例 1 ...
- Python datetime库计算两个时间点之间的分钟(秒、天)数
计算两个时间点之间的分钟数 import datetime def minNums(startTime, endTime): '''计算两个时间点之间的分钟数''' # 处理格式,加上秒位 start ...
- centos系统python2.7更新到3.5
1. 下载Python-3.5.2 wget https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgz 2.安装 (报错no acceptabl ...
- VC++实现标准型计算器步骤及源码
VC++实现标准型计算器步骤及源码 2013年06月19日 09:48:47 无敌的成长日记 阅读数:4686 最近一段时间一直在做这个东西,刚刚拿到题目的时候认为这是一个简单的程序,可是 ...