Advertisement
kompilainenn

Untitled

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