-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSymbolWriter.cs
More file actions
214 lines (199 loc) · 7.7 KB
/
Copy pathSymbolWriter.cs
File metadata and controls
214 lines (199 loc) · 7.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
using System;
using System.Collections.Generic;
using System.IO;
namespace Writer
{
/// <summary>
/// Used to store the atomic symbols and convert words to atomic symbols
/// </summary>
class AtomicReader
{
public List<string> symbols, names;
/// <summary>
/// Loads the symbols and the names of the atoms from a file.
/// </summary>
public void LoadSymbolsAndNames()
{
try
{
//Load the file with the atom symbols and split it by commas
symbols = new List<string>(File.ReadAllText("AtomSymbols.csv").Split(','));
//Load the file with the atom names and split it by commas
names = new List<string>(File.ReadAllText("AtomNames.csv").Split(','));
}
catch (Exception)
{
//The file failed to open
Console.WriteLine("Unable to open file \"AtomSymbols.csv\" and/or \"AtomNames.csv\", where the atomic symbols are stored.");
Environment.Exit(-1);
}
}
/// <summary>
/// Returns the possible outcome of 2 characters
/// </summary>
Tuple<int, int> GetPossible(char c1, char c2)
{
//Get the characters to the periodic table standard (Upper and lower case. Eg.: Hg)
c1 = Char.ToUpper(c1);
c2 = Char.ToLower(c2);
//Check if there are character matches
int c1index = symbols.IndexOf(c1.ToString());
int c2index = symbols.IndexOf(c1.ToString() + c2.ToString());
//Return the number of matches
return new Tuple<int, int>(c1index, c2index);
}
/// <summary>
/// Gets a part of a string between 2 character indices
/// </summary>
string GetPart(string str, int start, int end)
{
string ret = "";
for (int i = start; i < end; i++)
ret += str[i];
return ret;
}
/// <summary>
/// Converts a string to atomic symbols
/// </summary>
public List<int> Convert(string word)
{
//Check if there's any match with the first or second character
Tuple<int, int> res;
if (word.Length >= 2)
res = GetPossible(word[0], word[1]);
else
res = GetPossible(word[0], '\0');
//There's no match. Impossible to convert the first two characters
if (res.Item1 == -1 && res.Item2 == -1)
return new List<int>();
//If the 2 first characters match to an atomic symbol
if (res.Item2 != -1)
{
//Get the rest of the string to process those characters
string ToFeed = GetPart(word, 2, word.Length);
if (ToFeed != "")
{
//Process the rest of the string
List<int> ret = new List<int>();
ret.Add(res.Item2);
List<int> NewSymbols = Convert(ToFeed);
for (int i = 0; i < NewSymbols.Count; i++)
ret.Add(NewSymbols[i]);
if (NewSymbols.Count != 0)
return ret;
}
else
return new List<int>() { res.Item2 };
}
//Check if only the 1st character matches
if (res.Item1 != -1)
{
//Get the rest of the string to process those characters
string ToFeed = GetPart(word, 1, word.Length);
if (ToFeed != "")
{
//Process the rest of the string
List<int> ret = new List<int>();
ret.Add(res.Item1);
List<int> NewSymbols = Convert(ToFeed);
for (int i = 0; i < NewSymbols.Count; i++)
ret.Add(NewSymbols[i]);
if (NewSymbols.Count != 0)
return ret;
}
else
return new List<int>() { res.Item1 };
}
return new List<int>();
}
}
/// <summary>
/// The class where the entry point is.
/// </summary>
static class MainClass
{
/// <summary>
/// Writes the help message (when you have wrong arguments or run SymbolWriter -help).
/// </summary>
static void WriteHelpMessage()
{
Console.WriteLine("Error. SymbolWriter Usage: \"mono SymbolWriter.exe [words]\" to write words as atomic symbols or \"mono SymbolWriter.exe -help\" to get this message.");
}
/// <summary>
/// The entry point of the program.
/// </summary>
static void Main(string[] arguments)
{
//Check for the right number of arguments (1 or more words)
if (arguments.Length == 0)
{
WriteHelpMessage();
Environment.Exit(-1);
}
//Check if the user wanted help
if (arguments[0] == "-help")
{
WriteHelpMessage();
Environment.Exit(-1);
}
//Join all the words into an array. This also separates arguments with multiple words.
List<string> words = new List<string>();
for (int i = 0; i < arguments.Length; i++)
{
string[] ArgumentWords = arguments[i].Split(' ');
for (int j = 0; j < ArgumentWords.Length; j++)
{
if (ArgumentWords[j] != "")
words.Add(ArgumentWords[j]);
}
}
//Load the atomic elements
AtomicReader reader = new AtomicReader();
reader.LoadSymbolsAndNames();
for (int i = 0; i < words.Count; i++)
{
//Convert the word to atoms
List<int> converted = reader.Convert(words[i]);
//Print the atoms to the console
string ToPrint = words[i] + ": ";
//The list is empty, there is no way of writing a word with element symbols
if (converted.Count == 0)
{
ToPrint += "Impossible to convert";
Console.WriteLine(ToPrint);
}
else
{
//Write atom symbols
for (int j = 0; j < converted.Count; j++)
{
ToPrint += reader.symbols[converted[j]];
//Don't write the last space
if (j != converted.Count - 1)
ToPrint += ' ';
}
ToPrint += "; ";
//Write atom names
for (int j = 0; j < converted.Count; j++)
{
ToPrint += reader.names[converted[j]];
//Don't write the last space
if (j != converted.Count - 1)
ToPrint += ' ';
}
ToPrint += "; ";
//Write element numbers (Z)
for (int j = 0; j < converted.Count; j++)
{
ToPrint += (converted[j] + 1).ToString();
//Don't write the last space
if (j != converted.Count - 1)
ToPrint += ' ';
}
//Print everything to the console
Console.WriteLine(ToPrint);
}
}
}
}
}