Guest User

Untitled

a guest
Apr 19th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <list>
  4. #include <map>
  5. #include <string>
  6. #include <sstream>
  7.  
  8. const std::string BEGIN = "desc";
  9. const std::string END = "csed";
  10. const std::string EQUALS = "=";
  11.  
  12. struct Block
  13. {
  14. std::string action;
  15. std::list<std::string> arguments;
  16. };
  17.  
  18. std::ostream& operator<<(std::ostream& stream, const Block& block)
  19. {
  20. stream << block.action << ' ';
  21.  
  22. for(std::list<std::string>::const_iterator it = block.arguments.begin(); it != block.arguments.end(); it++)
  23. {
  24. stream << *it << ' ';
  25. }
  26.  
  27. return stream;
  28. }
  29.  
  30. std::map<int, Block> parse()
  31. {
  32. std::ifstream input("f.txt");
  33. std::map<int, Block> blocks;
  34. std::string line, equals, action, arg;
  35. int id;
  36. bool flag = true;
  37.  
  38. // input.open(name);
  39.  
  40. getline(input, line);
  41.  
  42. if(line.compare(BEGIN) != 0)
  43. {
  44. // throw std::invalid_argument("Exception incorrect data");
  45. }
  46.  
  47. while(flag)
  48. {
  49. getline(input, line);
  50.  
  51. if(line.compare(END) == 0)
  52. {
  53. flag = false;
  54. }
  55.  
  56. if(flag)
  57. {
  58. std::stringstream stream(line);
  59.  
  60. stream >> id >> equals;
  61.  
  62. if(equals.compare(EQUALS) == 0)
  63. {
  64. Block token;
  65.  
  66. stream >> action;
  67. token.action = action;
  68.  
  69. while(!stream.eof())
  70. {
  71. stream >> arg;
  72. token.arguments.push_back(arg);
  73. }
  74.  
  75. blocks[id] = token;
  76. }
  77. else
  78. {
  79. // throw std::invalid_argument("Exception incorrect data");
  80. }
  81. }
  82. }
  83.  
  84. return blocks;
  85. }
  86.  
  87. std::list<int> transporter()
  88. {
  89. std::ifstream input("f.txt");
  90. std::list<int> transporter;
  91. std::string line, next;
  92. int id;
  93. bool flag = true;
  94.  
  95. getline(input, line);
  96.  
  97. if(line.compare(BEGIN) != 0)
  98. {
  99. // throw std::invalid_argument("Exception incorrect data");
  100. }
  101.  
  102. while(flag)
  103. {
  104. getline(input, line);
  105.  
  106. if(line.compare(END) == 0)
  107. {
  108. flag = false;
  109. }
  110. }
  111.  
  112. getline(input, line);
  113.  
  114. std::stringstream stream(line);
  115.  
  116. while(!stream.eof())
  117. {
  118. stream >> id >> next;
  119. transporter.push_back(id);
  120. }
  121.  
  122. return transporter;
  123. }
  124.  
  125. int main()
  126. {
  127. std::map<int, Block> lol;
  128. std::list<int> what;
  129.  
  130. lol = parse();
  131. what = transporter();
  132.  
  133. for(std::map<int, Block>::const_iterator it = lol.begin(); it != lol.end(); it++)
  134. {
  135. std::cout << it->first << "=" << it->second << std::endl;
  136. }
  137.  
  138. for(std::list<int>::const_iterator it = what.begin(); it != what.end(); it++)
  139. {
  140. std::cout << *it << ' ';
  141. }
  142.  
  143. getchar();
  144. return 0;
  145. }
Add Comment
Please, Sign In to add comment