HDU 3624: Digital Calculator

来源:互联网 发布:佳格大数据怎么样 编辑:程序博客网 时间:2024/05/16 06:18

链接: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;elsestrValue = 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;} elseresult = base();inRange(result);return result;}long base() throws Exception {long result = 0;if (strValue.equals("(")) {nextToken();result = expression();match(")");nextToken();} elseresult = 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;}}



原创粉丝点击