fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. #include <regex.h>
  5.  
  6. const int MAX_MATCHES(20);
  7.  
  8. void elements(const std::string &input)
  9. {
  10. regex_t ElementRegEx;
  11. regcomp(&ElementRegEx, "([0-9]+)", REG_EXTENDED);
  12.  
  13. std::string Elements(input);
  14.  
  15. regmatch_t ElementMatches[MAX_MATCHES];
  16. while (!regexec(&ElementRegEx, Elements.c_str(), MAX_MATCHES, ElementMatches, 0))
  17. {
  18. const regmatch_t &Match = ElementMatches[0];
  19.  
  20. if ((Match.rm_so != -1) && (Match.rm_eo != -1))
  21. {
  22. const std::string CurrentElement(Elements.substr(Match.rm_so, Match.rm_eo - Match.rm_so));
  23. std::cout << '\t' << CurrentElement << '\n';
  24.  
  25. if (Elements.size() > static_cast<size_t>(Match.rm_eo))
  26. Elements = Elements.substr(Match.rm_eo);
  27. else
  28. Elements = "";
  29. }
  30. }
  31. }
  32.  
  33. void cycles(const std::string &input)
  34. {
  35. regex_t CycleRegEx;
  36. regcomp(&CycleRegEx, "([0-9]+[ ]*,[ ]*)*[0-9]+", REG_EXTENDED);
  37.  
  38. std::string Cycles(input);
  39.  
  40. regmatch_t CycleMatches[MAX_MATCHES];
  41. while (!regexec(&CycleRegEx, Cycles.c_str(), MAX_MATCHES, CycleMatches, 0))
  42. {
  43. const regmatch_t &Match = CycleMatches[0];
  44.  
  45. if ((Match.rm_so != -1) && (Match.rm_eo != -1))
  46. {
  47. const std::string CurrentCycle(Cycles.substr(Match.rm_so, Match.rm_eo - Match.rm_so));
  48. std::cout << CurrentCycle << '\n';
  49. elements(CurrentCycle);
  50.  
  51. if (Cycles.size() > static_cast<size_t>(Match.rm_eo))
  52. Cycles = Cycles.substr(Match.rm_eo + 1);
  53. else
  54. Cycles = "";
  55. }
  56. }
  57.  
  58. regfree(&CycleRegEx);
  59. }
  60.  
  61. int main(int argc, char **argv)
  62. {
  63. std::string input("(1,2,5)(3,4)");
  64.  
  65. std::cout << "input: " << input << "\n\n";
  66.  
  67. cycles(input);
  68. return 0;
  69. }
Success #stdin #stdout 0.01s 2860KB
stdin
Standard input is empty
stdout
input: (1,2,5)(3,4)

1,2,5
	1
	2
	5
3,4
	3
	4