fork download
  1. #include <algorithm>
  2. #include <functional>
  3. #include <iostream>
  4. #include <iterator>
  5. #include <list>
  6. #include <regex>
  7. #include <sstream>
  8. #include <string>
  9. using namespace std;
  10.  
  11. typedef char Task;
  12.  
  13. struct Tache {
  14. char step;
  15. int duration;
  16. list<Task> precedentTask;
  17. };
  18.  
  19. istream& operator>>(istream& lhs, Tache& rhs) {
  20. string line;
  21.  
  22. getline(lhs, line, '\n');
  23.  
  24. stringstream ss(line);
  25.  
  26. ss >> rhs.step;
  27. ss.ignore(numeric_limits<streamsize>::max(), '(');
  28. ss >> rhs.duration;
  29. ss.ignore(numeric_limits<streamsize>::max(), ')');
  30.  
  31. const regex re("\\s*,\\s*([a-zA-Z])");
  32. string precedentTasks;
  33.  
  34. getline(ss, precedentTasks);
  35.  
  36. rhs.precedentTask.clear();
  37.  
  38. transform(sregex_token_iterator(cbegin(precedentTasks), cend(precedentTasks), re, 1), sregex_token_iterator(), back_insert_iterator<list<Task>>(rhs.precedentTask), [](const string& i) {
  39. return i.front();
  40. });
  41.  
  42. return lhs;
  43. }
  44.  
  45. int main() {
  46. stringstream seq("A(3)\nB(4),A\nC(2),A\nE(5),A\nG(3),A\nJ(8),B,H\nH(7),C,E,G\nI(6),G\nF(5),H");
  47.  
  48. list<Tache> allTaches{ istream_iterator<Tache>(seq), istream_iterator<Tache>() };
  49.  
  50. for (const auto& i : allTaches) {
  51. cout << i.step << ' ' << i.duration << ' ';
  52. copy(cbegin(i.precedentTask), cend(i.precedentTask), ostream_iterator<Task>(cout, " "));
  53. cout << endl;
  54. }
  55. }
Success #stdin #stdout 0.01s 3556KB
stdin
Standard input is empty
stdout
A 3 
B 4 A 
C 2 A 
E 5 A 
G 3 A 
J 8 B H 
H 7 C E G 
I 6 G 
F 5 H