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
|
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
bool splitFile(fstream &fileA, fstream &tempA,fstream &tempB)
{
string currValue,prevValue;
bool swapFile,isEmptyB;
isEmptyB = true;
if(!fileA.eof())
{
getline(fileA,currValue);
tempA<<currValue<<'\n';
prevValue = currValue;
}
while(!fileA.eof())
{
getline(fileA,currValue);
if(prevValue > currValue)
swapFile = !swapFile;
if(swapFile)
tempA<<currValue<<'\n';
else
{
tempB<<currValue<<'\n';
isEmptyB = false;
}
}
return isEmptyB;
}
int main()
{
string pathA,pathTempA,pathTempB;
fstream fileA,tempA,tempB;
cout<<"Enter the file you want to split for natural merge sort "<<'\n';
getline(cin,pathA);
pathTempA = pathA.substr(0,pathA.find('.'))+"_temp001.txt";
pathTempB = pathA.substr(0,pathA.find('.'))+"_temp002.txt";
fileA.open(pathA,ios::in);
tempA.open(pathTempA,ios::out);
tempB.open(pathTempB,ios::out);
if(!fileA.good())
return 1;
splitFile(fileA,tempA,tempB);
fileA.close();
tempA.close();
tempB.close();
return 0;
}
|