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
|
void merge(ifstream& infile1, ifstream& infile2, ofstream& outfile, const size_t& bufsize, const string& in_title1, const string& in_title2, const string& out_title){
infile1.open(in_title1.c_str(), ios::in);
infile2.open(in_title2.c_str(), ios::in);
outfile.open(out_title.c_str(), ios::out);
string word1[bufsize];
string word2[bufsize];
string temp1, temp2; //to avoid repeating the last words...
int index1 = 0, index2 = 0, w1_count = 0, w2_count = 0;
int a,b,ele = 0;
while(true){
fill_1:
if(infile1.eof() || w1_count > 0) goto fill_2;
index1 = 0;
while(w1_count < bufsize && infile1 >> word1[w1_count]){
w1_count++;
}
fill_2:
if(infile2.eof() || w2_count > 0) goto skip;
index2 = 0;
while(w2_count < bufsize && infile2 >> word2[w2_count]){
w2_count++;
}
skip:
while(w1_count > 0 && w2_count > 0){
if(word1[index1] < word2[index2]){
outfile << word1[index1] + " ";
index1++;
w1_count--;
ele++;
}
else{
outfile << word2[index2] + " ";
index2++;
w2_count--;
ele++;
}
}
if(!infile1.eof() && !infile2.eof()){
goto fill_1;
}
if(infile1.eof() && infile2.eof()){
if(w1_count == 0 && w2_count > 0){
while(w2_count >0){
outfile << word2[index2] + " ";
index2++;
w2_count--;
ele++;
}
}
else if(w1_count > 0 && w2_count == 0){
while(w1_count > 0){
outfile << word1[index1] + " ";
index1++;
w1_count--;
ele++;
}
}
infile1.close();
infile2.close();
outfile.close();
cout<< ele << " words are merged, and ";
return;
}
}
}
|