Skip to content

Instantly share code, notes, and snippets.

@iamcsharper
Created February 19, 2017 14:13
Show Gist options
  • Save iamcsharper/47c711bdc6d6fdb93b8dee4ec63890a3 to your computer and use it in GitHub Desktop.
Save iamcsharper/47c711bdc6d6fdb93b8dee4ec63890a3 to your computer and use it in GitHub Desktop.
Well, I tried
class ChemicalElement {
private:
float a;
int z;
bool _isMetal;
int period;
int group;
bool main;
std::string name; // Sodium
std::string sign; // Na
public:
ChemicalElement() {
}
ChemicalElement(std::string name) {
this->name = name;
}
ChemicalElement(float a, int z, int group, int period, bool main, bool metal, std::string name, std::string sign) {
this->group = group;
this->period = period;
this->_isMetal = metal;
this->main = main;
this->a = a;
this->z = z;
this->sign = sign;
this->name = name;
}
bool isMetal() {
return this->_isMetal;
}
int getPeriod() {
return this->period;
}
int getGroup() {
return this->group;
}
bool isMain() {
return this->main;
}
std::string getName() {
return this->name;
}
std::string getSign() {
return this->sign;
}
int getZ() {
return this->z;
}
int getA() {
return this->a;
}
};
namespace PeriodicTable {
std::vector<ChemicalElement*> elements;
std::map<std::string, int> indicies;
void add(ChemicalElement* el) {
indicies[el->getSign()] = elements.size();
elements.push_back(el);
}
void init() {
add(new ChemicalElement(1, 1, "Hydrogen", "H"));
add(new ChemicalElement(14, 7, "Nitrogen", "N"));
add(new ChemicalElement(16, 8, "Oxygen", "O"));
add(new ChemicalElement(19, 9, "Fluorine", "F"));
add(new ChemicalElement(23, 11, "Sodium", "Na"));
add(new ChemicalElement(39, 19, "Potassium", "K"));
add(new ChemicalElement(35.5f, 17, "Chlorine", "Cl"));
add(new ChemicalElement(107.8f, 47, "Silver", "Ag"));
}
ChemicalElement* getBySign(std::string name) {
std::cout << "[DEBUG] " << name << std::endl;
return elements[indicies[name]];
}
ChemicalElement* getByName(std::string sign) {
for (int i = 0; i < elements.size() && elements[i]->getName() != sign; ++i) {
return elements[i];
}
}
ChemicalElement* getByZ(int z) {
for (int i = 0; i < elements.size() && elements[i]->getZ() != z; ++i) {
return elements[i];
}
}
}
class Ion {
private:
int charge;
ChemicalElement* element;
const std::regex ionRegex = std::regex("(?:\\w)*(\\+|\\-[0-9]*)");
std::string textual;
void parse(std::string str) {
std::smatch result;
std::regex_search(str, result, ionRegex);
if (result[1] == "-") {
this->charge = -1;
}
else if (result[1] == "+") {
this->charge = 1;
}
else {
this->charge = std::stoi(result[1]);
}
this->element = PeriodicTable::getBySign(str.substr(0, str.length() - str.find(result[1]) + 1));
}
public:
// OH- -> O, H, charge = -1
// NO3- -> N, O, charge =
// Ag+
Ion(std::string str) {
this->textual = str;
this->parse(str);
std::cout << "Hello, I'm " << this->element->getName() << " I have charge: " << this->charge << std::endl;
}
};
class Molecule {
private:
std::vector<ChemicalElement*> elements;
const std::regex expression = std::regex("(?:\\w)*(\\+|\\-[0-9]*)");
std::string textual;
void parse() {
std::string atoms = str.substr(0, str.length() - str.find(result[1]) + 1);
std::cout << str << " " << result[1] << std::endl;
std::cout << "Atoms: " << atoms << std::endl;
// NaCl
// Ca(OH)2
// Na[Al(OH)4]
// (NH4)2SO4
int i = 0;
int j = -1;
std::string temp;
while (atoms[i]) {
char current = atoms[i];
temp += current;
if (isupper(current)) {
if (j != -1) {
//this->elements.push_back(PeriodicTable::getBySign(temp));
std::cout << temp;
temp = "";
}
j = i;
}
++i;
}
}
public:
};
class Reactor {
private:
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment