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的更多相关文章

  1. C++ Knowledge series Conversion & Constructor & Destructor

    Everything has its lifecycle, from being created to disappearing. Pass by reference instead of pass ...

  2. Linux+postfix+extmail+dovecot打造基于web页面的邮件系统

    原文地址:http://blog.csdn.net/deansrk/article/details/6717720 最终效果图: 准备阶段:需要手动下载的软件包: postfix-2.6.5.tar. ...

  3. Postfix 电子邮件系统精要

    来源: http://sery.blog.51cto.com/10037/45500 Postfix 电子邮件系统精要 作者:田逸(sery@163.com)  from [url]http://ww ...

  4. CentOS 系统中安装postfix+dovecot+openwebmail <转>

    一.先卸载sendmain[root@ser ~]#  yum remove sendmail 二.安装postfix ,dovecot,cyrus-sasl[root@ser ~]#  yum -y ...

  5. CentOS 6.4 x64 postfix + dovecot + 虚拟用户认证

    第一, 首先必须安装 apacache  mysql  php CentOS 直接使用 yum 安装 yum -y install httpd httpd-devel mysql php-mysql  ...

  6. 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 ...

  7. Linux下开源邮件系统Postfix+Extmail+Extman环境部署记录

    一.基础知识梳理MUA (Mail User Agent) MUA 既是"邮件使用者代理人",因为除非你可以直接利用类似 telnet 之类的软件登入邮件主机来主动发出信件,否则您 ...

  8. Postfix - Extmail 邮箱系统

    Postfix Dovecot Extmail 邮箱系统早前的内部邮箱系统重新整理下:现在Extmail官方有集成镜像的EMOS_1.6_x86_64免费版:可直接下载安装: 系统环境: linux ...

  9. postfix邮件服务器搭建02-安装篇

    本文接着上文的环境,进行postfix邮件发信端和dovecot邮件收信端的部署,之后部署基于浏览器的extmail图形管理端,使管理员可以通过网页对邮件虚拟用户进行管理,对邮件服务器进行管控 1.p ...

随机推荐

  1. Luogu P4398 [JSOI2008]Blue Mary的战役地图 矩阵哈希

    其实可以二分矩阵边长但是我太懒了$qwq$. 把每个子矩阵扔到$map$里,然后就没了 #include<cstdio> #include<map> #include<i ...

  2. shell 脚本拉取svn代码,vim中文乱码解决办法

    VIM安装成功后可以使用,但对中文是乱码,解决方法是在vim的配置文档中添加相关设置即可: 找到etc/vimrc 编辑~/.vimrc文件,加上如下几行: set fileencodings=utf ...

  3. 在一个非默认的位置包含头文件stdafx.h

    如果stdafx.h和你当前的工程不在一个文件夹下,当你在代码中第一行写下#include "stdafx.h"时,VC编译器会根据编译规则(相关的规则请查看MSDN)来区别std ...

  4. golang 文件导入数据追加sheet

    func ReadXlsx(c []CmdbTest, SheetName string) error {     //打开文件,如果文件不存在创建,存在就打开     path := ". ...

  5. golang的写文件测试

    package main import ( "os" "strings" "time" "fmt" "strc ...

  6. Netfilter 之 连接跟踪相关数据结构

    Netfilter通过连接跟踪来记录和跟踪连接的状态,为状态防火墙和NAT提供基础支持: 钩子点与钩子函数 下图为钩子点和钩子函数的关系图(点击图片查看原图),其中ipv4_conntrack_def ...

  7. 【互联网运营P1】

    一.导论 [运营]是什么 二.运营的职业分工和职能发展 三.转化型文案 4个高转化率短文案的常见姿势 2个短文案写作的核心要则 中长型转化文案的写作 针对所有问题点依次进行详细解读 四.第三方推广 常 ...

  8. RoP

    RoPS特征提取 RoPS为Rotational Projection Statistics的简写,即旋转投影统计特征.RoPS特征具有对点云旋转和平移(即姿态变化)的不变性,具备很强的鉴别力以及对噪 ...

  9. 前端知识点回顾——Javascript篇(六)

    fetch 在原生ajax+es6promise的基础上封装的一个语法糖,返回promise对象. fetch(url, initObj) .then(res=>res.json()) .the ...

  10. CNN基础框架简介

    卷积神经网络简介 卷积神经网络是多层感知机的变种,由生物学家休博尔和维瑟尔在早期关于猫视觉皮层的研究发展而来.视觉皮层的细胞存在一个复杂的构造,这些细胞对视觉输入空间的子区域非常敏感,我们称之为感受野 ...