Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //============================================================================
- // Name : merge.cpp
- // Author : Hasan Rasulov
- // Version :
- // Copyright : Your copyright notice
- // Description : Hello World in C++, Ansi-style
- //============================================================================
- #include <iostream>
- #include<fstream>
- #include <chrono>
- #include<ctime>
- #include<initializer_list>
- #include<algorithm>
- #include<vector>
- using namespace std;
- class Person {
- string name;
- tm date;
- static string DEFAULT_FORMAT;
- string date_to_string(string format = DEFAULT_FORMAT) const{
- char buff[100];
- strftime(buff, 100, format.c_str(), &date);
- return string { buff };
- }
- tm string_to_date(string date, string format = DEFAULT_FORMAT) const{
- struct tm time;
- strptime(date.c_str(), format.c_str(), &time);
- return time;
- }
- public:
- Person() {
- time_t t;
- time(&t);
- date=*localtime(&t);
- }
- Person(string name, string date, string format = DEFAULT_FORMAT) {
- this->name = name;
- this->date = string_to_date(date, format);
- }
- string get_name() const {
- return this->name;
- }
- void set_name(string name) {
- this->name = name;
- }
- void set_date(string date) {
- this->date = string_to_date(date);
- }
- string get_date() const{
- return date_to_string();
- }
- bool operator>(Person& ob) {
- return this->name.compare(ob.name) < 0;
- }
- bool operator<(Person& ob) {
- return this->name.compare(ob.name) > 0;
- }
- bool operator==(Person& ob) {
- return this->name.compare(ob.name) == 0;
- }
- bool operator!=(Person& ob) {
- return this->name.compare(ob.name) != 0;
- }
- friend istream& operator>>(istream& in, Person& p);
- friend ostream& operator<<(ostream& out, Person& p);
- };
- string Person::DEFAULT_FORMAT = "%Y-%m-%d %H:%M:%S";
- istream& operator>>(istream& in, Person& p) {
- if (in.eof()) {
- return in;
- }
- getline(in, p.name);
- string date;
- if (in.eof()) {
- return in;
- }
- getline(in,date);
- p.date = p.string_to_date(date);
- return in;
- }
- ostream& operator<<(ostream& out, Person& p) {
- out << p.name << '\n';
- out << p.date_to_string() << std::endl;
- return out;
- }
- void merge(string result_filename, istream& file1, istream& file2) {
- ifstream res(result_filename, ios::in);
- Person p;
- vector<Person> vec1;
- vector<Person> vec2;
- while (!file2.eof()) {
- file2 >> p;
- if (!file2.eof())
- vec2.push_back(p);
- }
- while (!file1.eof()) {
- file1 >> p;
- if (!file1.eof())
- vec1.push_back(p);
- }
- vector<Person> result(vec1.size() + vec2.size());
- size_t i = 0, j = 0, k = 0;
- while (i < vec1.size() && j < vec2.size()) {
- if (vec1[i] < vec2[j]) {
- result[k++] = vec2[j++];
- } else if (vec1[i] > vec2[j]) {
- result[k++] = vec1[i++];
- } else if(vec1[i]==vec2[j]) {
- result[k++] = vec1[i++];
- result[k++] = vec2[j++];
- }
- }
- while (i < vec1.size())
- result[k++] = vec1[i++];
- while (j < vec2.size())
- result[k++] = vec2[j++];
- ofstream res_out(result_filename, ios::out);
- for_each(result.begin(), result.end(), [&res_out](Person p) {
- res_out<<p;
- });
- res_out.close();
- }
- void init(){
- ofstream out1("file1.txt", ios::out);
- ofstream out2("file2.txt", ios::out);
- initializer_list<Person> list1 {
- { "Afaq", "1997-2-2 08:33:19" },
- { "Behram", "1976-3-12 19:23:03" },
- { "zahid", "2015-11-12 07:14:09" }
- };
- initializer_list<Person> list2 {
- { "Eray", "1998-5-14 15:43:02" },
- { "Fle", "1987-1-12 18:31:01" },
- { "Zerxan", "1997-10-27 00:00:01" },
- { "asan", "1996-10-29 17:31:01" }
- };
- for_each(list1.begin(), list1.end(), [&out1](Person p) {
- out1<<p;
- });
- for_each(list2.begin(), list2.end(), [&out2](Person p) {
- out2<<p;
- });
- out1.close();
- out2.close();
- }
- int main() {
- init();
- ifstream in1("file1.txt", ios::in);
- ifstream in2("file2.txt", ios::in);
- merge("result.txt", in1, in2);
- in1.close();
- in2.close();
- ifstream in("result.txt", ios::in);
- Person p;
- while (!in.eof()) {
- in >> p;
- if(!in.eof())
- cout << p << '\n';
- }
- in.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement