欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java语言实现中缀表达式转换为后缀表达式

程序员文章站 2024-03-21 13:14:34
...
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Stack;
import java.util.regex.Pattern;

import org.springframework.util.StringUtils;

/**
 * @ClassName:     PostfixExpression.java
 * @Description:   中缀变后缀表达式
 * @author:        ma-zhuo
 * @version:       V1.0  
 * @Date:          2019年11月25日 下午3:37:53 
 */
public class PostfixExpression {

	private static String[] one = {"+","-"};
	private static String[] two = {"*","/"};
	private static ArrayList<Object> InfixList = new ArrayList<Object>();  //中缀表达式
	private static Stack<String> opStack = new Stack<String>();
	private static ArrayList<Object> suffixList = new ArrayList<Object>();  //后缀表达式
	
	/**
	 * @Description: 该方法实现将字符串中的每个数字和符号转化放进InfixList列表中
	 * @param @param expression 中缀字符串
	 * @throws
	 */
	private static void InffixList(String expression){
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i<expression.length(); i++) {
			String a = expression.substring(i,i+1);
			if(a.equals(".")){
				sb.append(a);
			}else if(isNum(a)){
				sb.append(a);
			}else{
				if(sb.length() != 0){
					InfixList.add(sb.toString());
					sb = new StringBuilder();
				}
				InfixList.add(a);
			}
			if(i == (expression.length() - 1)){
				InfixList.add(sb.toString());
			}
		}
	}
	
	/**
	 * @Title: isNum 
	 * @Description: 判断str是否是数字(只匹配正数,小数或者正数都可以)
	 * @return boolean    返回类型 
	 * @throws
	 */
	private static boolean isNum(String str){
		if(StringUtils.isEmpty(str)){
			return false;
		}
		Pattern pattern = Pattern.compile("^(0|[1-9][0-9]*)(\\.\\d+)?$");
		return pattern.matcher(str).matches();
	}
	
	/**
	 * @Title: priority 
	 * @Description: 判断两个运算符的优先级
	 * @return boolean a高于b,返回true;反之返回false 
	 * @throws
	 */
	private static boolean priority(String a,String b){
		if(StringUtils.isEmpty(a) || StringUtils.isEmpty(b)){
			return false;
		}
		int aa = 0,bb = 0;
		if(Arrays.asList(one).contains(a)){
			aa = 1;
		}else if(Arrays.asList(two).contains(a)){
			aa = 2;
		}else if(a.equals("(")){
			aa = 0;
		}
		if(Arrays.asList(one).contains(b)){
			bb = 1;
		}else if(Arrays.asList(two).contains(b)){
			bb = 2;
		}else if(b.equals("(")){
			bb = 0;
		}
		return (aa-bb) > 0;
	}
	
	//开始转换后缀表达式
	public static String getSuffixList(String expression){
		InffixList(expression);
		System.out.println("从字符串中提取出元素:"+InfixList.toString());
		
		for (Object object : InfixList) {
			if(isNum(object.toString())){
				suffixList.add(object);
			}else{
				//判断栈是否为空,如果为空则直接放入
				if(opStack.empty()){
					opStack.push(object.toString());
				}else if(object.toString().equals("(")){
					opStack.push(object.toString());
				}else if(object.toString().equals(")")){
					while(!opStack.peek().equals("(")){
						String a = opStack.pop();
						suffixList.add(a);
					}
					opStack.pop();
				}else{
					//如果不为空,则取出比较
					while(true){
						if(opStack.empty()){
							opStack.push(object.toString());
							break;
						}else if(priority(object.toString(), opStack.peek())){
							opStack.push(object.toString());
							break;
						}else{
							String a = opStack.pop();
							suffixList.add(a);
						}
					}
				}
			}
		}
		while(!opStack.empty()){
			suffixList.add(opStack.pop());
		}
		return suffixList.toString();
	}
}
import com.example.stack.postfixExpression.PostfixExpression;

/**
 * @ClassName:     TestStack.java
 * @Description:   TODO
 * @author:        mazhuo
 * @version:       V1.0  
 * @Date:          2019年11月26日 上午9:18:09 
 */
public class TestStack {

	/** 
	 * @Title: main 
	 * @Description: TODO
	 * @param @param args    设定文件 
	 * @return void    返回类型 
	 * @throws 
	 */
	public static void main(String[] args) {

		String str = "10.23+2*3+(4*5+6)*7";
		String suffix = PostfixExpression.getSuffixList(str);
		System.out.println("后缀表达式:"+suffix);
	}

}

输出结果

java语言实现中缀表达式转换为后缀表达式