Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <list>
  5. #include <fstream>
  6. #include <ctime>
  7. #include <chrono>
  8. #include <thread>
  9.  
  10.  
  11. class Command
  12. {
  13. public:
  14. Command(std::string commandName):
  15. commandName(commandName)
  16. {
  17. time = std::time(nullptr);
  18. // std::cout << std::to_string(time) <<std::endl;
  19. }
  20. std::string commandName;
  21. int time;
  22. };
  23.  
  24. class Observer
  25. {
  26. public:
  27. virtual void update(std::list<Command *>& commands) = 0;
  28. };
  29.  
  30. class Printer
  31. {
  32. int n; //Размер блока. Задается в конструкторе
  33. int nCounter = 0; //счетчик вложенных скобок
  34. int historySize = 0; //Размер буфера
  35. std::vector<Observer*> subs; //Подписчики
  36. std::list<Command*> buffer; //Список команд
  37. public:
  38. Printer(int n):n(n){}
  39. Printer() = delete;
  40. void subscribe(Observer *obs)
  41. {
  42. subs.push_back(obs);
  43. }
  44.  
  45. void printAll(std::list<Command *>& commands)
  46. {
  47. for(auto &s: subs)
  48. {
  49. s->update(commands);
  50. }
  51. historySize = 0;
  52. buffer.clear();
  53. }
  54.  
  55. void print(std::string cmd)
  56. {
  57. if (cmd[0] == '{')
  58. {
  59. if (!nCounter && historySize) //Вывести буфер, если открылась ПЕРВАЯ скобка
  60. {
  61. printAll(buffer);
  62. }
  63. ++nCounter;
  64. }
  65. else if (cmd[0] == '}')
  66. {
  67. --nCounter;
  68. if (nCounter <= 0) //Вывести буфер, если закрылась ПЕРВАЯ скобка
  69. {
  70. printAll(buffer);
  71. }
  72.  
  73. }
  74. else if (!std::cin.eof())
  75. {
  76. buffer.push_back(new Command(cmd));
  77. if (!nCounter)
  78. {
  79. ++historySize;
  80. if (historySize >=n)
  81. {
  82. printAll(buffer);
  83. }
  84. }
  85. }
  86. else if (historySize && !nCounter)
  87. {
  88. //std::cout <<"historySize = " << historySize <<std::endl;
  89. printAll(buffer);
  90. }
  91. }
  92. };
  93.  
  94. class Console: public Observer
  95. {
  96. public:
  97. Console(Printer& print)
  98. {
  99. print.subscribe(this);
  100. }
  101.  
  102. void update(std::list<Command *>& commands) override
  103. {
  104. std::string text = "";
  105. for (auto command : commands)
  106. {
  107. text+=command->commandName + " ";
  108. }
  109. std::cout << "bulk: " << text <<std::endl;
  110. }
  111. };
  112.  
  113. class File: public Observer
  114. {
  115. public:
  116. File(Printer& print)
  117. {
  118. print.subscribe(this);
  119. }
  120.  
  121. void update(std::list<Command *>& commands) override
  122. {
  123. std::this_thread::sleep_for(std::chrono::seconds(1));
  124. std::string text = "";
  125. std::string fileName = "bulk" + std::to_string(commands.front()->time);
  126. std::ofstream outfile ("./" + fileName + ".txt",std::ofstream::binary);
  127. for (auto command : commands)
  128. {
  129. text+=command->commandName + "\n";
  130. }
  131. outfile.write(text.c_str(),text.size());
  132. outfile.close();
  133.  
  134. std::cout << fileName + ".txt created\n" << std::endl;
  135. }
  136. };
  137.  
  138. int main(int argc, char** argv)
  139. {
  140.  
  141. int n; //Размер блока команд
  142.  
  143. //std::cout << "Введите размер блока: ";
  144. //std::cin >> n;
  145. n = std::stoi(argv[1]);
  146.  
  147. //std::cout<<"The argument is"<<argv[1]<<std::endl;
  148.  
  149. std::string str;
  150. //std::getline(std::cin, str); //После std::cin первый getline не срабатывает
  151.  
  152. Printer printer{n}; //Класс вывода на экран
  153. Console console{printer}; //Подписать консольно на вывод
  154. File file{printer}; //Подписать файл на вывод
  155.  
  156. while (!std::cin.eof()) //Cntr + D -конец файла
  157. {
  158. std::getline(std::cin, str);
  159. printer.print(str);
  160. }
  161. return 0;
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement