author | zecke <zecke> | 2003-12-09 20:58:41 (UTC) |
---|---|---|
committer | zecke <zecke> | 2003-12-09 20:58:41 (UTC) |
commit | afa2a1983fba363bc1d4f692fefc7fc26965de8d (patch) (unidiff) | |
tree | 89f148a1417e6cdc0dac8e28cbadf187338d6d6f | |
parent | 31ba7b6bf273889f9486bb2df44d9a8a5381f579 (diff) | |
download | opie-afa2a1983fba363bc1d4f692fefc7fc26965de8d.zip opie-afa2a1983fba363bc1d4f692fefc7fc26965de8d.tar.gz opie-afa2a1983fba363bc1d4f692fefc7fc26965de8d.tar.bz2 |
Do not include stl if you don't use it
-rw-r--r-- | noncore/apps/oxygen/kmolcalc.cpp | 1 |
1 files changed, 0 insertions, 1 deletions
diff --git a/noncore/apps/oxygen/kmolcalc.cpp b/noncore/apps/oxygen/kmolcalc.cpp index 1d41b0f..7a47942 100644 --- a/noncore/apps/oxygen/kmolcalc.cpp +++ b/noncore/apps/oxygen/kmolcalc.cpp | |||
@@ -1,200 +1,199 @@ | |||
1 | /* | 1 | /* |
2 | * kmolcalc.cpp | 2 | * kmolcalc.cpp |
3 | * | 3 | * |
4 | * Copyright (C) 2000,2001 Tomislav Gountchev <tomi@idiom.com> | 4 | * Copyright (C) 2000,2001 Tomislav Gountchev <tomi@idiom.com> |
5 | * Copyright (C) 2002 Carsten Niehaus <cniehaus@handhelds.org> | 5 | * Copyright (C) 2002 Carsten Niehaus <cniehaus@handhelds.org> |
6 | */ | 6 | */ |
7 | 7 | ||
8 | /** | 8 | /** |
9 | * KMOLCALC is the calculation engine. It knows about a hashtable of user defined atomic | 9 | * KMOLCALC is the calculation engine. It knows about a hashtable of user defined atomic |
10 | * weights and group definitions ELSTABLE, and the currently processed formula, stored | 10 | * weights and group definitions ELSTABLE, and the currently processed formula, stored |
11 | * as a list of elements and their coefficients, ELEMENTS. | 11 | * as a list of elements and their coefficients, ELEMENTS. |
12 | */ | 12 | */ |
13 | 13 | ||
14 | #include "kmolcalc.h" | 14 | #include "kmolcalc.h" |
15 | #include <qdict.h> | 15 | #include <qdict.h> |
16 | #include <qdir.h> | 16 | #include <qdir.h> |
17 | #include <qfile.h> | 17 | #include <qfile.h> |
18 | #include <qpe/qpeapplication.h> | 18 | #include <qpe/qpeapplication.h> |
19 | #include <iostream> | ||
20 | 19 | ||
21 | 20 | ||
22 | /** | 21 | /** |
23 | * Construct a new calculator object. | 22 | * Construct a new calculator object. |
24 | */ | 23 | */ |
25 | KMolCalc::KMolCalc() { | 24 | KMolCalc::KMolCalc() { |
26 | elements = new ElementList; | 25 | elements = new ElementList; |
27 | elstable = NULL; | 26 | elstable = NULL; |
28 | readElstable(); | 27 | readElstable(); |
29 | } | 28 | } |
30 | 29 | ||
31 | KMolCalc::~KMolCalc() { | 30 | KMolCalc::~KMolCalc() { |
32 | delete elements; | 31 | delete elements; |
33 | } | 32 | } |
34 | 33 | ||
35 | void KMolCalc::readElstable() { | 34 | void KMolCalc::readElstable() { |
36 | weight = -1; // not calculated yet | 35 | weight = -1; // not calculated yet |
37 | if (elstable) delete elstable; | 36 | if (elstable) delete elstable; |
38 | elstable = new QDict<SubUnit> (197, TRUE); | 37 | elstable = new QDict<SubUnit> (197, TRUE); |
39 | elstable->setAutoDelete(TRUE); | 38 | elstable->setAutoDelete(TRUE); |
40 | mwfile = QPEApplication::qpeDir() +"share/oxygen/kmolweights"; | 39 | mwfile = QPEApplication::qpeDir() +"share/oxygen/kmolweights"; |
41 | QFile f(mwfile); | 40 | QFile f(mwfile); |
42 | if (f.exists()) readMwfile(f); | 41 | if (f.exists()) readMwfile(f); |
43 | } | 42 | } |
44 | 43 | ||
45 | 44 | ||
46 | /** | 45 | /** |
47 | * Parse a string S and construct the ElementList this->ELEMENTS, representing the | 46 | * Parse a string S and construct the ElementList this->ELEMENTS, representing the |
48 | * composition of S. Returns 0 if successful, or an error code (currently -1) if | 47 | * composition of S. Returns 0 if successful, or an error code (currently -1) if |
49 | * parsing failed. | 48 | * parsing failed. |
50 | * The elements is S must be valid element or group symbols, as stored in this->ELSTABLE. | 49 | * The elements is S must be valid element or group symbols, as stored in this->ELSTABLE. |
51 | * See help files for correct formula syntax. | 50 | * See help files for correct formula syntax. |
52 | */ | 51 | */ |
53 | QString KMolCalc::readFormula(const QString& s) { | 52 | QString KMolCalc::readFormula(const QString& s) { |
54 | weight = -1; | 53 | weight = -1; |
55 | if (elements) delete elements; | 54 | if (elements) delete elements; |
56 | elements = new ElementList; | 55 | elements = new ElementList; |
57 | return KMolCalc::readGroup(s, elements); | 56 | return KMolCalc::readGroup(s, elements); |
58 | } | 57 | } |
59 | 58 | ||
60 | // read a formula group recursively. Called by readFormula. | 59 | // read a formula group recursively. Called by readFormula. |
61 | QString KMolCalc::readGroup(const QString& s, ElementList* els) { | 60 | QString KMolCalc::readGroup(const QString& s, ElementList* els) { |
62 | if (s.isEmpty()) return QString ("Enter a formula."); //ERROR | 61 | if (s.isEmpty()) return QString ("Enter a formula."); //ERROR |
63 | int sl = s.length(); | 62 | int sl = s.length(); |
64 | int i = 0; | 63 | int i = 0; |
65 | QString errors ("OK"); | 64 | QString errors ("OK"); |
66 | bool ok = TRUE; | 65 | bool ok = TRUE; |
67 | while (i < sl && ((s[i] <= '9' && s[i] >= '0') || s[i] == '.')) i++; | 66 | while (i < sl && ((s[i] <= '9' && s[i] >= '0') || s[i] == '.')) i++; |
68 | double prefix = (i == 0 ? 1 : s.left(i).toDouble(&ok)); | 67 | double prefix = (i == 0 ? 1 : s.left(i).toDouble(&ok)); |
69 | if (! ok || i == sl || prefix == 0) return QString ("Bad formula."); // ERROR | 68 | if (! ok || i == sl || prefix == 0) return QString ("Bad formula."); // ERROR |
70 | ElementList* elstemp = new ElementList; | 69 | ElementList* elstemp = new ElementList; |
71 | while (i < sl) { | 70 | while (i < sl) { |
72 | int j = i; | 71 | int j = i; |
73 | if (s[i] == '(') { | 72 | if (s[i] == '(') { |
74 | ElementList* inner = new ElementList; | 73 | ElementList* inner = new ElementList; |
75 | int level = 1; // count levels of nested ( ). | 74 | int level = 1; // count levels of nested ( ). |
76 | while (1) { | 75 | while (1) { |
77 | if (i++ == sl) { | 76 | if (i++ == sl) { |
78 | delete inner; | 77 | delete inner; |
79 | delete elstemp; | 78 | delete elstemp; |
80 | return QString ("Bad formula."); //ERROR | 79 | return QString ("Bad formula."); //ERROR |
81 | } | 80 | } |
82 | if (s[i] == '(') level++; | 81 | if (s[i] == '(') level++; |
83 | if (s[i] == ')') level--; | 82 | if (s[i] == ')') level--; |
84 | if (level == 0) break; | 83 | if (level == 0) break; |
85 | } | 84 | } |
86 | errors = KMolCalc::readGroup(s.mid(j+1, i-j-1), inner); | 85 | errors = KMolCalc::readGroup(s.mid(j+1, i-j-1), inner); |
87 | j = ++i; | 86 | j = ++i; |
88 | while (i < sl && ((s[i] <= '9' && s[i] >= '0') || s[i] == '.')) i++; | 87 | while (i < sl && ((s[i] <= '9' && s[i] >= '0') || s[i] == '.')) i++; |
89 | double suffix = (i == j ? 1 : s.mid(j, i-j).toDouble(&ok)); | 88 | double suffix = (i == j ? 1 : s.mid(j, i-j).toDouble(&ok)); |
90 | if (! ok || suffix == 0) { | 89 | if (! ok || suffix == 0) { |
91 | delete inner; | 90 | delete inner; |
92 | delete elstemp; | 91 | delete elstemp; |
93 | return QString ("Bad formula."); // ERROR | 92 | return QString ("Bad formula."); // ERROR |
94 | } | 93 | } |
95 | inner->addTo(*elstemp, suffix); | 94 | inner->addTo(*elstemp, suffix); |
96 | delete inner; | 95 | delete inner; |
97 | inner = NULL; | 96 | inner = NULL; |
98 | } else if ((s[i] >= 'A' && s[i] <= 'Z') || (s[i] >= 'a' && s[i] <= 'z')) { | 97 | } else if ((s[i] >= 'A' && s[i] <= 'Z') || (s[i] >= 'a' && s[i] <= 'z')) { |
99 | while (++i < sl && ((s[i] >= 'a' && s[i] <= 'z') || s[i] == '*' || | 98 | while (++i < sl && ((s[i] >= 'a' && s[i] <= 'z') || s[i] == '*' || |
100 | s[i] == '\'')); | 99 | s[i] == '\'')); |
101 | QString elname = s.mid(j, i-j); | 100 | QString elname = s.mid(j, i-j); |
102 | j = i; | 101 | j = i; |
103 | while (i < sl && ((s[i] <= '9' && s[i] >= '0') || s[i] == '.')) i++; | 102 | while (i < sl && ((s[i] <= '9' && s[i] >= '0') || s[i] == '.')) i++; |
104 | double suffix = (i == j ? 1 : s.mid(j, i-j).toDouble(&ok)); | 103 | double suffix = (i == j ? 1 : s.mid(j, i-j).toDouble(&ok)); |
105 | if (! ok || suffix == 0) { | 104 | if (! ok || suffix == 0) { |
106 | delete elstemp; | 105 | delete elstemp; |
107 | return QString ("Bad formula."); // ERROR | 106 | return QString ("Bad formula."); // ERROR |
108 | } | 107 | } |
109 | SubUnit* group = elstable->find(elname); | 108 | SubUnit* group = elstable->find(elname); |
110 | if (group == 0) { | 109 | if (group == 0) { |
111 | delete elstemp; | 110 | delete elstemp; |
112 | return QString ("Undefined symbol: ") + elname; //ERROR | 111 | return QString ("Undefined symbol: ") + elname; //ERROR |
113 | } | 112 | } |
114 | group->addTo(*elstemp, suffix); | 113 | group->addTo(*elstemp, suffix); |
115 | } else if (s[i] == '+') { | 114 | } else if (s[i] == '+') { |
116 | if (elstemp->isEmpty()) { | 115 | if (elstemp->isEmpty()) { |
117 | delete elstemp; | 116 | delete elstemp; |
118 | return QString ("Bad formula."); //ERROR | 117 | return QString ("Bad formula."); //ERROR |
119 | } | 118 | } |
120 | elstemp->addTo(*els, prefix); | 119 | elstemp->addTo(*els, prefix); |
121 | delete elstemp; | 120 | delete elstemp; |
122 | errors = KMolCalc::readGroup(s.mid(i+1, sl-i-1), els); | 121 | errors = KMolCalc::readGroup(s.mid(i+1, sl-i-1), els); |
123 | return errors; | 122 | return errors; |
124 | } else { | 123 | } else { |
125 | delete elstemp; | 124 | delete elstemp; |
126 | return QString ("Bad formula."); //ERROR | 125 | return QString ("Bad formula."); //ERROR |
127 | } | 126 | } |
128 | } | 127 | } |
129 | elstemp->addTo(*els, prefix); | 128 | elstemp->addTo(*els, prefix); |
130 | delete elstemp; | 129 | delete elstemp; |
131 | return errors; | 130 | return errors; |
132 | } | 131 | } |
133 | 132 | ||
134 | /** | 133 | /** |
135 | * Calculate and return the molecular weight of the current chemical formula. | 134 | * Calculate and return the molecular weight of the current chemical formula. |
136 | */ | 135 | */ |
137 | double KMolCalc::getWeight() { | 136 | double KMolCalc::getWeight() { |
138 | if (weight == -1) weight = elements->getWeight(elstable); | 137 | if (weight == -1) weight = elements->getWeight(elstable); |
139 | return weight; | 138 | return weight; |
140 | } | 139 | } |
141 | 140 | ||
142 | /** | 141 | /** |
143 | * Return the elemental composition of the current formula, as a string of tab-separated | 142 | * Return the elemental composition of the current formula, as a string of tab-separated |
144 | * element - percentage pairs, separated by newlines. | 143 | * element - percentage pairs, separated by newlines. |
145 | */ | 144 | */ |
146 | QString KMolCalc::getEA() { | 145 | QString KMolCalc::getEA() { |
147 | if (weight == -1) weight = elements->getWeight(elstable); | 146 | if (weight == -1) weight = elements->getWeight(elstable); |
148 | if (weight == -1) return QString("ERROR: Couldn't get Mw..."); // ERROR | 147 | if (weight == -1) return QString("ERROR: Couldn't get Mw..."); // ERROR |
149 | return elements->getEA(elstable, weight); | 148 | return elements->getEA(elstable, weight); |
150 | } | 149 | } |
151 | 150 | ||
152 | /** | 151 | /** |
153 | * Return the empirical formula of the current compound as a QString. | 152 | * Return the empirical formula of the current compound as a QString. |
154 | */ | 153 | */ |
155 | QString KMolCalc::getEmpFormula() { | 154 | QString KMolCalc::getEmpFormula() { |
156 | return elements->getEmpFormula(); | 155 | return elements->getEmpFormula(); |
157 | } | 156 | } |
158 | 157 | ||
159 | // Read the element definition file. | 158 | // Read the element definition file. |
160 | void KMolCalc::readMwfile(QFile& f) { | 159 | void KMolCalc::readMwfile(QFile& f) { |
161 | if (! f.open(IO_ReadOnly)) return; //ERROR | 160 | if (! f.open(IO_ReadOnly)) return; //ERROR |
162 | QTextStream fs (&f); | 161 | QTextStream fs (&f); |
163 | QString line; | 162 | QString line; |
164 | while (! fs.eof()) { | 163 | while (! fs.eof()) { |
165 | line = fs.readLine(); | 164 | line = fs.readLine(); |
166 | SubUnit* s = SubUnit::makeSubUnit(line); | 165 | SubUnit* s = SubUnit::makeSubUnit(line); |
167 | elstable->replace(s->getName(), s); | 166 | elstable->replace(s->getName(), s); |
168 | } | 167 | } |
169 | f.close(); | 168 | f.close(); |
170 | } | 169 | } |
171 | 170 | ||
172 | /** | 171 | /** |
173 | * Remove a group or element definition from ELSTABLE. | 172 | * Remove a group or element definition from ELSTABLE. |
174 | */ | 173 | */ |
175 | void KMolCalc::undefineGroup (const QString& name) { | 174 | void KMolCalc::undefineGroup (const QString& name) { |
176 | elstable->remove (name); | 175 | elstable->remove (name); |
177 | } | 176 | } |
178 | 177 | ||
179 | /** | 178 | /** |
180 | * Add a new element name - atomic weight record to the ELSTABLE hashtable. Assumes | 179 | * Add a new element name - atomic weight record to the ELSTABLE hashtable. Assumes |
181 | * NAME has valid syntax. | 180 | * NAME has valid syntax. |
182 | 181 | ||
183 | */ | 182 | */ |
184 | void KMolCalc::defineElement (const QString& name, double weight) { | 183 | void KMolCalc::defineElement (const QString& name, double weight) { |
185 | Element* el = new Element(name, weight); | 184 | Element* el = new Element(name, weight); |
186 | elstable->replace(name, el); | 185 | elstable->replace(name, el); |
187 | } | 186 | } |
188 | 187 | ||
189 | /** | 188 | /** |
190 | * Add a new group definition to the ELSTABLE. Returns 0 if OK, -1 if parsing FORMULA | 189 | * Add a new group definition to the ELSTABLE. Returns 0 if OK, -1 if parsing FORMULA |
191 | * fails. Assumes the syntax of grpname is correct. | 190 | * fails. Assumes the syntax of grpname is correct. |
192 | */ | 191 | */ |
193 | QString KMolCalc::defineGroup (const QString& grpname, const QString& formula) { | 192 | QString KMolCalc::defineGroup (const QString& grpname, const QString& formula) { |
194 | ElementList* els = new ElementList(grpname); | 193 | ElementList* els = new ElementList(grpname); |
195 | QString error = readGroup(formula, els); | 194 | QString error = readGroup(formula, els); |
196 | if (error != "OK") return error; | 195 | if (error != "OK") return error; |
197 | if (els->contains(grpname)) return QString("Can't define a group recursively!\n"); | 196 | if (els->contains(grpname)) return QString("Can't define a group recursively!\n"); |
198 | elstable->replace(grpname, els); | 197 | elstable->replace(grpname, els); |
199 | return QString("OK"); | 198 | return QString("OK"); |
200 | } | 199 | } |