-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathelements.cpp
68 lines (48 loc) · 1.73 KB
/
elements.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
// Copyright (C) 2017 gahag
// All rights reserved.
//
// This software may be modified and distributed under the terms
// of the BSD license. See the LICENSE file for details.
#include <cctype>
#include <iostream>
#include <sstream>
#include <string>
#include <tpc/parser/combinators/fold.hpp>
#include <tpc/parser/combinators/join.hpp>
#include <tpc/parser/combinators/maybe.hpp>
#include <tpc/parser/standard/identifier.hpp>
#include <tpc/parser/standard/number.hpp>
namespace elements {
namespace util {
bool islower(char c) { return std::islower(c); }
bool isupper(char c) { return std::isupper(c); }
float sum(float x, float y) { return x + y; }
}
float weight(const std::string& element, const unsigned& count) {
if (element == "O") return 15.9994 * count;
if (element == "H") return 1.00794 * count;
if (element == "Na") return 22.9897 * count;
if (element == "Cl") return 35.4527 * count;
if (element == "C") return 12.0107 * count;
if (element == "S") return 32.0655 * count;
return -1;
}
constexpr tpc::parser<std::string> element = tpc::identifier<util::isupper, util::islower>;
constexpr tpc::parser<unsigned> count = tpc::number<unsigned>;
constexpr tpc::parser<float> formula = tpc::fold1<
float, tpc::join< std::string, element,
unsigned, tpc::maybe<unsigned, 1, count>,
float, weight >,
util::sum
>;
}
auto main() -> int {
std::string str;
std::getline(std::cin, str);
std::stringstream stream(str);
auto r = elements::formula(stream);
if (r)
std::cout << "result: " << *r << std::endl;
else
std::cout << "failed!" << std::endl;
}