使用后缀表达式计算表达式

需求:输入字符串表达式,计算表达式结果

解决:将输入的表示转化为后缀表达式形式(也称作逆波兰式),再计算结果。

package com.phh;

import java.util.Stack;

public class CalculatedString {

	/**
	 * 根据中缀表达式计算结果
	 * 
	 * @param expression
	 * @return
	 */
	public Float calculate(String expression) {
		Float result = null;

		Stack stack = new Stack();

		char[] exp = expression.toCharArray();
		StringBuffer s = new StringBuffer();
		for (char c : exp) {

			if (c == 32) {
				if (!s.toString().equals("")) {
					stack.push(Float.valueOf(s.toString()));
					s = new StringBuffer();
				}
			}

			if (c >= 48 && c <= 57) {
				s.append(c);
			}

			if (c == 42 || c == 43 || c == 45 || c == 47) {
				if (!s.toString().equals("")) {
					stack.push(Float.valueOf(s.toString()));
					s = new StringBuffer();
				}
			}

			if (c == 42) {
				float b = (Float) stack.pop();
				float a = (Float) stack.pop();
				stack.push(a * b);
			}

			if (c == 43) {
				float b = (Float) stack.pop();
				float a = (Float) stack.pop();
				stack.push(a + b);
			}

			if (c == 45) {
				float b = (Float) stack.pop();
				float a = (Float) stack.pop();
				stack.push(a - b);
			}

			if (c == 47) {
				float b = (Float) stack.pop();
				float a = (Float) stack.pop();
				stack.push(a / b);
			}
			
		}

		result = (Float) stack.pop();

		return result;
	}

	public static void main(String[] args) {
		MiddleToLast m = new MiddleToLast();
		CalculatedString cal = new CalculatedString();
		String expression = m.transfer("15+18-2*10+(9-6+2)/2+18");
		System.out.println(cal.calculate(expression));
	}
}

package com.phh;

import java.util.Stack;

public class MiddleToLast {

	/**
	 * 将输入的中缀表达式转化为后缀表达式
	 * 
	 * @param 中缀表达式
	 * @return 后缀表达式
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public String transfer(String middle) {
		StringBuffer last = new StringBuffer();

		Stack stack = new Stack();

		int level = 0;

		char middleChar[] = middle.toCharArray();
		for (char c : middleChar) {
			if (c >= 48 && c <= 57) {
				last.append(c);
			}
			if (c == 40) {
				stack.push(c);
			}
			if (c == 43 || c == 45) {
				if (level > 1) {
					while (!stack.isEmpty()
							&& 40 != (Character) stack.firstElement()) {
						char c1 = (Character) stack.pop();
						if (c1 != 40) {
							last.append(c1);
						}
					}
				}
				stack.push(c);
				last.append(" ");
				level = 1;
			}
			if (c == 42 || c == 47) {
				stack.push(c);
				level = 2;
				last.append(" ");
			}
			if (c == 41) {
				while (!stack.isEmpty()) {
					char c1 = (Character) stack.pop();
					if (c1 != 40) {
						last.append(c1);
					} else {
						break;
					}
				}
			}
		}

		while (!stack.isEmpty()) {
			char c1 = (Character) stack.firstElement();
			if (c1 != 40) {
				last.append(stack.pop());
			}
		}

		return last.toString();
	}

	public static void main(String[] args) {
		MiddleToLast m = new MiddleToLast();
		String middle = "9+(3-1)*3+10/2";
		System.out.println(m.transfer(middle));
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值