第一题
static String toHex(int n) { Deque<Integer> deque = new ArrayDeque<>(); char[] hexMap = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; while (n != 0) { deque.push(n % 16); n = n / 16; } StringBuilder sb = new StringBuilder(); while (!deque.isEmpty()) { sb.append(hexMap[deque.pop()]); } return sb.toString();}
第二题
public class SuffixA { public static void main(String[] args) { String exp = "1 + 2 * (9 - 5)"; SuffixExpressionA se = compile(exp); int result = se.execute(); System.out.println(exp + " = " + result + " " + (result == 1 + 2 * (9 - 5) ? "✓" : "✗")); } static SuffixExpressionA compile(String exp) { // TODO: HashMap<Character, Integer> precedence = new HashMap<>(); precedence.put('+', 0); precedence.put('-', 0); precedence.put('*', 1); precedence.put('/', 1); precedence.put('(', -1); exp = exp.replaceAll("\\s",""); Deque<Character> operator = new ArrayDeque<>(); String result = ""; for (char c:exp.toCharArray()) { if (Character.isDigit(c)) { result += c; } else if (c == '(') { operator.push(c); } else if (c == ')') { // remove parentheses and output operators inside parentheses while (operator.peek() != '(') { result += operator.pop(); } operator.pop(); } else if (precedence.containsKey(c)) { // output operators with lower/equal precedence while (!operator.isEmpty() && precedence.get(c) <= precedence.get(operator.peek())) { if (operator.peek() == '(') throw new IllegalArgumentException(); result += operator.pop(); } // push income operator to stack operator.push(c); } else throw new IllegalArgumentException(); } while (!operator.isEmpty()) { result += operator.pop(); } return new SuffixExpressionA(result); }}class SuffixExpressionA { private String exp; public SuffixExpressionA (String expAftCom) { this.exp = expAftCom; } int execute() { Deque<Integer> operand = new ArrayDeque<>(); for (char c:exp.toCharArray()) { if (Character.isDigit(c)) { operand.push(Character.getNumericValue(c)); } else { operand.push(cal(c, operand.pop(), operand.pop())); } } if (operand.size() > 1) throw new IllegalArgumentException(); return operand.pop(); } private int cal(char o, int b, int a) { switch (o) { case '+' -> { return a + b; } case '-' -> { return a - b; } case '*' -> { return a * b; } case '/' -> { return a / b; } } throw new IllegalArgumentException(); }}
第三题
public class SuffixB { public static void main(String[] args) { String exp = "x + 2 * (y - 5)"; SuffixExpressionB se = compile(exp); Map<String, Integer> env = Map.of("x", 1, "y", 9); int result = se.execute(env); System.out.println(exp + " = " + result + " " + (result == 1 + 2 * (9 - 5) ? "✓" : "✗")); } static SuffixExpressionB compile(String exp) { HashMap<Character, Integer> precedence = new HashMap<>(); precedence.put('+', 0); precedence.put('-', 0); precedence.put('*', 1); precedence.put('/', 1); precedence.put('(', -1); exp = exp.replaceAll("\\s",""); Deque<Character> operator = new ArrayDeque<>(); String result = ""; for (char c:exp.toCharArray()) { if (Character.isDigit(c) || c == 'x' || c == 'y') { result += c; } else if (c == '(') { operator.push(c); } else if (c == ')') { // remove parentheses and output operators inside parentheses while (operator.peek() != '(') { result += operator.pop(); } operator.pop(); } else if (precedence.containsKey(c)) { // output operators with lower/equal precedence while (!operator.isEmpty() && precedence.get(c) <= precedence.get(operator.peek())) { if (operator.peek() == '(') throw new IllegalArgumentException(); result += operator.pop(); } // push income operator to stack operator.push(c); } else throw new IllegalArgumentException(); } while (!operator.isEmpty()) { result += operator.pop(); } return new SuffixExpressionB(result); }}class SuffixExpressionB { private String exp; public SuffixExpressionB (String expAftCom) { this.exp = expAftCom; } int execute(Map<String, Integer> env) { // replace variables with values Set<String> var = env.keySet(); for (String s:var) { exp = exp.replaceAll(s, String.valueOf(env.get(s))); } // same calculation as SuffixA Deque<Integer> operand = new ArrayDeque<>(); for (char c:exp.toCharArray()) { if (Character.isDigit(c)) { operand.push(Character.getNumericValue(c)); } else { operand.push(cal(c, operand.pop(), operand.pop())); } } if (operand.size() > 1) throw new IllegalArgumentException(); return operand.pop(); } private int cal(char o, int b, int a) { switch (o) { case '+' -> { return a + b; } case '-' -> { return a - b; } case '*' -> { return a * b; } case '/' -> { return a / b; } } throw new IllegalArgumentException(); }}
Sign in to make a reply
Healthy_Life_Style
第一题
第二题
第三题