Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <regex>
- #include <unordered_map>
- #include <functional>
- std::regex re1{"(\\w+)[ -]?(.+)?", std::regex::optimize} ,re2{" and"};
- long parse_int(std::string number) {
- number = std::regex_replace(number, re2, "");
- long res = 0, tmp = 0;
- std::unordered_map<std::string, std::function<void()>> dict{
- {"zero", [&]{}},
- {"one", [&]{ tmp += 1; }},
- {"two", [&]{ tmp += 2; }},
- {"three", [&]{ tmp += 3; }},
- {"four", [&]{ tmp += 4; }},
- {"five", [&]{ tmp += 5; }},
- {"six", [&]{ tmp += 6; }},
- {"seven", [&]{ tmp += 7; }},
- {"eight", [&]{ tmp += 8; }},
- {"nine", [&]{ tmp += 9; }},
- {"ten", [&]{ tmp += 10; }},
- {"eleven", [&]{ tmp += 11; }},
- {"twelve", [&]{ tmp += 12; }},
- {"thirteen", [&]{ tmp += 13; }},
- {"fourteen", [&]{ tmp += 14; }},
- {"fifteen", [&]{ tmp += 15; }},
- {"sixteen", [&]{ tmp += 16; }},
- {"seventeen", [&]{ tmp += 17; }},
- {"eighteen", [&]{ tmp += 18; }},
- {"nineteen", [&]{ tmp += 19; }},
- {"twenty", [&]{ tmp += 20; }},
- {"thirty", [&]{ tmp += 30; }},
- {"forty", [&]{ tmp += 40; }},
- {"fifty", [&]{ tmp += 50; }},
- {"sixty", [&]{ tmp += 60; }},
- {"seventy", [&]{ tmp += 70; }},
- {"eighty", [&]{ tmp += 80; }},
- {"ninety", [&]{ tmp += 90; }},
- {"hundred", [&]{ tmp *= 100; }},
- {"thousand", [&]{ res = tmp * 1000; tmp = 0; }},
- {"million", [&]{ tmp = 1000000; }}
- };
- for (std::smatch m; std::regex_match(number, m, re1); number = m[2])
- dict[m[1]]();
- return res + tmp;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement