Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

kmolelements.cpp

Go to the documentation of this file.
00001 /*
00002  * kmolelements.cpp
00003  *
00004  * Copyright (C) 2000 Tomislav Gountchev <tomi@idiom.com>
00005  */
00006 
00007 // classes that store and manipulate chemical formulas represented as
00008 // lists of elements
00009 
00010 #include <stdio.h>
00011 #include "kmolelements.h"
00012 
00016 SubUnit::SubUnit () {}
00017 
00018 SubUnit::~SubUnit () {}
00019 
00024 SubUnit* SubUnit::makeSubUnit(QString line) {
00025   QString name, grpname, weight, coef;
00026   QTextStream str (line, IO_ReadOnly);
00027   str >> name;
00028   if (name != "-group") { // not a group - must be represented as Element
00029     str >> weight >> ws;
00030     return new Element(name, weight.toDouble());
00031   }
00032   else {
00033     str >> grpname;
00034     ElementList* els = new ElementList(grpname); // group - make an ElementList
00035     while (!str.eof()) {
00036       str >> name >> ws;
00037       str >> coef >> ws;
00038       els->addElement(name, coef.toDouble());
00039     }
00040     return els;
00041   }
00042 }
00043 
00044 QString SubUnit::getName() const {
00045   return QString("None");
00046 }
00047 
00051 double SubUnit::getWeight(QDict<SubUnit>* ) const {
00052   return -1;
00053 }
00054 
00058 ElementList::ElementList () {
00059   elements = new QList<ElementCoef>;
00060 }
00061 
00062 ElementList::~ElementList () {
00063   delete elements;
00064 }
00065 
00066 
00070 ElementList::ElementList (QString name) {
00071   this->name = name;
00072   elements = new QList<ElementCoef>;
00073 }
00074 
00078 void ElementList::writeOut(QString& line) {
00079   QString coef;
00080   line = "-group " + name;
00081   ElementCoef* current = elements->first();
00082   while (current != 0) {
00083     line += " " + current->name + " " + coef.setNum(current->coef, 'g', 10);
00084     // precision set to 10 digits
00085     current = elements->next();
00086   }
00087 }
00088 
00092 double ElementList::getWeight(QDict<SubUnit>* elstable) const {
00093   double weight = 0;
00094   ElementCoef* current = elements->first();
00095   while (current != 0) {
00096     SubUnit* e = elstable->find(current->name);
00097     if (e != 0) {
00098       weight += (current->coef) * (e->getWeight(elstable));
00099     } else return -1; //ERROR
00100     current = elements->next();
00101   }
00102   return weight;
00103 }
00104 
00109 QString ElementList::getEA(QDict<SubUnit>* elstable, double mw) const {
00110   if (mw == 0) mw = getWeight(elstable);
00111   QString ea;
00112   QString temp;
00113   ElementCoef* current = elements->first();
00114   while (current != 0) {
00115     SubUnit* e = elstable->find(current->name);
00116     if (e != 0) {
00117       double current_percent = 100 * (current->coef) *
00118         (e->getWeight(elstable))
00119         / mw;
00120       ea += current->name + "\t" +
00121         temp.setNum(current_percent) + "\n";
00122     } else return QString("ERROR!\n"); //ERROR
00123     current = elements->next();
00124   }
00125   return ea;
00126 }
00127 
00131 QString ElementList::getEmpFormula() const {
00132   QString ef;
00133   QString temp;
00134   ElementCoef* current = elements->first();
00135   while (current != 0) {
00136     ef += current->name + temp.setNum(current->coef);
00137     current = elements->next();
00138   }
00139   return ef;
00140 }
00141 
00145 void ElementList::multiplyBy(double coef) {
00146   ElementCoef* current = elements->first();
00147   while (current != 0) {
00148     (current->coef) *= coef;
00149     current = elements->next();
00150   }
00151 }
00152 
00156 void ElementList::addTo(ElementList& els, double coef) {
00157   ElementCoef* current = elements->first();
00158   while (current != 0) {
00159     els.addElement(current->name, (current->coef) * coef);
00160     current = elements->next();
00161   }
00162 }
00163 
00169 void ElementList::addElement(const QString& name, double coef) {
00170   ElementCoef* current = elements->first();
00171   while (current != 0) {
00172     if (current->name == name) {
00173       current->coef += coef;
00174       return;
00175     }
00176     current = elements->next();
00177   }
00178   elements->append(new ElementCoef(name, coef));
00179 }
00180 
00184 bool ElementList::contains(const QString& name) {
00185   ElementCoef* current = elements->first();
00186   while (current != 0) {
00187     if (current->name == name)
00188       return true;
00189     current = elements->next();
00190   }
00191   return false;
00192 }
00193 
00194 bool ElementList::isEmpty() {
00195   return elements->isEmpty();
00196 }
00197 
00198 QString ElementList::getName() const {
00199   return name;
00200 }
00201 
00205 Element::Element(const QString& n, double w)
00206   : weight(w), name(n) { }
00207 
00208 
00209 Element::~Element() {
00210 }
00211 
00212 
00216 void Element::writeOut(QString& line) {
00217   line.setNum(weight);
00218   line = name + " " + line;
00219 }
00220 
00221 double Element::getWeight(QDict<SubUnit>* ) const {
00222   return weight;
00223 }
00224 
00225 void Element::addTo(ElementList& els, double coef) {
00226   els.addElement(name, coef);
00227 }
00228 
00229 QString Element::getName() const {
00230   return name;
00231 }
00232 
00237 ElementCoef::ElementCoef(const QString& n, double c) : name(n), coef(c) {}
00238 

Generated on Sat Nov 5 16:17:07 2005 for OPIE by  doxygen 1.4.2