Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. vector <string> list, list2, tasks;
  9.  
  10. struct Knot {
  11. string Value;
  12. string Value2;
  13. Knot *Left = nullptr;
  14. Knot *Right = nullptr;
  15. };
  16.  
  17. bool isLarger (string v1, string v2) {
  18. if (v1.size() > v2.size()) return true;
  19. else if (v1.size() < v2.size()) return false;
  20. else if (v1 > v2) return true;
  21. return false;
  22. }
  23.  
  24. void Insert (string v1, string v2, Knot *&root) {
  25. if (root == nullptr) {
  26. root = new Knot;
  27. root->Value = v1;
  28. root->Value2 = v2;
  29. }
  30. else if (root->Value == v1 && root->Value2 == v2) return;
  31. else if (isLarger(v1, root->Value) || isLarger(v2, root->Value2)) Insert (v1, v2, root->Right);
  32. else if (!isLarger(v1, root->Value) || !isLarger(v2, root->Value2)) Insert (v1, v2, root->Left);
  33. else return;
  34. }
  35.  
  36. void Pre_Order (Knot *root) {
  37. if (root != nullptr) {
  38. list.push_back(root->Value);
  39. list2.push_back(root->Value2);
  40. Pre_Order(root->Left);
  41. Pre_Order(root->Right);
  42. }
  43. }
  44.  
  45. string Minimum (Knot *root) {
  46. //root = root->Right;
  47. while (root->Left != nullptr) root = root->Left;
  48. return root->Value;
  49. }
  50.  
  51. void Delete (string v1, string v2, Knot *&root) {
  52. if(root != nullptr) {
  53. if (root->Value == v1 && root->Value2 == v2 && root->Left == nullptr && root->Right == nullptr) root = nullptr;
  54. else if (root->Value == v1 && root->Value2 == v2 && root->Left == nullptr && root->Right != nullptr) root = root->Right;
  55. else if (root->Value == v1 && root->Value2 == v2 && root->Left != nullptr && root->Right == nullptr) root = root->Left;
  56. else if (root->Value == v1 && root->Value2 == v2 && root->Left != nullptr && root->Right != nullptr) {
  57. root->Value = Minimum(root->Right);
  58. Delete(root->Value, root->Value2, root->Right);
  59. }
  60. else if (isLarger(root->Value, v1) || isLarger(root->Value2, v2)) Delete(v1, v2, root->Left);
  61. else if (!isLarger(root->Value, v1) || !isLarger(root->Value2, v2)) Delete(v1, v2, root->Right);
  62. }
  63. }
  64.  
  65. void Search (string v1, string v2, Knot *root, int &flag) {
  66. if (root != nullptr) {
  67. if (root->Value == v1 && root->Value2 == v2) { flag = 1; return; }
  68. else if (isLarger(root->Value, v1) || isLarger(root->Value2, v2)) Search (v1, v2, root->Left, flag);
  69. else if (!isLarger(root->Value, v1) || !isLarger(root->Value2, v2)) Search (v1, v2, root->Right, flag);
  70. else { flag = 0; return; }
  71. }
  72. else { flag = 0; return; }
  73. }
  74.  
  75. void Count (string v, Knot *root, int &c) {
  76. if (root != nullptr) {
  77. if (root->Value == v) c++;
  78. Count (v, root->Left, c);
  79. Count (v, root->Right, c);
  80. }
  81. }
  82.  
  83. int main(int argc, char *argv[]) {
  84. ifstream in ("In.txt");
  85. ofstream out ("Out.txt");
  86. if (!in) { cout << "BLAD ODCZYTU PLIKU!\n"; return 1; }
  87.  
  88. string n;
  89. int x, flag = 0, c = 0;
  90.  
  91. in >> x;
  92.  
  93. Knot *root = nullptr;
  94.  
  95. for (int i = 0; i <= x; i++) {
  96. in >> n;
  97.  
  98. string temp = "", after = "", before = "", task = n;
  99.  
  100. getline(in, n);
  101.  
  102. for (int i = 0; i < n.size(); i++) {
  103. if (n[i] == ',') { before = temp; temp = ""; }
  104. else if(n[i] != ' ') temp+=n[i];
  105. }
  106. after = temp;
  107. if (task == "W") Insert (before, after, root);
  108. else if (task == "U") Delete (before, after, root);
  109. else if (task == "S") Search (before, after, root, flag);
  110. else if (task == "L") Count (after, root, c);
  111. }
  112. in.close();
  113. if (flag == 0) out << "NIE\n";
  114. else if (flag == 1) out << "TAK\n";
  115. out << c;
  116. out.flush();
  117. out.close();
  118. return 0;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement