forked from examplehub/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrecedences.java
More file actions
69 lines (67 loc) · 1.3 KB
/
Precedences.java
File metadata and controls
69 lines (67 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.examplehub.matrix;
/** https://en.wikipedia.org/wiki/Order_of_operations */
public class Precedences {
private static int getPrecedences(String operator) {
switch (operator) {
case ",":
return 0;
case "=":
case "+=":
case "-=":
case "*=":
case "/=":
case "%=":
case "&=":
case "|=":
case "^=":
case "<<=":
case ">>=":
return 1;
case "?:":
return 2;
case "||":
return 3;
case "&&":
return 4;
case "|":
return 5;
case "^":
return 6;
case "&":
return 7;
case "==":
case "!=":
return 8;
case "<":
case "<=":
case ">":
case ">=":
return 9;
case "<<":
case ">>":
return 10;
case "+":
case "-":
return 11;
case "*":
case "/":
case "%":
return 12;
case "!":
case "~":
return 13;
case "(":
case ")":
case "[":
case "]":
case "->":
case ".":
return 14;
default:
return -1;
}
}
public static int compare(String firstOperator, String secondOperator) {
return Integer.compare(getPrecedences(firstOperator), getPrecedences(secondOperator));
}
}