HasanRasulov

merge.cpp

Oct 25th, 2019
812
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. //============================================================================
  2. // Name : merge.cpp
  3. // Author : Hasan Rasulov
  4. // Version :
  5. // Copyright : Your copyright notice
  6. // Description : Hello World in C++, Ansi-style
  7. //============================================================================
  8.  
  9. #include <iostream>
  10. #include<fstream>
  11. #include <chrono>
  12. #include<ctime>
  13. #include<initializer_list>
  14. #include<algorithm>
  15. #include<vector>
  16. using namespace std;
  17.  
  18. class Person {
  19.  
  20. string name;
  21.  
  22. tm date;
  23.  
  24. static string DEFAULT_FORMAT;
  25.  
  26. string date_to_string(string format = DEFAULT_FORMAT) const{
  27. char buff[100];
  28. strftime(buff, 100, format.c_str(), &date);
  29. return string { buff };
  30. }
  31.  
  32. tm string_to_date(string date, string format = DEFAULT_FORMAT) const{
  33. struct tm time;
  34. strptime(date.c_str(), format.c_str(), &time);
  35. return time;
  36. }
  37.  
  38. public:
  39.  
  40. Person() {
  41. time_t t;
  42. time(&t);
  43. date=*localtime(&t);
  44. }
  45.  
  46.  
  47. Person(string name, string date, string format = DEFAULT_FORMAT) {
  48. this->name = name;
  49. this->date = string_to_date(date, format);
  50. }
  51.  
  52. string get_name() const {
  53. return this->name;
  54. }
  55.  
  56. void set_name(string name) {
  57. this->name = name;
  58. }
  59.  
  60. void set_date(string date) {
  61. this->date = string_to_date(date);
  62. }
  63.  
  64. string get_date() const{
  65. return date_to_string();
  66. }
  67.  
  68. bool operator>(Person& ob) {
  69. return this->name.compare(ob.name) < 0;
  70. }
  71.  
  72. bool operator<(Person& ob) {
  73. return this->name.compare(ob.name) > 0;
  74. }
  75.  
  76. bool operator==(Person& ob) {
  77. return this->name.compare(ob.name) == 0;
  78. }
  79.  
  80. bool operator!=(Person& ob) {
  81. return this->name.compare(ob.name) != 0;
  82. }
  83.  
  84. friend istream& operator>>(istream& in, Person& p);
  85. friend ostream& operator<<(ostream& out, Person& p);
  86.  
  87. };
  88.  
  89. string Person::DEFAULT_FORMAT = "%Y-%m-%d %H:%M:%S";
  90.  
  91. istream& operator>>(istream& in, Person& p) {
  92.  
  93. if (in.eof()) {
  94. return in;
  95. }
  96. getline(in, p.name);
  97.  
  98. string date;
  99. if (in.eof()) {
  100. return in;
  101. }
  102. getline(in,date);
  103.  
  104. p.date = p.string_to_date(date);
  105.  
  106. return in;
  107. }
  108.  
  109. ostream& operator<<(ostream& out, Person& p) {
  110.  
  111. out << p.name << '\n';
  112. out << p.date_to_string() << std::endl;
  113. return out;
  114.  
  115. }
  116.  
  117. void merge(string result_filename, istream& file1, istream& file2) {
  118.  
  119. ifstream res(result_filename, ios::in);
  120.  
  121. Person p;
  122.  
  123. vector<Person> vec1;
  124. vector<Person> vec2;
  125.  
  126. while (!file2.eof()) {
  127. file2 >> p;
  128. if (!file2.eof())
  129. vec2.push_back(p);
  130. }
  131.  
  132. while (!file1.eof()) {
  133. file1 >> p;
  134. if (!file1.eof())
  135. vec1.push_back(p);
  136. }
  137.  
  138. vector<Person> result(vec1.size() + vec2.size());
  139.  
  140. size_t i = 0, j = 0, k = 0;
  141.  
  142. while (i < vec1.size() && j < vec2.size()) {
  143.  
  144. if (vec1[i] < vec2[j]) {
  145. result[k++] = vec2[j++];
  146. } else if (vec1[i] > vec2[j]) {
  147. result[k++] = vec1[i++];
  148. } else if(vec1[i]==vec2[j]) {
  149. result[k++] = vec1[i++];
  150. result[k++] = vec2[j++];
  151. }
  152. }
  153.  
  154. while (i < vec1.size())
  155. result[k++] = vec1[i++];
  156.  
  157.  
  158. while (j < vec2.size())
  159. result[k++] = vec2[j++];
  160.  
  161.  
  162. ofstream res_out(result_filename, ios::out);
  163.  
  164. for_each(result.begin(), result.end(), [&res_out](Person p) {
  165. res_out<<p;
  166. });
  167.  
  168. res_out.close();
  169.  
  170. }
  171.  
  172. void init(){
  173.  
  174. ofstream out1("file1.txt", ios::out);
  175. ofstream out2("file2.txt", ios::out);
  176.  
  177. initializer_list<Person> list1 {
  178. { "Afaq", "1997-2-2 08:33:19" },
  179. { "Behram", "1976-3-12 19:23:03" },
  180. { "zahid", "2015-11-12 07:14:09" },
  181. {}
  182. };
  183.  
  184. initializer_list<Person> list2 {
  185. { "Eray", "1998-5-14 15:43:02" },
  186. { "Fle", "1987-1-12 18:31:01" },
  187. { "Zerxan", "1997-10-27 00:00:01" },
  188. { "asan", "1996-10-29 17:31:01" }
  189. };
  190.  
  191. for_each(list1.begin(), list1.end(), [&out1](Person p) {
  192.  
  193. out1<<p;
  194.  
  195. });
  196.  
  197. for_each(list2.begin(), list2.end(), [&out2](Person p) {
  198.  
  199. out2<<p;
  200.  
  201. });
  202.  
  203. out1.close();
  204. out2.close();
  205. }
  206.  
  207. int main() {
  208.  
  209.  
  210. init();
  211.  
  212. ifstream in1("file1.txt", ios::in);
  213. ifstream in2("file2.txt", ios::in);
  214.  
  215. merge("result.txt", in1, in2);
  216.  
  217. in1.close();
  218. in2.close();
  219.  
  220. ifstream in("result.txt", ios::in);
  221.  
  222. Person p;
  223. while (!in.eof()) {
  224. in >> p;
  225. if(!in.eof())
  226. cout << p << '\n';
  227. }
  228.  
  229. in.close();
  230.  
  231. return 0;
  232. }
Advertisement
Add Comment
Please, Sign In to add comment