suffering

This commit is contained in:
Damimani 2026-10-25 12:03:04 +01:00
parent 04e2b81674
commit 92766cd621
6 changed files with 48 additions and 20 deletions

View file

@ -1,5 +1,7 @@
C:\Users\danit\Desktop\2026-27-cpp-homework\feladat3\UnitTest1\dataset\testin5.dat
C:\Users\danit\Desktop\2026-27-cpp-homework\feladat3\UnitTest1\dataset\testin10.dat
C:\Users\danit\Desktop\2026-27-cpp-homework\feladat3\UnitTest1\dataset\testin50.dat
C:\Users\danit\Desktop\2026-27-cpp-homework\feladat3\UnitTest1\dataset\testin100.dat
C:\Users\danit\Desktop\2026-27-cpp-homework\feladat3\UnitTest1\dataset\testin10000.dat
C:\Users\danit\Desktop\2026-27-cpp-homework\feladat3\UnitTest1\dataset\testin5.dat

View file

@ -99,9 +99,7 @@ int SortFile(char* infn, char* outfn, unsigned char* buf,
{
return NOK;
}
unsigned short files[16][MYBUFSIZE/2]; //this is probably a terrible solution. too bad!
int filesizes[16] = { 0 };
int ptrshifts[16] = { 0 };
char filenames[16][30];
*pmin = MYMAXVALUE;
*pmax = 0;
@ -118,6 +116,7 @@ int SortFile(char* infn, char* outfn, unsigned char* buf,
int i = 0;
int j = 0;
int usedfilescnt = 0;
char filename[15];
while (readifs.read((char*)&n, sizeof(n))) {
if (*pmax < n) {
*pmax = n;
@ -136,12 +135,16 @@ int SortFile(char* infn, char* outfn, unsigned char* buf,
if (i % (MYBUFSIZE / 2) == 0)
{
BubbleSort(buf, MYBUFSIZE);
sprintf_s(filename, "filename%d.dat", usedfilescnt);
CopyStr(filenames[usedfilescnt], filename);
ofstream tempoutofs(filename, ios_base::binary);
while (j < MYBUFSIZE/2)
{
files[usedfilescnt][j] = buf[j * 2] + buf[(j * 2) + 1] * 256;
j++;
unsigned short insert = buf[j * 2] + buf[(j * 2) + 1] * 256;
tempoutofs.write((char*)&insert, sizeof(insert));
j+=2;
}
filesizes[usedfilescnt] = MYBUFSIZE / 2;
tempoutofs.close();
usedfilescnt++;
j=0;
}
@ -150,12 +153,16 @@ int SortFile(char* infn, char* outfn, unsigned char* buf,
int size = (*pn * 2) % MYBUFSIZE;
BubbleSort(buf, size);
sprintf_s(filename, "filename%d.dat", usedfilescnt);
CopyStr(filenames[usedfilescnt], filename);
ofstream tempoutofs(filename, ios_base::binary);
while (j < size / 2)
{
files[usedfilescnt][j] = buf[j * 2] + buf[(j * 2) + 1] * 256;
unsigned short insert = buf[j * 2] + buf[(j * 2) + 1] * 256;
tempoutofs.write((char*)&insert, sizeof(insert));
j++;
}
filesizes[usedfilescnt] = size/2;
tempoutofs.close();
usedfilescnt++;
if (test)
@ -164,20 +171,31 @@ int SortFile(char* infn, char* outfn, unsigned char* buf,
}
i = 0;
ifstream* streams = new ifstream[16];
for (j = 0; j < usedfilescnt*2; j+=2)
{
streams[j].open(filenames[j / 2], ios_base::binary);
unsigned short num;
streams[j].read((char*)&num, sizeof(num));
buf[j] = num;
buf[j+1] = num >> 8;
}
bool doneFile[16] = {false};
while (i < *pn)
{
int savedJ = 0;
unsigned short smallest = MYMAXVALUE;
for (j = 0; j < usedfilescnt; j++)
for (size_t j = 0; j < usedfilescnt*2; j+=2)
{
if (ptrshifts[j] >= filesizes[j])
if (doneFile[j])
{
continue;
}
if (files[j][ptrshifts[j]] < smallest)
int currentnum = buf[j] + buf[j + 1] * 256;
if (currentnum < smallest)
{
smallest = files[j][ptrshifts[j]];
savedJ = j;
smallest = currentnum;
savedJ = j/2;
}
}
if (test)
@ -185,7 +203,15 @@ int SortFile(char* infn, char* outfn, unsigned char* buf,
cout << i << ".\t" << smallest << "\n";
}
outofs.write((char *)&smallest, sizeof(smallest));
ptrshifts[savedJ]++;
ifstream tempinifs(filenames[savedJ], ios_base::binary);
unsigned short num;
if (streams[savedJ].read((char*)&num, sizeof(num)))
{
doneFile[savedJ] = true;
}
buf[savedJ] = num;
buf[savedJ + 1] = num >> 8;
int debugnum = buf[savedJ] + buf[savedJ] * 256;
i++;
}

Binary file not shown.

Binary file not shown.

Binary file not shown.