fork download
  1. #include <cassert>
  2. #include <iostream>
  3. #include <sstream>
  4. #include <string>
  5.  
  6. void New(int delay, std::string message)
  7. {
  8. std::cout << "TODO: New" << std::endl;
  9. }
  10.  
  11. void List()
  12. {
  13. std::cout << "TODO: List" << std::endl;
  14. }
  15.  
  16. void Cancel(int index)
  17. {
  18. std::cout << "TODO: Cancel" << std::endl;
  19. }
  20.  
  21. void Exit()
  22. {
  23. std::cout << "TODO: Exit" << std::endl;
  24. }
  25.  
  26. enum class CommandType
  27. {
  28. None,
  29. New,
  30. List,
  31. Cancel,
  32. Exit
  33. };
  34.  
  35. struct Command
  36. {
  37. CommandType type{};
  38. int delay{};
  39. std::string message;
  40. int index{};
  41.  
  42. Command() = default;
  43. Command(CommandType type) : type(type) {}
  44. Command(CommandType type, int delay, const std::string& message) : type(type), delay(delay), message(message) {}
  45. Command(CommandType type, int index) : type(type), index(index) {}
  46. };
  47.  
  48. Command ReadCommand()
  49. {
  50. std::string input;
  51. std::getline(std::cin, input);
  52.  
  53. std::stringstream parser(input);
  54. std::string command;
  55. if (parser >> command)
  56. {
  57. if (command == "exit")
  58. return Command(CommandType::Exit);
  59. else if (command == "list")
  60. return Command(CommandType::List);
  61. else if (command == "cancel")
  62. {
  63. int index = 0;
  64. if (parser >> index)
  65. return Command(CommandType::Cancel, index);
  66. else
  67. {
  68. std::cerr << "Usage: cancel index" << std::endl
  69. << " index : index of the item to remove" << std::endl;
  70. }
  71. }
  72. else if (command == "new")
  73. {
  74. int delay = 0;
  75. if (parser >> delay)
  76. {
  77. if (delay > 0)
  78. {
  79. std::string message;
  80. std::getline(parser, message);
  81. if (!message.empty())
  82. message = message.substr(1);
  83. return Command(CommandType::New, delay, message);
  84. }
  85. else
  86. std::cerr << "Delay must be positive" << std::endl;
  87. }
  88. else
  89. {
  90. std::cerr << "Usage: new delay message" << std::endl
  91. << " delay : positive delay in seconds" << std::endl
  92. << " message : message to show without quotes" << std::endl;
  93. }
  94. }
  95. else
  96. {
  97. std::cerr << "Unknown command" << std::endl;
  98. }
  99. }
  100.  
  101. return Command(CommandType::None);
  102. }
  103.  
  104. int main()
  105. {
  106. std::cout
  107. << "Commands:" << std::endl
  108. << " new <delay> <message>" << std::endl
  109. << " Schedule a notification with given message and delay in seconds" << std::endl
  110. << " delay : positive delay in seconds" << std::endl
  111. << " message : message to show without quotes" << std::endl
  112. << " list" << std::endl
  113. << " Show the list of active notifications" << std::endl
  114. << " cancel <index>" << std::endl
  115. << " Cancel active notification with given index" << std::endl
  116. << " index : index in the previously shown list" << std::endl
  117. << " exit" << std::endl
  118. << " Exit application" << std::endl;
  119.  
  120. while (true)
  121. {
  122. std::cout << std::endl << "> ";
  123. const auto command = ReadCommand();
  124. switch (command.type)
  125. {
  126. case CommandType::None:
  127. continue;
  128. case CommandType::Exit:
  129. Exit();
  130. return 0;
  131. case CommandType::New:
  132. New(command.delay, command.message);
  133. break;
  134. case CommandType::List:
  135. List();
  136. break;
  137. case CommandType::Cancel:
  138. Cancel(command.index);
  139. break;
  140. default:
  141. assert(0);
  142. }
  143. }
  144. return 0;
  145. }
  146.  
Success #stdin #stdout 0s 15240KB
stdin
new 60 Message1
new 40 Message2
new 20 Message3
list
cancel 2
exit
stdout
Commands:
   new <delay> <message>
      Schedule a notification with given message and delay in seconds
      delay   : positive delay in seconds
      message : message to show without quotes
   list
      Show the list of active notifications
   cancel <index>
      Cancel active notification with given index
      index   : index in the previously shown list
   exit
      Exit application

> TODO: New

> TODO: New

> TODO: New

> TODO: List

> TODO: Cancel

> TODO: Exit