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. transform(sregex_token_iterator(cbegin(precedentTasks), cend(precedentTasks), re, 1), sregex_token_iterator(), back_insert_iterator<list<Task>>(rhs.precedentTask), [](const string& i) {
  37. return i.front();
  38. });
  39.  
  40. return lhs;
  41. }
  42.  
  43. int main() {
  44. 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");
  45.  
  46. list<Tache> allTaches{ istream_iterator<Tache>(seq), istream_iterator<Tache>() };
  47.  
  48. for (const auto& i : allTaches) {
  49. cout << i.step << ' ' << i.duration << ' ';
  50. copy(cbegin(i.precedentTask), cend(i.precedentTask), ostream_iterator<Task>(cout, " "));
  51. cout << endl;
  52. }
  53. }
Success #stdin #stdout 0.01s 3556KB
stdin
Standard input is empty
stdout
A 3 
B 4 A 
C 2 A A 
E 5 A A A 
G 3 A A A A 
J 8 A A A A B H 
H 7 A A A A B H C E G 
I 6 A A A A B H C E G G 
F 5 A A A A B H C E G G H