fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. const std::vector<std::string> simple {
  5. "twelve", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine",
  6. "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen",
  7. "nineteen"
  8. };
  9.  
  10. const std::vector<std::string> units {
  11. "oh", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine",
  12. };
  13.  
  14. const std::vector<std::string> dozens {
  15. "", "ten", "twenty", "thirty", "fourty", "fifty"
  16. };
  17.  
  18. std::string to_hours(const std::string& input)
  19. {
  20. return simple[std::stoi(input) % 12];
  21. }
  22.  
  23. std::string to_minutes(const std::string& input)
  24. {
  25. size_t number = std::stoi(input);
  26. if(number == 0)
  27. return "";
  28. if(1 <= number && number < simple.size())
  29. return 1 <= number && number <= 9 ? "oh " + simple[number] : simple[number];
  30. std::string result = dozens[input[0] - '0'];
  31. if(input[1] != '0')
  32. result += " " + units[input[1] - '0'];
  33. return result;
  34. }
  35.  
  36. int main()
  37. {
  38. std::string line;
  39. while(getline(std::cin, line))
  40. {
  41. size_t colon = line.find(':');
  42. std::string hour = line.substr(0, colon);
  43. std::string minute = line.substr(colon + 1);
  44.  
  45. std::cout << "It's " << to_hours(hour) << ' ';
  46. std::string minutes = to_minutes(minute);
  47. if(minutes.length())
  48. std::cout << minutes << ' ';
  49. int number = std::stoi(hour);
  50. std::cout << (0 <= number && number <= 11 ? "am" : "pm") << std::endl;
  51. }
  52. }
Success #stdin #stdout 0s 15248KB
stdin
00:00
01:30
12:05
14:01
20:29
21:00
stdout
It's twelve am
It's one thirty am
It's twelve oh five pm
It's two oh one pm
It's eight twenty nine pm
It's nine pm