author | kergoth <kergoth> | 2002-09-15 17:23:20 (UTC) |
---|---|---|
committer | kergoth <kergoth> | 2002-09-15 17:23:20 (UTC) |
commit | 9bd6edd077df796677c9d069200172f930ac1304 (patch) (unidiff) | |
tree | 3b473d4280249b5ade8e5276758cc8f9aa3c2822 /noncore/apps/checkbook/kmolelements.cpp | |
parent | 33ec9ead7ce30bf9450b9048506f0bda49ba4791 (diff) | |
download | opie-9bd6edd077df796677c9d069200172f930ac1304.zip opie-9bd6edd077df796677c9d069200172f930ac1304.tar.gz opie-9bd6edd077df796677c9d069200172f930ac1304.tar.bz2 |
Reverting to checkbook in its previous state, hopefully i got everything.
Diffstat (limited to 'noncore/apps/checkbook/kmolelements.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | noncore/apps/checkbook/kmolelements.cpp | 238 |
1 files changed, 0 insertions, 238 deletions
diff --git a/noncore/apps/checkbook/kmolelements.cpp b/noncore/apps/checkbook/kmolelements.cpp deleted file mode 100644 index ce8f9c1..0000000 --- a/noncore/apps/checkbook/kmolelements.cpp +++ b/dev/null | |||
@@ -1,238 +0,0 @@ | |||
1 | /* | ||
2 | * kmolelements.cpp | ||
3 | * | ||
4 | * Copyright (C) 2000 Tomislav Gountchev <tomi@idiom.com> | ||
5 | */ | ||
6 | |||
7 | // classes that store and manipulate chemical formulas represented as | ||
8 | // lists of elements | ||
9 | |||
10 | #include <stdio.h> | ||
11 | #include "kmolelements.h" | ||
12 | |||
13 | /** | ||
14 | * A generic chemical entity. Can be an element or a group. | ||
15 | */ | ||
16 | SubUnit::SubUnit () {} | ||
17 | |||
18 | SubUnit::~SubUnit () {} | ||
19 | |||
20 | /** | ||
21 | * Construct a subunit and return a pointer to it. The syntax of LINE is | ||
22 | * the one used in the element definition file. | ||
23 | */ | ||
24 | SubUnit* SubUnit::makeSubUnit(QString line) { | ||
25 | QString name, grpname, weight, coef; | ||
26 | QTextStream str (line, IO_ReadOnly); | ||
27 | str >> name; | ||
28 | if (name != "-group") { // not a group - must be represented as Element | ||
29 | str >> weight >> ws; | ||
30 | return new Element(name, weight.toDouble()); | ||
31 | } | ||
32 | else { | ||
33 | str >> grpname; | ||
34 | ElementList* els = new ElementList(grpname); // group - make an ElementList | ||
35 | while (!str.eof()) { | ||
36 | str >> name >> ws; | ||
37 | str >> coef >> ws; | ||
38 | els->addElement(name, coef.toDouble()); | ||
39 | } | ||
40 | return els; | ||
41 | } | ||
42 | } | ||
43 | |||
44 | QString SubUnit::getName() const { | ||
45 | return QString("None"); | ||
46 | } | ||
47 | |||
48 | /** | ||
49 | * Get the molecular weight of THIS, based on the data from ELSTABLE. | ||
50 | */ | ||
51 | double SubUnit::getWeight(QDict<SubUnit>* elstable) const { | ||
52 | return -1; | ||
53 | } | ||
54 | |||
55 | /** | ||
56 | * A group of elements. | ||
57 | */ | ||
58 | ElementList::ElementList () { | ||
59 | elements = new QList<ElementCoef>; | ||
60 | } | ||
61 | |||
62 | ElementList::~ElementList () { | ||
63 | delete elements; | ||
64 | } | ||
65 | |||
66 | |||
67 | /** | ||
68 | * A group of elements. | ||
69 | */ | ||
70 | ElementList::ElementList (QString name) { | ||
71 | this->name = name; | ||
72 | elements = new QList<ElementCoef>; | ||
73 | } | ||
74 | |||
75 | /** | ||
76 | * Write THIS to LINE, in a format suitable for the element definition file. | ||
77 | */ | ||
78 | void ElementList::writeOut(QString& line) { | ||
79 | QString coef; | ||
80 | line = "-group " + name; | ||
81 | ElementCoef* current = elements->first(); | ||
82 | while (current != 0) { | ||
83 | line += " " + current->name + " " + coef.setNum(current->coef, 'g', 10); | ||
84 | // precision set to 10 digits | ||
85 | current = elements->next(); | ||
86 | } | ||
87 | } | ||
88 | |||
89 | /** | ||
90 | * Get the molecular weight of THIS, based on the data from ELSTABLE. | ||
91 | */ | ||
92 | double ElementList::getWeight(QDict<SubUnit>* elstable) const { | ||
93 | double weight = 0; | ||
94 | ElementCoef* current = elements->first(); | ||
95 | while (current != 0) { | ||
96 | SubUnit* e = elstable->find(current->name); | ||
97 | if (e != 0) { | ||
98 | weight += (current->coef) * (e->getWeight(elstable)); | ||
99 | } else return -1; //ERROR | ||
100 | current = elements->next(); | ||
101 | } | ||
102 | return weight; | ||
103 | } | ||
104 | |||
105 | /** | ||
106 | * Return a string representing the elemental composition of THIS, as | ||
107 | * a tab-separated element - percentage pairs, separated by newlines. | ||
108 | */ | ||
109 | QString ElementList::getEA(QDict<SubUnit>* elstable, double mw) const { | ||
110 | if (mw == 0) mw = getWeight(elstable); | ||
111 | QString ea; | ||
112 | QString temp; | ||
113 | ElementCoef* current = elements->first(); | ||
114 | while (current != 0) { | ||
115 | SubUnit* e = elstable->find(current->name); | ||
116 | if (e != 0) { | ||
117 | double current_percent = 100 * (current->coef) * | ||
118 | (e->getWeight(elstable)) | ||
119 | / mw; | ||
120 | ea += current->name + "\t" + | ||
121 | temp.setNum(current_percent) + "\n"; | ||
122 | } else return QString("ERROR!\n"); //ERROR | ||
123 | current = elements->next(); | ||
124 | } | ||
125 | return ea; | ||
126 | } | ||
127 | |||
128 | /** | ||
129 | * Return a string representing THIS as an empirical chemical formula. | ||
130 | */ | ||
131 | QString ElementList::getEmpFormula() const { | ||
132 | QString ef; | ||
133 | QString temp; | ||
134 | ElementCoef* current = elements->first(); | ||
135 | while (current != 0) { | ||
136 | ef += current->name + temp.setNum(current->coef); | ||
137 | current = elements->next(); | ||
138 | } | ||
139 | return ef; | ||
140 | } | ||
141 | |||
142 | /** | ||
143 | * Multiply THIS (i.e. the coefficient of each element) by coef. | ||
144 | */ | ||
145 | void ElementList::multiplyBy(double coef) { | ||
146 | ElementCoef* current = elements->first(); | ||
147 | while (current != 0) { | ||
148 | (current->coef) *= coef; | ||
149 | current = elements->next(); | ||
150 | } | ||
151 | } | ||
152 | |||
153 | /** | ||
154 | * Add THIS to ELS. THIS is not modified; ELS is. | ||
155 | */ | ||
156 | void ElementList::addTo(ElementList& els, double coef) { | ||
157 | ElementCoef* current = elements->first(); | ||
158 | while (current != 0) { | ||
159 | els.addElement(current->name, (current->coef) * coef); | ||
160 | current = elements->next(); | ||
161 | } | ||
162 | } | ||
163 | |||
164 | /** | ||
165 | * Add an element to THIS, with a coefficient COEF. If THIS already contains | ||
166 | * an element with the same name, adjust its coefficient only; if not, create | ||
167 | * a new ElementCoef pair and add to THIS. | ||
168 | */ | ||
169 | void ElementList::addElement(const QString& name, double coef) { | ||
170 | ElementCoef* current = elements->first(); | ||
171 | while (current != 0) { | ||
172 | if (current->name == name) { | ||
173 | current->coef += coef; | ||
174 | return; | ||
175 | } | ||
176 | current = elements->next(); | ||
177 | } | ||
178 | elements->append(new ElementCoef(name, coef)); | ||
179 | } | ||
180 | |||
181 | /** | ||
182 | * True iff THIS contains element named NAME. | ||
183 | */ | ||
184 | bool ElementList::contains(const QString& name) { | ||
185 | ElementCoef* current = elements->first(); | ||
186 | while (current != 0) { | ||
187 | if (current->name == name) | ||
188 | return true; | ||
189 | current = elements->next(); | ||
190 | } | ||
191 | return false; | ||
192 | } | ||
193 | |||
194 | bool ElementList::isEmpty() { | ||
195 | return elements->isEmpty(); | ||
196 | } | ||
197 | |||
198 | QString ElementList::getName() const { | ||
199 | return name; | ||
200 | } | ||
201 | |||
202 | /** | ||
203 | * A chemical element. | ||
204 | */ | ||
205 | Element::Element(const QString& n, double w) | ||
206 | : weight(w), name(n) { } | ||
207 | |||
208 | |||
209 | Element::~Element() { | ||
210 | } | ||
211 | |||
212 | |||
213 | /** | ||
214 | * Write THIS to LINE, in a format suitable for the element definition file. | ||
215 | */ | ||
216 | void Element::writeOut(QString& line) { | ||
217 | line.setNum(weight); | ||
218 | line = name + " " + line; | ||
219 | } | ||
220 | |||
221 | double Element::getWeight(QDict<SubUnit>* elstable) const { | ||
222 | return weight; | ||
223 | } | ||
224 | |||
225 | void Element::addTo(ElementList& els, double coef) { | ||
226 | els.addElement(name, coef); | ||
227 | } | ||
228 | |||
229 | QString Element::getName() const { | ||
230 | return name; | ||
231 | } | ||
232 | |||
233 | /** | ||
234 | * An element - coefficient pair. Used to represent elements within an | ||
235 | * element list. | ||
236 | */ | ||
237 | ElementCoef::ElementCoef(const QString& n, double c) : name(n), coef(c) {} | ||
238 | |||