-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValuator.cpp
102 lines (86 loc) · 3.25 KB
/
Valuator.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
#include "Valuator.h"
#include <boost/property_tree/xml_parser.hpp>
namespace pt = boost::property_tree;
Addition* Valuator::createAddition(const boost::property_tree::ptree::value_type& v) {
Addition* a = new Addition;
for (const boost::property_tree::ptree::value_type &w : v.second.get_child("")) {
if (w.first == "item") {
Expression* e = createExpression(w);
a->addItem(e);
}
}
return a;
}
Division* Valuator::createDivision(const boost::property_tree::ptree::value_type& v) {
Division* d = new Division;
for (const boost::property_tree::ptree::value_type &w : v.second.get_child("")) {
if (w.first == "dividend") {
Expression* e = createExpression(w);
d->setDividend(e);
} else if (w.first == "divisor") {
Expression* e = createExpression(w);
d->setDivisor(e);
}
}
return d;
}
Multiplication* Valuator::createMultiplication(const boost::property_tree::ptree::value_type& v) {
Multiplication* m = new Multiplication;
for (const boost::property_tree::ptree::value_type &w : v.second.get_child("")) {
if (w.first == "factor") {
Expression* e = createExpression(w);
m->addFactor(e);
}
}
return m;
}
Subtraction* Valuator::createSubtraction(const boost::property_tree::ptree::value_type& v) {
Subtraction* s = new Subtraction;
for (const boost::property_tree::ptree::value_type &w : v.second.get_child("")) {
if (w.first == "minuend") {
Expression* e = createExpression(w);
s->setMinuend(e);
} else if (w.first == "subtrahend") {
Expression* e = createExpression(w);
s->setSubtrahend(e);
}
}
return s;
}
//! @brief: create the expression given an xml structure
Expression* Valuator::createExpression(const boost::property_tree::ptree::value_type& v) {
// if the field has no child, it contains only the constant
// for example <item>3</item>
if (v.second.empty()) {
return new Constant(std::stoi(v.second.data()));
}
// <addition>...</addition>
if (v.first == "addition") {
return createAddition(v);
} else if (v.first == "subtraction") {
return createSubtraction(v);
} else if (v.first == "multiplication") {
return createMultiplication(v);
} else if (v.first == "division") {
return createDivision(v);
}
// if the child is another expression
// for example <item><subtraction>...</subtraction></item>
auto subexp = v.second.get_child("").front();
return createExpression(subexp);
}
void Valuator::evaluate(const std::string& inputFileName, const std::string& outputFileName) {
pt::ptree inputTree;
pt::read_xml(inputFileName, inputTree);
pt::ptree outputTree;
for (pt::ptree::value_type &v : inputTree.get_child("expressions")) {
std::string id = v.second.get_child("<xmlattr>.id").data();
Expression* e = Valuator::createExpression(v);
if (e != nullptr) {
pt::ptree& result = outputTree.add("expressions.result", e->value());
result.put("<xmlattr>.id", id);
delete e;
}
}
pt::write_xml(outputFileName, outputTree);
}