Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- #include <map>
- #include <iostream>
- #include <cstring>
- #include <vector>
- long parse_int(std::string number)
- {
- //создадим словарь слово-число
- std::map<std::string, int> dictionary =
- {
- {"zero", 0},
- {"one", 1},
- {"two", 2},
- {"three", 3},
- {"four", 4},
- {"five", 5},
- {"six", 6},
- {"seven", 7},
- {"eight", 8},
- {"nine", 9},
- {"ten", 10},
- {"eleven", 11},
- {"twelve", 12},
- {"thirteen", 13},
- {"fourteen", 14},
- {"fifteen", 15},
- {"sixteen", 16},
- {"seventeen", 17},
- {"eighteen", 18},
- {"nineteen", 19},
- {"twenty", 20},
- {"thirty", 30},
- {"forty", 40},
- {"fifty", 50},
- {"sixty", 60},
- {"seventy", 70},
- {"eighty", 80},
- {"ninety", 90}
- };
- std::vector<std::string> temp_vec_str;
- std::string temp_str = "";
- //считываем строку и записываем слова в вектор
- for ( auto i: number)
- if (islower(i))
- temp_str.push_back(i);
- else
- {
- temp_vec_str.push_back(temp_str);
- temp_str = "";
- }
- //записываем в вектор последнее слово в строке
- if (!temp_str.empty())
- temp_vec_str.push_back(temp_str);
- long sum = 0;
- long sum2 = 0;
- int thousand_count = 0;
- for (int i = 0; i < temp_vec_str.size(); ++i)
- {
- if (thousand_count == 0 && temp_vec_str[i] != "hundred")
- {
- sum += dictionary[temp_vec_str[i]];
- };
- if (temp_vec_str[i] == "hundred" && thousand_count == 0)
- {
- sum *= 100;
- };
- if (temp_vec_str[i] == "thousand")
- {
- sum *= 1000;
- thousand_count++;
- };
- if (thousand_count == 1 && temp_vec_str[i] != "hundred")
- {
- sum2 += dictionary[temp_vec_str[i]];
- };
- if (temp_vec_str[i] == "hundred" && thousand_count == 1)
- {
- sum2 *= 100;
- };
- }
- return sum+sum2;
- }
- int main()
- {
- std::string number = "seven hundred ninety-three thousand one hundred seven";
- std::cout << "The result is: " << parse_int(number) << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement