HDU 3624: Digital Calculator

链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=11708

题意:就是表达式求值。。。

题解:一种非常优雅的办法解决这个问题。。具体看代码啊把。。。

代码君:

import java.util.ArrayList;
import java.util.Scanner;

public class Main {

	Scanner scan = new Scanner(System.in);
	ArrayList<Unit> units = new ArrayList<Unit>();
	String source, strValue;
	int current = 0;

	void init() throws Exception {
		source = scan.nextLine();
		current = -1;
		units.clear();
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < source.length(); i++) {
			if (source.charAt(i) == ' ')
				continue;
			sb.append(source.charAt(i));
		}
		source = sb.toString();
		grammer();
		nextToken();
	}

	boolean isSymbel(char ch) throws Exception {
		if (ch == '(' || ch == ')' || ch == '%' || ch == '^')
			return true;
		if (ch == '+' || ch == '-' || ch == '*' || ch == '/')
			return true;
		return false;
	}

	void grammer() throws Exception {
		for (int i = 0; i < source.length();) {
			char ch = source.charAt(i);
			if (isSymbel(ch)) {
				units.add(new Unit(new String(new char[] { ch })));
				i++;
				continue;
			} else if (Character.isDigit(ch)) {
				StringBuilder sb = new StringBuilder();
				while (i < source.length()) {
					if (Character.isDigit(source.charAt(i)) == false)
						break;
					sb.append(source.charAt(i));
					i++;
				}
				Unit u = new Unit(sb.toString());
				u.isNumber = true;
				units.add(u);
			} else {
				throw new Exception();
			}
		}
	}

	void match(String c) throws Exception {
		if (!c.equals(strValue))
			throw new Exception();
	}

	void inRange(long a) throws Exception {
		if (a > Integer.MAX_VALUE || a < Integer.MIN_VALUE)
			throw new Exception();
	}

	void nextToken() throws Exception {
		if (current == units.size())
			throw new Exception();
		current++;
		if (current != units.size())
			strValue = units.get(current).str;
		else
			strValue = null;
	}

	long number() throws Exception {
		if (units.get(current).isNumber) {
			long result = (long) Integer.valueOf(strValue);
			nextToken();
			return result;
		}
		throw new Exception();
	}

	long add(long a, long b) throws Exception {
		long result = a + b;
		inRange(result);
		return result;
	}

	long minus(long a, long b) throws Exception {
		long result = a - b;
		inRange(result);
		return result;
	}

	long multiply(long a, long b) throws Exception {
		long result = a * b;
		inRange(result);
		return result;
	}

	long divide(long a, long b) throws Exception {
		if (b == 0)
			throw new Exception();
		long result = a / b;
		inRange(result);
		return result;
	}

	long mod(long a, long b) throws Exception {
		if (b == 0)
			throw new Exception();
		long result = a % b;
		inRange(result);
		return result;
	}

	long exp(long a, long b) throws Exception {
		inRange(a);
		inRange(b);
		if (b < 0)
			throw new Exception();
		if (a == 0 && b == 0)
			throw new Exception();
		if (a == 0)
			return 0;
		if (a == 1)
			return 1;
		if (a == -1) {
			if (b % 2 == 0)
				return 1;
			return -1;
		}
		long result = 1;
		for (long i = 1; i <= b; i++) {
			result *= a;
			inRange(result);
		}
		return result;
	}

	long factor() throws Exception {
		long result = 0;
		if (strValue.equals("-")) {
			int cnt = 0;
			while (current < units.size() && strValue.equals("-")) {
				cnt++;
				nextToken();
			}
			result = base();
			if (cnt % 2 != 0)
				result = -result;
		} else
			result = base();
		inRange(result);
		return result;
	}

	long base() throws Exception {
		long result = 0;
		if (strValue.equals("(")) {
			nextToken();
			result = expression();
			match(")");
			nextToken();
		} else
			result = number();
		inRange(result);
		return result;
	}

	long expression() throws Exception {
		long result = term();
		while (current < units.size()) {
			if (strValue.equals("+")) {
				nextToken();
				result = add(result, term());
			} else if (strValue.equals("-")) {
				nextToken();
				result = minus(result, term());
			} else {
				break;
			}
		}
		return result;
	}

	long term() throws Exception {
		long result = eterm();
		while (current < units.size()) {
			if (strValue.equals("*")) {
				nextToken();
				result = multiply(result, eterm());
			} else if (strValue.equals("/")) {
				nextToken();
				result = divide(result, eterm());
			} else if (strValue.equals("%")) {
				nextToken();
				result = mod(result, eterm());
			} else {
				break;
			}
		}
		return result;
	}

	long eterm() throws Exception {
		long result = factor();
		if (strValue != null && strValue.equals("^")) {
			nextToken();
			result = exp(result, eterm());
		}
		inRange(result);
		return result;
	}

	void work() throws Exception {
		long result = expression();
		if (current < units.size()) {
			throw new Exception();
		}
		System.out.println(result);
	}

	void run() throws Exception {
		int test = scan.nextInt();
		scan.nextLine();
		for (int cas = 1; cas <= test; cas++) {
			System.out.print("Case " + cas + ": ");
			try {
				init();
				work();
			} catch (Exception e) {
				System.out.println("ERROR!");
			}
		}
	}

	public static void main(String args[]) throws Exception {
		new Main().run();
	}
}

class Unit {

	String str = new String();
	boolean isNumber;

	public Unit() {
	}

	public Unit(String str) {
		this.str = str;
	}

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值