Advertisement
kompilainenn

Untitled

Jul 9th, 2022
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.30 KB | None | 0 0
  1. #include <string>
  2. #include <map>
  3. #include <iostream>
  4. #include <cstring>
  5. #include <vector>
  6.  
  7. long parse_int(std::string number)
  8. {
  9.     //создадим словарь слово-число
  10.     std::map<std::string, int> dictionary =
  11.   {
  12.    {"zero", 0},
  13.    {"one", 1},
  14.    {"two", 2},
  15.    {"three", 3},
  16.    {"four", 4},
  17.    {"five", 5},
  18.    {"six", 6},
  19.    {"seven", 7},
  20.    {"eight", 8},
  21.    {"nine", 9},
  22.    {"ten", 10},
  23.    {"eleven", 11},
  24.    {"twelve", 12},
  25.    {"thirteen", 13},
  26.    {"fourteen", 14},
  27.    {"fifteen", 15},
  28.    {"sixteen", 16},
  29.    {"seventeen", 17},
  30.    {"eighteen", 18},
  31.    {"nineteen", 19},
  32.    {"twenty", 20},
  33.    {"thirty", 30},
  34.    {"forty", 40},
  35.    {"fifty", 50},
  36.    {"sixty", 60},
  37.    {"seventy", 70},
  38.    {"eighty", 80},
  39.    {"ninety", 90}
  40.    };
  41.  
  42.     std::vector<std::string> temp_vec_str;
  43.     std::string temp_str = "";
  44.    
  45.     //считываем строку и записываем слова в вектор
  46.     for ( auto i: number)
  47.         if (islower(i))
  48.             temp_str.push_back(i);
  49.         else
  50.         {
  51.             temp_vec_str.push_back(temp_str);
  52.             temp_str = "";
  53.         }
  54.     //записываем в вектор последнее слово в строке
  55.     if (!temp_str.empty())
  56.         temp_vec_str.push_back(temp_str);
  57.    
  58.     long sum = 0;
  59.     long sum2 = 0;
  60.     int thousand_count = 0;
  61.     for (int i = 0; i < temp_vec_str.size(); ++i)
  62.     {
  63.         if (thousand_count == 0 && temp_vec_str[i] != "hundred")
  64.         {
  65.             sum += dictionary[temp_vec_str[i]];
  66.         };
  67.        
  68.         if (temp_vec_str[i] == "hundred" && thousand_count == 0)
  69.         {
  70.             sum *= 100;
  71.         };
  72.        
  73.         if (temp_vec_str[i] == "thousand")
  74.         {
  75.             sum *= 1000;
  76.             thousand_count++;
  77.         };
  78.        
  79.         if (thousand_count == 1 && temp_vec_str[i] != "hundred")
  80.         {
  81.             sum2 += dictionary[temp_vec_str[i]];
  82.         };
  83.        
  84.         if (temp_vec_str[i] == "hundred" && thousand_count == 1)
  85.         {
  86.             sum2 *= 100;
  87.         };
  88.     }
  89.  
  90.     return sum+sum2;
  91.   }
  92.  
  93. int main()
  94. {
  95.     std::string number = "seven hundred ninety-three thousand one hundred seven";
  96.     std::cout << "The result is: " << parse_int(number) << std::endl;
  97.  
  98.   return 0;
  99. }
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement