正则实现四则运算

public class CalcuStack
{

	static Stack<Character> stack = new Stack<Character>();
	static StringBuilder group = new StringBuilder();
	static NumberFormat NF = NumberFormat.getNumberInstance();
	static Pattern P_AS = Pattern.compile("\\+\\-");
	static Pattern P_RD = Pattern.compile("\\*\\/");
	static String REG_RD = "\\-?(\\d+\\.\\d+|\\d+)[*/]\\-?(\\d+\\.\\d+|\\d+)";
	static Pattern P_REG_RD = Pattern.compile(REG_RD);
	static String REG_AS = "\\-?(\\d+\\.\\d+|\\d+)(\\+\\-|\\-\\-|\\+|\\-)(\\d+\\.\\d+|\\d+)";
	static Pattern P_REG_AS = Pattern.compile(REG_AS);
	static boolean DEBUG = true;

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		NF.setMinimumFractionDigits(10);
		String s = "2*-2+2*(2*3.4)";
		System.out.println("表达式 --> " + s);
		s = s.contains("(") ? analyGroup(s) : analyMulDiv(s);
		System.out.println("最终结果 --> " + s);
	}

	private static String analyGroup(String s)
	{
		boolean put = false;
		int si = 0;
		int ei = 0;
		for (char c : s.toCharArray())
		{
			if (c == '(')
				put = false;
			if (c == ')')
				put = true;
			if (put)
			{
				int deepth = stack.size();
				int fi = 0;
				while (true)
				{
					char tc = stack.pop();
					if (tc == '(')
					{
						si = deepth - fi;
						break;
					}
					group.append(tc);
					fi++;
				}
				break;
			}
			stack.push(c);
			ei++;
		}
		group.delete(0, group.length());
		stack.clear();
		s = s.substring(0, si - 1) + analyMulDiv(s.substring(si, ei))
				+ s.substring(ei + 1, s.length());
		debug("计算后 --> " + s);
		return s.contains("(") ? analyGroup(s) : analyMulDiv(s);
	}

	private static String analyMulDiv(String arithmetic)
	{
		Matcher m = P_REG_RD.matcher(arithmetic);
		if (m.find())
		{
			String rd = m.group();
			String expr = calc(rd);
			debug("[乘除] --> " + rd + "=" + expr);
			return analyMulDiv(arithmetic.replaceFirst(REG_RD, expr));
		}
		return analyAddSub(arithmetic);
	}

	private static String analyAddSub(String arithmetic)
	{
		Matcher m = P_REG_AS.matcher(arithmetic);
		if (m.find())
		{
			String as = m.group();
			String expr = calc(as);
			debug("[加减] --> " + as + "=" + expr);
			return analyAddSub(arithmetic.replaceFirst(REG_AS, expr));
		}
		return arithmetic;
	}

	private static String calc(String expr)
	{
		String[] ss = null;
		char op = '/';
		if (expr.contains("*"))
		{
			ss = expr.split("\\*|/");
			op = '*';
		}
		else if (expr.contains("/"))
		{
			ss = expr.split("\\*|/");
			op = '/';
		}
		else if (expr.contains("+-") || expr.contains("+"))
		{
			ss = expr.split("\\+-");
			op = '-';
			if (ss.length == 1)
			{
				ss = expr.split("\\+");
				op = '+';
			}
		}
		else if (expr.contains("--"))
		{
			ss = expr.split("--");
			op = '+';
		}
		else if (expr.contains("-"))
		{
			ss = expr.split("-");
			op = '-';
		}
		Double d1 = Double.parseDouble(ss[0]);
		Double d2 = Double.parseDouble(ss[1]);
		Double r = 0d;
		switch (op)
		{
			case '*':
				r = d1 * d2;
				break;
			case '/':
				r = d1 / d2;
				break;
			case '+':
				r = d1 + d2;
				break;
			case '-':
				r = d1 - d2;
				break;
		}
		return NF.format(r);
	}

	static void debug(String msg)
	{
		if (DEBUG)
		{
			System.out.println(msg);
		}
	}
}


没仔细测^_^,不知道有没有BUG

你可能感兴趣的:(java)