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. regmatch_t ElementMatches[MAX_MATCHES];
  14. if (!regexec(&ElementRegEx, input.c_str(), MAX_MATCHES, ElementMatches, 0))
  15. {
  16. int Element = 0;
  17.  
  18. while ((ElementMatches[Element].rm_so != -1) && (ElementMatches[Element].rm_eo != -1))
  19. {
  20. regmatch_t &ElementMatch = ElementMatches[Element];
  21. std::string CurrentElement(input.substr(ElementMatch.rm_so, ElementMatch.rm_eo - ElementMatch.rm_so));
  22. std::cout << '\t' << CurrentElement << '\n';
  23.  
  24. ++Element;
  25. }
  26. }
  27. }
  28.  
  29. void cycles(const std::string &input)
  30. {
  31. regex_t CycleRegEx;
  32. regcomp(&CycleRegEx, "([0-9]+[ ]*,[ ]*)*[0-9]+", REG_EXTENDED);
  33.  
  34. regmatch_t CycleMatches[MAX_MATCHES];
  35. if (!regexec(&CycleRegEx, input.c_str(), MAX_MATCHES, CycleMatches, 0))
  36. {
  37. int Cycle = 0;
  38.  
  39. while ((CycleMatches[Cycle].rm_so != -1) && (CycleMatches[Cycle].rm_eo != -1))
  40. {
  41. regmatch_t &CycleMatch = CycleMatches[Cycle];
  42. const std::string CurrentCycle(input.substr(CycleMatch.rm_so, CycleMatch.rm_eo - CycleMatch.rm_so));
  43. std::cout << CurrentCycle << '\n';
  44.  
  45. elements(CurrentCycle);
  46. ++Cycle;
  47. }
  48. }
  49.  
  50. regfree(&CycleRegEx);
  51. }
  52.  
  53. int main(int argc, char **argv)
  54. {
  55. std::string input("(1,2,5)(3,4)");
  56.  
  57. std::cout << "input: " << input << "\n\n";
  58.  
  59. cycles(input);
  60. return 0;
  61. }
  62.  
Success #stdin #stdout 0.02s 2812KB
stdin
Standard input is empty
stdout
input: (1,2,5)(3,4)

1,2,5
	1
	1
2,
	2
	2