-
Notifications
You must be signed in to change notification settings - Fork 0
/
Formula.cs
118 lines (105 loc) · 3.33 KB
/
Formula.cs
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
using System.Text;
using System.Collections.Generic;
namespace ChemTools {
/// <summary> A chemical formula like H2O, CO2, CH3COOH, etc. </summary>
public class Formula {
//Map that holds NElements
//accessible like an array, but uses keys instead of integers
//this map uses Elements as keys and holds NElements
//Given Element e, an NElement ne can be saved as:
//elementMap[e] = ne;
//and can later be retrieved by using
//elementMap[e]
public readonly Dictionary<Element, NElements> elementMap = new Dictionary<Element, NElements>();
/// <summary> Default empty formula </summary>
public Formula() {}
/// <summary> Copy constructor. </summary>
/// <param name="f"> Formula to copy. </param>
public Formula(Formula f) {
MergeFormula(f);
}
/// <summary> Gets molar mass of the formula </summary>
public double MolarMass {
get {
double mm = 0;
foreach(NElements ne in elementMap.Values) {
mm += ne.Number * ne.element.mass;
}
return mm;
}
}
/// <summary> Merges this formula with other.
/// Result is this object; other isn't affected. </summary>
/// <example> H2O merged with H2 will result in H4O </example>
public void MergeFormula(Formula other) {
foreach(NElements ne in other.elementMap.Values) {
Add(new NElements(ne));
}
}
/// <summary> Multiplies each element in formula by a constant.
/// Changes the formula itself, not the coefficient before the formula. </summary>
/// <returns> This formula for chaining/convenience. </returns>
/// <example> H2O Factor(2) results in H4O2, not 2H2O. </example>
public Formula Factor(int x) {
foreach(NElements ne in elementMap.Values) {
ne.Number = ne.Number * x;
}
return this;
}
/// <summary> Adds one or more elements to the formula. </summary>
/// <example> H2 Add(O) results in H2O </example>
public void Add(NElements ne) {
if(elementMap.ContainsKey(ne.element)) {
NElements ne2 = elementMap[ne.element];
ne2.Number += ne.Number;
} else {
elementMap.Add(ne.element, ne);
}
}
/// <summary> Gets the number of elements in the formula. </summary>
public int Size {
get {
return elementMap.Count;
}
}
/// <returns> true iff other is a Formula and
/// has the same number of each element </returns>
public override bool Equals(object other) {
if(other is Formula) {
Formula f = (Formula) other;
foreach(NElements ne in elementMap.Values) {
if(!f.elementMap.ContainsKey(ne.element) ||
!ne.Equals(f.elementMap[ne.element])) {
return false;
}
}
foreach(NElements ne in f.elementMap.Values) {
if(!f.elementMap.ContainsKey(ne.element) ||
!ne.Equals(elementMap[ne.element])) {
return false;
}
}
return true;
}
return false;
}
/// <returns> Integer representation of this
/// object. Not guaranteed to be unique. </returns>
public override int GetHashCode() {
int hash = 0;
foreach(NElements ne in elementMap.Values) {
hash ^= ne.GetHashCode();
}
return hash;
}
/// <returns> All the element-number sets in any order. </returns>
/// <example> H2O ToString() may return "H2O" or "O2H" </example>
public override string ToString(){
StringBuilder sb = new StringBuilder();
foreach(NElements ne in elementMap.Values) {
sb.Append(ne);
}
return sb.ToString();
}
}
}