-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpression.java
More file actions
99 lines (90 loc) · 3.43 KB
/
Expression.java
File metadata and controls
99 lines (90 loc) · 3.43 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package app;
import java.io.*;
import java.util.*;
import java.util.regex.*;
import structures.Stack;
public class Expression {
public static String delims = " \t*+-/()[]";
/**
* Populates the vars list with simple variables, and arrays lists with arrays
* in the expression. For every variable (simple or array), a SINGLE instance is created
* and stored, even if it appears more than once in the expression.
* At this time, values for all variables and all array items are set to
* zero - they will be loaded from a file in the loadVariableValues method.
*
* @param expr The expression
* @param vars The variables array list - already created by the caller
* @param arrays The arrays array list - already created by the caller
*/
public static void
makeVariableLists(String expr, ArrayList<Variable> vars, ArrayList<Array> arrays) {
String expr1=expr;
if(expr1.substring(
{
continue;
}
else if(expr1.substring(1,2)==vars)
{
expr.substring(1,2)==vars.value;
}
else if(expr1.substring(1,2)==arrays) {
expr.substring(1,2)==arrays.value;
}
makeVariableLists(String expr1.substring(1))
/** COMPLETE THIS METHOD **/
/** DO NOT create new vars and arrays - they are already created before being sent in
** to this method - you just need to fill them in.
**/
}
/**
* Loads values for variables and arrays in the expression
*
* @param sc Scanner for values input
* @throws IOException If there is a problem with the input
* @param vars The variables array list, previously populated by makeVariableLists
* @param arrays The arrays array list - previously populated by makeVariableLists
*/
public static void
loadVariableValues(Scanner sc, ArrayList<Variable> vars, ArrayList<Array> arrays)
throws IOException {
while (sc.hasNextLine()) {
StringTokenizer st = new StringTokenizer(sc.nextLine().trim());
int numTokens = st.countTokens();
String tok = st.nextToken();
Variable var = new Variable(tok);
Array arr = new Array(tok);
int vari = vars.indexOf(var);
int arri = arrays.indexOf(arr);
if (vari == -1 && arri == -1) {
continue;
}
int num = Integer.parseInt(st.nextToken());
if (numTokens == 2) { // scalar symbol
vars.get(vari).value = num;
} else { // array symbol
arr = arrays.get(arri);
arr.values = new int[num];
// following are (index,val) pairs
while (st.hasMoreTokens()) {
tok = st.nextToken();
StringTokenizer stt = new StringTokenizer(tok," (,)");
int index = Integer.parseInt(stt.nextToken());
int val = Integer.parseInt(stt.nextToken());
arr.values[index] = val;
}
}
}
}
/**
* Evaluates the expression.
*
* @param vars The variables array list, with values for all variables in the expression
* @param arrays The arrays array list, with values for all array items
* @return Result of evaluation
*/
public static float evaluate(String expr, ArrayList<Variable> vars, ArrayList<Array> arrays) {
/** COMPLETE THIS METHOD **/
// following line just a placeholder for compilation
return 0;
}
}