-
Notifications
You must be signed in to change notification settings - Fork 0
/
elementschema.cpp
151 lines (144 loc) · 5.01 KB
/
elementschema.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
// Part of GTKPeriodicTable - Periodic Table of the Elements
// Copyright (C) 2018 Erich Küster
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// File: elementschema.cpp
// Created on 1. Oktober 2018, 19:28
*/
#include "elementschema.h"
ElementSchema::ElementSchema() {
for (auto element : elements) {
double mass;
stringstream massStream(element[3]);
massStream >> mass;
FormulaItem formulaItem = {element[1], mass, 0};
formulaTable[element[1]] = formulaItem;
}
}
ElementSchema::ElementSchema(const ElementSchema& orig) {
}
ElementSchema::~ElementSchema() {
}
std::vector<ElementSchema::FormulaItem> ElementSchema::parseElements(FormulaParser* inputParser) {
std::vector<ElementSchema::FormulaItem> outputElements;
auto closingBracket = inputParser->scanForOpeningBracket();
if (closingBracket == 'q') {
throw(2);
}
if (closingBracket != u'?') {
// found opening bracket
std::vector<ElementSchema::FormulaItem> bracketElements;
do {
// run through at least once
auto partialElements = parseElements(inputParser);
if (partialElements.size() == 0) break;
for (auto partial : partialElements) {
bracketElements.push_back(partial);
}
if (inputParser->endOfString) {
switch (closingBracket) {
// no closing bracket
case ')':
throw(6);
default:
throw(7);
}
break;
}
} while (!inputParser->isCharClosingBracket(closingBracket));
// scan for integer index, if no integer found, index is one
int index = inputParser->scanForIndex();
if (index < 0) {
// index 0 not allowed
//ErrorAlert(message: 8).show()
throw(8);
return {};
}
for (auto element : bracketElements) {
element.indexSum *= index;
outputElements.push_back(element);
}
} else {
string symbol = inputParser->scanForElement();
//found an element?
if (symbol.empty()) {
throw(2);
return {};
}
// now identify the element symbol
if (formulaTable.find(symbol) == formulaTable.end()) {
//no suitable element symbol found
throw(3);
return {};
}
// scan for integer index, if no integer found, index is one
int index = inputParser->scanForIndex();
if (index < 0) {
//index 0 not allowed
throw(8);
return {};
}
int indexsum = 1;
indexsum *= index;
FormulaItem formulaItem = {symbol, 0.0, indexsum};
outputElements.push_back(formulaItem);
}
// scan for bond sign, result is not evaluated at the moment
auto bondSign = inputParser->scanForBond();
if (!bondSign.empty()) {
//after bound must follow a formula element
if ((inputParser->followsClosingBracket()) || (inputParser->endOfString)) {
throw(5);
}
}
return outputElements;
}
string ElementSchema::analytics() {
stringstream analyticStream;
// use momentan valid locale
analyticStream.imbue(std::locale(""));
for ( auto const& pair : formulaTable ) {
auto symbol = pair.first;
FormulaItem formulaItem = formulaTable[symbol];
auto indexSum = formulaItem.indexSum;
if (indexSum > 0) {
double percentage = formulaItem.mass / totalMass * 100;
analyticStream << symbol << "\t"
<< setprecision(3) << fixed << percentage << " %" << endl;
}
}
return analyticStream.str();
}
string ElementSchema::finalFormula() {
stringstream formulaStream;
totalMass = 0.0;
for ( auto const& pair : formulaTable ) {
auto symbol = pair.first;
FormulaItem formulaItem = formulaTable[symbol];
auto indexSum = formulaItem.indexSum;
if (indexSum > 0) {
formulaStream << symbol;
double massMultiple = formulaItem.mass;
if (indexSum > 1) {
formulaStream << indexSum;
massMultiple *= indexSum;
}
formulaTable[symbol].mass = massMultiple;
totalMass += massMultiple;
}
}
return formulaStream.str();
}