Advertisement
illfate

Untitled

May 19th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. //////main.cpp///////
  2.  
  3. #include <vector>
  4. #include <iostream>
  5. #include <utility>
  6. #include <string>
  7. #include <algorithm>
  8. #include "string_edit.h"
  9. #include <fstream>
  10. #include <set>
  11. #include <algorithm>
  12. #include <string>
  13.  
  14. using namespace std;
  15.  
  16. ostream& operator<<(ostream& out, const vector<string>& vect) {
  17. for (const auto& x : vect) {
  18. out << x << " ";
  19. }
  20. return out;
  21. }
  22.  
  23. vector<string> Intersection(const string& file_name) {
  24. vector<string> result;
  25. vector<vector<string>> file_sets;
  26. ifstream in(file_name);
  27. if (in) {
  28. string line;
  29. for(int i=0; getline(in, line);i++){
  30. StringModify str_mod(line);
  31. vector<string> temp = str_mod.modify();
  32. if (i!=0) {
  33. for (string& str : temp) {
  34. sort(str.begin(), str.end());
  35. }
  36. }
  37. file_sets.push_back(move(temp));
  38. }
  39. }
  40. for (const auto& element : *file_sets.begin()) {
  41. size_t counter = 0;
  42. string element_sort = element;
  43. sort(element_sort.begin(), element_sort.end());
  44. for (auto set_ = file_sets.begin() + 1; set_ != file_sets.end(); set_++) {
  45. for (const auto& x : *set_) {
  46. if (element_sort == x) {
  47. counter++;
  48. break;
  49. }
  50. }
  51. }
  52. if (counter == file_sets.size() - 1) {
  53. result.push_back(element);
  54. for (auto set_ = file_sets.begin() + 1; set_ != file_sets.end(); set_++) {
  55. auto it = find(set_->begin(), set_->end(), element_sort);
  56. if (it != set_->end()) {
  57. set_->erase(it);
  58. }
  59. }
  60. }
  61. counter = 0;
  62. }
  63. return result;
  64. }
  65.  
  66. int main() {
  67.  
  68. cout << Intersection("test.txt");
  69. }
  70.  
  71.  
  72.  
  73. /////string_edit.h////////
  74. #pragma once
  75. #include <string>
  76. #include <vector>
  77. #include <set>
  78. #include <iostream>
  79. #include <sstream>
  80. #include <string_view>
  81.  
  82. class StringModify {
  83. public:
  84. StringModify(const std::string& str):string_(str){}
  85.  
  86. std::vector<std::string> modify() {
  87. clean(string_);
  88. return split(string_);
  89. }
  90. private:
  91. std::string string_;
  92. void clean(std::string& str) {
  93. str.erase(str.begin());
  94. str.erase(prev(str.end()));
  95. const auto end = str.end();
  96. auto it = str.begin();
  97. while (it != end) {
  98. it = std::find(it, end, ' ');
  99. if (it == end) {
  100. break;
  101. }
  102. str.erase(it);
  103. }
  104. }
  105. std::vector<char> open_symbols{ '<','{','[' };
  106. std::vector<char> close_symbols{ '>','}',']' };
  107.  
  108. bool consits(const std::vector<char>& vector, char ch) {
  109. return std::find(vector.begin(), vector.end(), ch) == vector.end() ? false : true;
  110. }
  111.  
  112. std::vector<std::string> split(const std::string& str) {
  113. std::vector<std::string> result;
  114. size_t counter = 0;
  115. auto pos = str.begin();
  116. if (str == "") {
  117. result.push_back(str);
  118. return result;
  119. }
  120. for (auto it = str.begin(); it != str.end(); it++) {
  121. if (*it == ',' && counter == 0) {
  122. result.push_back(std::string(pos, it));
  123. pos = next(it);
  124. }
  125. else if (consits(open_symbols, *it)) {
  126. counter++;
  127. }
  128. else if (consits(close_symbols, *it)) {
  129. counter--;
  130. if (counter == 0) {
  131. result.push_back(std::string(pos, next(it)));
  132. if (next(it) != str.end()) {
  133. pos = next(++it);
  134. }
  135. }
  136. }
  137. else if (it == prev(str.end())) {
  138. result.push_back(std::string(1, *it));
  139. }
  140. }
  141. return result;
  142. }
  143. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement