逆波兰表达式特性:
后缀表达式——1,遇数字进栈;
2,遇符号取出栈顶前两位进行运算,再进栈;
3,最后求得的值出栈输出;
中缀表达式转后缀表达式——1,优先级较低的进栈遇到优先级较高情形,分别与栈中元素从栈顶比较,若优先级小 于等于栈顶元素,则栈顶元素出栈,再接着与下一个栈顶元素比对,直到比栈顶元 素小为止;
2,数字不进栈输出;
3,括号配对后要将夹在中间的值输出;
实例:
<span style="font-size:24px;">package 逆波兰式;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import 逆波兰式.StrategePattern.contextFactory;
public class NBLprocess {
public static void main(String[] args) {
String str = "9 3 1 - 3 * + 10 2 / +";
System.out.println(NBLcompute(str));
String str1 = "9 + ( 3 - 1 ) * 3 + 10 / 2";
System.out.println(NBLconvert(str1));
}
/**
* 逆波兰式中缀转后缀表达式
* @param str1
*/
private static String NBLconvert(String str1) {
String[] arr = str1.split("\\s+");
List<String> lst = new ArrayList<String>();
Pattern p = Pattern.compile("[0-9]|[0-9]{2}");
StringBuilder sb = new StringBuilder();
Stack stk = new Stack();
for(String s : arr){
Matcher m = p.matcher(s);
if(m.matches()){
sb.append(s + " ");
}else{
if(s.equals(")")){
String temp = null;
while(!(temp = stk.pop().toString()).equals("(")){
sb.append(temp + " ");
}
}else if((s.equals("+") || s.equals("-")) && !stk.isEmpty()){
String temp = stk.pop().toString();
if(temp.equals("*") || temp.equals("/")){
sb.append(temp + " ");
while(!stk.isEmpty()){
sb.append(stk.pop().toString() + " ");
}
stk.push(s);
}else{
stk.push(temp);
stk.push(s);
}
}else{
stk.push(s);
}
}
}
while(!stk.isEmpty()){
sb.append(stk.pop().toString() + " ");
}
return sb.toString();
}
/**
* 逆波兰后缀表达式计算
* @param str
* @return
*/
private static String NBLcompute(String str) {
String[] arr = str.split("\\s+");
Pattern p = Pattern.compile("[0-9]|[0-9]{2}");
StrategePattern sp = new StrategePattern();
Stack stk = new Stack();
for(String s : arr){
Matcher m = p.matcher(s);
if(m.matches()){ //匹配数字的入栈
stk.push(s);
}else{ //不匹配数字的前两值出栈计算
String a = stk.pop().toString();
String b = stk.pop().toString();
int a1 = Integer.parseInt(a);
int b1 = Integer.parseInt(b);
int max = a1 > b1 ? a1 : b1;
int min = a1 < b1 ? a1 : b1;
contextFactory cf = new contextFactory(s);
stk.push(cf.getResult(max, min));
}
}
return stk.pop().toString();
}
}
</span>
四则表达式的策略模式:
package 逆波兰式;
public class StrategePattern {
/**
* 算法抽象父类
* @author Administrator
*
* @description
*
* 20142014年9月5日上午10:38:03
*
*/
public static interface algorithm{
public int getResult(int a,int b);
}
/**
* 四则运算类
* @author Administrator
*
* @description
*
* 20142014年9月5日上午10:38:19
*
*/
public static class add implements algorithm{
@Override
public int getResult(int a,int b) {
return a+b;
}
}
public static class dec implements algorithm{
@Override
public int getResult(int a, int b) {
return a-b;
}
}
public static class multi implements algorithm{
@Override
public int getResult(int a, int b) {
return a*b;
}
}
public static class div implements algorithm{
@Override
public int getResult(int a, int b) {
return a/b;
}
}
/**
* 工厂类
* @author Administrator
*
* @description
*
* 20142014年9月5日上午10:39:08
*
*/
public static class contextFactory{
algorithm alg;
public contextFactory(String type) {
switch (type) {
case "+":
alg = new add();
break;
case "-":
alg = new dec();
break;
case "*":
alg = new multi();
break;
case "/":
alg = new div();
break;
default:
break;
}
}
public int getResult(int a,int b){
return alg.getResult(a, b);
}
}
//测试
public static void main(String[] args) {
contextFactory context = new contextFactory("+");
System.out.println(context.getResult(10, 1));
}
}
输出结果:
20
9 3 1 - 3 * + 10 2 / +