forked from matemaynard99/Project_2A
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrecedence.java
More file actions
16 lines (16 loc) · 842 Bytes
/
Copy pathPrecedence.java
File metadata and controls
16 lines (16 loc) · 842 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
* Class to set values of precendence for the operators in the expression strings
*/
public class Precedence {
public static int precedence(String operator) {
if (operator.equals("^")) { return 7; }
if (operator.equals("*") || operator.equals("/")) { return 6; }
if (operator.equals("+") || operator.equals("-")) { return 5; }
if (operator.equals(">") || operator.equals(">=")) { return 4; }
if (operator.equals("<") || operator.equals("<=")) { return 4; }
if (operator.equals("==") || operator.equals("!=")) { return 3; }
if (operator.equals("&&")) { return 2; }
if (operator.equals("||")) { return 1; }
throw new IllegalArgumentException(String.format("Operator %s is not supported.", operator));
} // Time complexity: O(n)
}