![]() |
ENSDF++ 1.1
An easy, fast and simple way to run querys towards the ENSDF database, written in C++.
|
00001 #include "Nukleid.h" 00002 using namespace std; 00003 00004 Nukleid::Nukleid() 00005 { 00006 name="UNKNOWN"; 00007 element="UNKNOWN"; 00008 A=0; 00009 Z=0; 00010 } 00011 00012 Nukleid::Nukleid(string nucleid) 00013 { 00014 name=trimString(nucleid); 00015 parseNukleidName(nucleid, A, element); 00016 Z=parseAtomicNumber(element); 00017 } 00018 00019 string Nukleid::trimString(string toTrim) 00020 { 00021 string results = ""; 00022 for(unsigned int i = 0; i<toTrim.size(); i++) 00023 if(!(toTrim[i]==' ' || toTrim[i]=='\n' || toTrim[i]=='\t')) 00024 results+=toTrim[i]; 00025 return results; 00026 } 00027 00028 unsigned short int Nukleid::parseAtomicNumber(string elementToParse) 00029 { 00030 ElementLookupTable myTable; 00031 return myTable.lookupElement(elementToParse); 00032 } 00033 00034 string Nukleid::toString() const 00035 { 00036 if(element.size()>10) 00037 return "STRINGERROR"; 00038 stringstream ss; 00039 ss << A << element; 00040 return ss.str(); 00041 } 00042 00043 string Nukleid::getElement() const 00044 { 00045 return element; 00046 } 00047 00048 int Nukleid::getZ() const 00049 { 00050 return Z; 00051 } 00052 00053 int Nukleid::getA() const 00054 { 00055 return A; 00056 } 00057 00058 // returns 0 as mass and "" as element if the input is invalid 00059 void Nukleid::parseNukleidName(string nucleid, short unsigned int &mass, string &element) 00060 { 00061 00063 if (nucleid.length() != 5 && nucleid.length()!=6) 00064 { 00065 throw DataFileException("Nucleid string was of wrong length."); 00066 return; 00067 } 00068 00069 for (int i = 0; i < 3; i++) 00070 { 00071 if (!((nucleid.at(i) >= '0' && nucleid.at(i) <= '9') || nucleid.at(i) == ' ')) 00072 { 00073 throw DataFileException("Nucleid number was invalid: '''" + nucleid + "'''"); 00074 return; 00075 } 00076 } 00077 00078 for (unsigned int i = 3; i < nucleid.length(); i++) 00079 { 00080 if(nucleid.at(i)>='a' && nucleid.at(i)<='z') 00081 { 00082 nucleid[i]+='A'-'a'; 00083 } 00084 if (!((nucleid.at(i) >= 'A' && nucleid.at(i) <= 'Z') || (nucleid.at(i) == ' ') || (nucleid.at(i) >= '0' && nucleid.at(i) <= '9'))) 00085 { 00086 throw DataFileException("Nucleid name was invalid: '''" + nucleid + "'''"); 00087 return; 00088 } 00089 } 00091 00092 string number = nucleid.substr(0, 3); 00093 const char* numberCString = number.c_str(); 00094 mass = atoi(numberCString); 00095 00096 if (nucleid.at(4) == ' ') 00097 element = nucleid.at(3); 00098 else 00099 if(nucleid.length()==5) 00100 element = nucleid.substr(3,2); 00101 else 00102 if(nucleid[5]!=' ') 00103 element = nucleid.substr(3,3); 00104 else 00105 element = nucleid.substr(3,2); 00106 } 00107 00108 bool Nukleid::operator == (const Nukleid & rhs) const 00109 { 00110 if (Z == rhs.Z && A == rhs.A && element.compare(rhs.element)==0) 00111 return true; 00112 return false; 00113 } 00114 00115 bool Nukleid::operator > (const Nukleid & rhs) const 00116 { 00117 if (Z > rhs.Z) 00118 return true; 00119 else if (Z == rhs.Z) 00120 { 00121 if(name.compare(rhs.name)==0) 00122 return false; 00123 return name.compare(rhs.name)>0; 00124 } 00125 return false; 00126 } 00127 00128 bool Nukleid::operator < (const Nukleid & rhs) const 00129 { 00130 if (Z > rhs.Z) 00131 { 00132 return false; 00133 } 00134 else if (Z == rhs.Z) 00135 { 00136 if(name.compare(rhs.name)==0) 00137 { 00138 return false; 00139 } 00140 return !(name.compare(rhs.name)>0); 00141 } 00142 return true; 00143 }