-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkWayMerge.cpp
More file actions
101 lines (85 loc) · 1.88 KB
/
Copy pathkWayMerge.cpp
File metadata and controls
101 lines (85 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream>
#include <string.h>
#include <fstream>
#include <list>
using namespace std;
class Record
{
public:
char usn[20], name[20];
};
void mergeFile(string file1, string file2, string file3)
{
Record f1Records[20];
Record f2Records[20];
fstream f1, f2;
int n = 0;
int m = 0;
f1.open(file1, ios::in);
f2.open(file2, ios::in);
while (!f1.eof())
{
f1.getline(f1Records[n].name, 20, '|');
f1.getline(f1Records[n].usn, 20, '\n');
n++;
}
while (!f2.eof())
{
f2.getline(f2Records[m].name, 20, '|');
f2.getline(f2Records[m].usn, 20, '\n');
m++;
}
int i = 0;
int j = 0;
fstream f3;
f3.open(file3, ios::out);
while (i < n - 1 and j < m - 1)
{
if (strcmp(f1Records[i].name, f2Records[j].name) <= 0)
{
f3 << f1Records[i].name << "|" << f1Records[i].usn << "\n";
i++;
}
else
{
f3 << f2Records[j].name << "|" << f2Records[j].usn << "\n";
j++;
}
}
while (i < n - 1)
{
f3 << f1Records[i].name << "|" << f1Records[i].usn << "\n";
i++;
}
while (j < m - 1)
{
f3 << f2Records[j].name << "|" << f2Records[j].usn << "\n";
j++;
}
f1.close();
f2.close();
f3.close();
}
void kWayMerge(list<string> filenames)
{
while (filenames.size() > 1)
{
string file1 = filenames.front();
filenames.pop_front();
string file2 = filenames.front();
filenames.pop_front();
string file3 = file1 + file2;
mergeFile(file1, file2, file3);
filenames.push_back(file3);
}
}
int main()
{
int K = 4;
list<string> filenames;
for (int i = 1; i <= K; i++)
filenames.push_back(to_string(i));
kWayMerge(filenames);
cout << "Merge complete\n";
return 0;
}