-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEquation.java
More file actions
200 lines (190 loc) · 5.7 KB
/
Equation.java
File metadata and controls
200 lines (190 loc) · 5.7 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import java.util.*;
public class Equation {
public ArrayList<ChemicalCompoundable> reactants;
public ArrayList<ChemicalCompoundable> products;
public int[] reactantAmounts;
public int[] productAmounts;
boolean balanced;
public Equation(ArrayList<ChemicalCompoundable> r, ArrayList<ChemicalCompoundable> p) {
reactants = r;
products = p;
reactantAmounts = new int[reactants.size()];
Arrays.fill(reactantAmounts, 1);
productAmounts = new int[products.size()];
Arrays.fill(productAmounts, 1);
balanced = false;
}
public Equation(String str) {
StringTokenizer st = new StringTokenizer(str);
boolean isProduct = false;
reactants = new ArrayList<ChemicalCompoundable>();
products = new ArrayList<ChemicalCompoundable>();
while (st.hasMoreTokens()) {
String token = st.nextToken();
if (token.equals("->")) {
isProduct = true;
} else if (!token.equals("+")) {
if (isProduct) {
products.add(new Chemical(token));
} else {
reactants.add(new Chemical(token));
}
}
}
reactantAmounts = new int[reactants.size()];
Arrays.fill(reactantAmounts, 1);
productAmounts = new int[products.size()];
Arrays.fill(productAmounts, 1);
balanced = false;
}
public void balance() {
balanced = true;
LinkedList<State> stack = new LinkedList();
stack.add(new State(reactantAmounts, productAmounts));
HashSet<String> done = new HashSet<>();
while (true) {
State curState = stack.remove();
ArrayList<Element> offSets = curState.getStateOffSets(reactants, products);
if (offSets.size() == 0) {
reactantAmounts = curState.reactAmounts;
productAmounts = curState.prodAmounts;
break;
}
boolean[] incrementR = new boolean[reactants.size()];
boolean[] incrementP = new boolean[products.size()];
for (int i = 0; i < offSets.size(); i++) {
Element unBalancedElem = offSets.get(i);
if (unBalancedElem.amount > 0) {
// add product
for (int j = 0; j < products.size(); j++) {
if (incrementP[j])
continue;
if (products.get(j).copiesOfElement(unBalancedElem) > 0) {
incrementP[j] = true;
}
}
} else {
// add react
for (int j = 0; j < reactants.size(); j++) {
if (incrementR[j])
continue;
if (reactants.get(j).copiesOfElement(unBalancedElem) > 0) {
incrementR[j] = true;
}
}
}
}
for (int i = 0; i < incrementR.length; i++) {
if (incrementR[i]) {
int[] newAmounts = new int[incrementR.length];
System.arraycopy(curState.reactAmounts, 0, newAmounts, 0, incrementR.length);
newAmounts[i]++;
State newState = new State(newAmounts, curState.prodAmounts);
String str = newState.toString();
if (!done.contains(str)) {
stack.add(newState);
done.add(str);
}
}
}
for (int i = 0; i < incrementP.length; i++) {
int[] newAmounts = new int[incrementP.length];
System.arraycopy(curState.prodAmounts, 0, newAmounts, 0, incrementP.length);
newAmounts[i]++;
State newState = new State(curState.reactAmounts, newAmounts);
String str = newState.toString();
if (!done.contains(str)) {
stack.add(newState);
done.add(str);
}
}
}
}
private class State {
public int[] reactAmounts;
public int[] prodAmounts;
public State(int[] r, int[] p) {
reactAmounts = r;
prodAmounts = p;
}
public ArrayList<Element> getStateOffSets(ArrayList<ChemicalCompoundable> r,
ArrayList<ChemicalCompoundable> p) {
ArrayList<Element> arr = new ArrayList<Element>();
for (int i = 0; i < r.size(); i++) {
ArrayList<Element> curList = r.get(i).getElements();
for (int j = 0; j < curList.size(); j++) {
Element cur = curList.get(j);
int index = arr.indexOf(cur);
if (index == -1) {
arr.add(new Element(cur.id, reactAmounts[i] * cur.amount));
} else {
arr.get(index).amount += reactAmounts[i] * cur.amount;
}
}
}
for (int i = 0; i < p.size(); i++) {
ArrayList<Element> curList = p.get(i).getElements();
for (int j = 0; j < curList.size(); j++) {
Element cur = curList.get(j);
int index = arr.indexOf(cur);
arr.get(index).amount -= prodAmounts[i] * cur.amount;
}
}
for (int i = 0; i < arr.size(); i++) {
if (arr.get(i).amount == 0) {
arr.remove(i);
i--;
}
}
return arr;
}
@Override
public String toString(){
return Arrays.toString(reactAmounts)+Arrays.toString(prodAmounts);
}
}
public int indexOf(ArrayList arr, Element e) {
for (int i = 0; i < arr.size(); i++)
if (e.id == ((Element) arr.get(i)).id)
return i;
return -1;
}
public String toLatexString() {
String str = "";
System.out.println("reactants: " + reactants + ", products:" + products);
for (int i = 0; i < reactants.size(); i++) {
if (reactantAmounts[i] != 1) {
str += reactantAmounts[i];
}
str += reactants.get(i).toLatexString();
if (i < reactants.size() - 1) {
str += "%20+%20";
} else {
str += "%20\\rightarrow%20";
}
}
for (int i = 0; i < products.size(); i++) {
if (productAmounts[i] != 1) {
str += productAmounts[i];
}
str += products.get(i).toLatexString();
if (i < products.size() - 1) {
str += "%20+%20";
}
}
return str;
}
public int amountOf(Chemical c) {
for (int i = 0; i < reactants.size(); i++) {
if (reactants.get(i).getElements().equals(c.getElements())) {
return reactantAmounts[i];
}
}
for (int i = 0; i < products.size(); i++) {
if (products.get(i).getElements().equals(c.getElements())) {
return productAmounts[i];
}
}
return 0;
}
}