-
Notifications
You must be signed in to change notification settings - Fork 0
/
FormulaParser.cs
123 lines (112 loc) · 2.92 KB
/
FormulaParser.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
119
120
121
122
123
using System.Text;
namespace ChemTools {
/// <summary>
/// Class for parsing strings to Formulas.
/// </summary>
public class FormulaParser {
//the input string broken down into a character array
private char[] chars;
//index of next character
private int next;
private FormulaParser(string str) {
next = 0;
chars = str.ToCharArray();
}
//gets a Formula starting from next
//hydrates arent handled
private Formula GetFormula() {
Formula formula = new Formula();
while(BoundCheck) {
//end inner formula, return this
if(chars[next] == ')') {
Next();
return formula;
}
//start inner formula
else if(chars[next] == '(') {
Next();
//gets formula
Formula f1 = GetFormula();
//gets the number after the closing parens
//i.e. get 7 from (CO2)7
if(f1 == null ) return null;
int n = GetNumber();
//adds it to the main formula
formula.MergeFormula(f1.Factor(n));
}
//start symbol
else if (char.IsUpper(chars[next])){
Element e = GetSymbol();
if(e == null) {
continue;
}
int n = GetNumber();
formula.Add(new NElements(n, e));
//error or end of data
} else {
return null;
}
}
return formula;
}
//gets an Element from a symbol starting from next
private Element GetSymbol() {
StringBuilder sb = new StringBuilder();
if(BoundCheck && char.IsUpper(chars[next])){
sb.Append(chars[next]);
while(char.IsLower(Next())) {
sb.Append(chars[next]);
}
return ChemTools.Table.GetElementBySymbol(sb.ToString());
}
return null;
}
//gets a number (integer) that starts from next
private int GetNumber() {
StringBuilder sb = new StringBuilder();
if(BoundCheck && char.IsDigit(chars[next])){
sb.Append(chars[next]);
while(char.IsDigit(Next())) {
sb.Append(chars[next]);
}
return int.Parse(sb.ToString());
}
return 1;
}
//returns true if next is within bounds
//and the next character is not a space
private bool BoundCheck {
get {
return next < chars.Length && chars[next] != ' ';
}
}
//gets the next character
//increments the index (next)
private char Next() {
next++;
if(next >= chars.Length) {
return (char) 0;
}
return chars[next];
}
/// <summary> Only method needed to be used by a program. </summary>
/// <example>
/// Usage:
/// <c>
/// string input = "H2SO4";
/// Formula f = FormulaParser.ParseFormula(input);
/// Console.WriteLine(f); //prints "H2O4S" (sorted by atomic number)
/// input = "CH3COOH";
/// f = FormulaParser.ParseFormula(input);
/// Console.WriteLine(f); //prints "H4C2O2"
/// input = "Na(NO3)2";
/// f = FormulaParser.ParseFormula(input);
/// Console.WriteLine(f); //prints "N2O6Na"
/// </c>
/// </example>
public static Formula ParseFormula(string str){
//instantiates class to be thread-safe
return new FormulaParser(str).GetFormula();
}
}
}