Advertisement
Guest User

Untitled

a guest
Dec 31st, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.81 KB | None | 0 0
  1. #include "Tree.h"
  2. int
  3. main (void)
  4. {
  5. Tree_node *Top;
  6. int returning;
  7. string Simbol;//символ
  8. ifstream file("Base.txt");
  9. ifstream Command_file ("Commands.txt");
  10. map<string, User> Users;
  11. getline (file, Simbol);
  12. getline (file, Simbol);
  13. Top = Construction_tree(Simbol, file);
  14. file.close();
  15. string Command;
  16. unsigned int Num_str = 0;
  17. while (!Command_file.eof())
  18. {
  19. getline (Command_file, Command);
  20. Num_str++;
  21. try
  22. {
  23. string Name_of_user = Split_str (Command, "::");
  24. if (Users.find(Name_of_user) == Users.end())
  25. {
  26. Users[Name_of_user];
  27. Users[Name_of_user].Change_point(Top);
  28. }
  29. if (Users[Name_of_user].Parse(Command) == 2)
  30. {
  31. break;
  32. }
  33. }
  34. catch (string i)
  35. {
  36. cout << "Неверно составлен запрос ::" << Num_str <<"-ая строка\n";
  37. return 0;
  38. }
  39. }
  40. // New_user.Parse("PRINT MATLOG 2rav ");
  41. // New_user.Parse("DELETE MM~MATH~VM~Afonin~");
  42. // returning = New_user.Parse("ADD MM|MATH|VM|[Afonin|~Proga~(123)tut opisanie~|2rav~(12)tut tozhe~|]");
  43. // New_user.Parse("SELECT MM");
  44. // New_user.Parse("PRINT Proga 2rav ");
  45. // New_user.Parse("CLEAR ");
  46. // New_user.Parse("SELECT VM");
  47. // New_user.Parse("END ");
  48. /* for (map<string, User>::iterator i = Users.begin(); i != Users.end(); i++)
  49. {
  50. delete i -> second;
  51. }*/
  52. Command_file.close();
  53. ofstream Out_file("Base.txt");
  54. Off_base(Top, Out_file);
  55. Out_file.close();
  56. Cleaner(Top);
  57.  
  58. }
  59. void
  60. Cleaner (Tree_node * Top_node)
  61. {
  62. for (unsigned int n = 0; n < (Top_node -> Down.size()); ++n)
  63. {
  64. Cleaner (Top_node -> Down[n]);
  65. }
  66. delete Top_node;
  67. }
  68. Tree_node *
  69. Construction_tree(string Node, ifstream &file)
  70. {
  71. string Simbol_;
  72. Tree_node *Top_node = new Tree_node;
  73. Top_node -> Name_of_node = Node;
  74. getline (file, Simbol_);
  75. if (Simbol_ == "{")
  76. {
  77. Top_node -> IsAHum_ = false ;
  78. getline (file, Simbol_);
  79. while (Simbol_ != "}" && file)
  80. {
  81. Top_node -> Down.insert (Top_node -> Down.end(), Construction_tree(Simbol_, file));
  82. getline (file, Simbol_);
  83. }
  84. }
  85. else if (Simbol_ == "[")
  86. {
  87. getline (file, Simbol_);
  88. while (Simbol_ != "]"&& file)
  89. {
  90. string Name_of_work;
  91. string Buf;
  92. int End_of_name = Simbol_.find("~");
  93. int End_of_hours = Simbol_.find("|");
  94. Name_of_work = Simbol_.substr(0,End_of_name);
  95. Top_node -> Work[Name_of_work].Description = Simbol_.substr( End_of_hours + 1);
  96. Top_node -> Work[Name_of_work].Duration = Find_int_str(Simbol_.substr (End_of_name + 1, End_of_hours - End_of_name - 1));
  97. getline (file, Simbol_);
  98. }
  99. Top_node->IsAHum_ = true;
  100. }
  101. return Top_node;
  102. }
  103. void
  104. Off_base(Tree_node * Top_node, ofstream &file)
  105. {
  106. if (Top_node -> IsAHum_ == false)
  107. {
  108. file<<"\n" << Top_node -> Name_of_node << "\n{";
  109. cout <<"\n"<< Top_node -> Name_of_node << "\n{";
  110. for(unsigned int n = 0; n < Top_node -> Down.size(); ++n)
  111. {
  112. Off_base (Top_node -> Down [n], file);
  113. }
  114. file << "\n}";
  115. cout << "\n}";
  116. }
  117. else if(Top_node -> IsAHum_ == true)
  118. {
  119. file << "\n"<<Top_node -> Name_of_node << "\n[\n";
  120. cout <<"\n"<< Top_node -> Name_of_node << "\n[\n";
  121. for(map <string , Description_of_work>::iterator i = Top_node -> Work.begin(); i != Top_node -> Work.end(); ++i)
  122. {
  123. file << i -> first <<"~" << i -> second.Duration << "|" << i -> second.Description << "\n";
  124. cout << i -> first << "~"<< i -> second.Duration << "|" << i -> second.Description << "\n";
  125. }
  126. file << "]";
  127. cout << "]";
  128.  
  129. }
  130. }
  131. string
  132. Split_str (string & Splitng_str, string separator)
  133. {
  134. string buf;
  135. string Word;
  136. int End_word;
  137. End_word = Splitng_str.find(separator);
  138. if (End_word == int(string::npos))
  139. {
  140. throw; //ВЕРНУТЬ ОШИБКУ
  141. }
  142. Word = Splitng_str.substr(0, End_word);
  143. Splitng_str = Splitng_str.substr(End_word+separator.length());
  144. return Word;
  145. }
  146. int
  147. Find_int_str (string New_int)
  148. {
  149. int Returning_value = 0;
  150. while ( New_int.length() != 0)
  151. {
  152. string Symbol = New_int.substr(0, 1);
  153. New_int = New_int.substr (1);
  154. if ( Symbol == "0")
  155. {
  156. Returning_value = Returning_value*10 + 0;
  157. }
  158. else if (Symbol == "1")
  159. {
  160. Returning_value = Returning_value*10 + 1;
  161. }
  162. else if (Symbol == "2")
  163. {
  164. Returning_value = Returning_value*10 + 2;
  165. }
  166. else if (Symbol == "3")
  167. {
  168. Returning_value = Returning_value*10 + 3;
  169. }
  170. else if (Symbol == "4")
  171. {
  172. Returning_value = Returning_value*10 + 4;
  173. }
  174. else if (Symbol == "5")
  175. {
  176. Returning_value = Returning_value*10 + 5;
  177. }
  178. else if (Symbol == "6")
  179. {
  180. Returning_value = Returning_value*10 + 6;
  181. }
  182. else if (Symbol == "7")
  183. {
  184. Returning_value = Returning_value*10 + 7;
  185. }
  186. else if (Symbol == "8")
  187. {
  188. Returning_value = Returning_value*10 + 8;
  189. }
  190. else if (Symbol == "9")
  191. {
  192. Returning_value = Returning_value*10 + 9;
  193. }
  194. else
  195. {
  196. throw "No numer";
  197. }
  198.  
  199. }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement