diff --git a/feladat3/UnitTest1/UnitTest1.cpp b/feladat3/UnitTest1/UnitTest1.cpp index ddb15a0..8dfb1d7 100644 --- a/feladat3/UnitTest1/UnitTest1.cpp +++ b/feladat3/UnitTest1/UnitTest1.cpp @@ -1,4 +1,4 @@ -// IFPQYY | Tóth Dániel Mátyás | V1 +// IFPQYY | Tóth Dániel Mátyás | V2 #include "pch.h" #include "CppUnitTest.h" #include "feladat3.h" diff --git a/feladat3/UnitTest1/dataset/testin10000.dat.srt b/feladat3/UnitTest1/dataset/testin10000.dat.srt index 23c050e..7d01a70 100644 Binary files a/feladat3/UnitTest1/dataset/testin10000.dat.srt and b/feladat3/UnitTest1/dataset/testin10000.dat.srt differ diff --git a/feladat3/UnitTest1/feladat3.cpp b/feladat3/UnitTest1/feladat3.cpp new file mode 100644 index 0000000..f727140 --- /dev/null +++ b/feladat3/UnitTest1/feladat3.cpp @@ -0,0 +1,265 @@ +// IFPQYY | Tóth Dániel Mátyás | V2 +#include +#include +#include +#include "feladat3.h" +using namespace std; + +static int CopyStr(char* dest, const char* src, const char* concat = "\0"); +static void BubbleSort(unsigned char* arr, int len); + +int GetFilenames(int argc, char* argv[], + char infilenames[MYMAXFILES][MYMAXFN], + char outfilenames[MYMAXFILES][MYMAXFN]) { + if (argc != 2 || argv == nullptr || infilenames == nullptr + || outfilenames == nullptr) { + return NOK; + } + + if (argv[1] == nullptr) + { + return NOK; + } + cout << "Parameter allomany:\n" << argv[1] << '\n'; + ifstream argifs(argv[1]); + if (!argifs.is_open()) + { + cout << "Nem lehet megnyitni a " << argv[1] << " nevu allomanyt\n"; + return NOK; + } + int readAmount = 0; + int i = 0; + char buf[MYMAXFN-1]; + cout << "Feldolgozando allomanyok:\n"; + while (readAmount < 5 && argifs.getline(buf, MYMAXFN-1)) + { + readAmount++; + ifstream inputfile(buf); + if (!inputfile.fail()) + { + CopyStr(infilenames[i], buf); + CopyStr(outfilenames[i], buf, ".srt"); + cout << infilenames[i] << " " << outfilenames[i] << "\tOK\n"; + i++; + } + else + { + cout << "<" << buf << ">" << "\tNOK\n"; + } + } + argifs.close(); + int status = OK; + if (i == 0) + { + status = NOK; + cout << "Ures\tNOK\n"; + } + while (i < 5) { + infilenames[i][0] = '\0'; + outfilenames[i][0] = '\0'; + i++; + } + cout << "*DONE\n"; + return status; +} + +static int CopyStr(char* dest, const char* src, const char* concat) { + if (dest == nullptr || src == nullptr) { + return NOK; + } + int i = 0; + while (src[i] != '\0') + { + dest[i++] = src[i]; + } + int j = 0; + while (concat[j] != '\0') + { + dest[i++] = concat[j++]; + } + dest[i] = '\0'; + return OK; +} + +int SortFile(char* infn, char* outfn, unsigned char* buf, + unsigned short* pmin, unsigned short* pmax, + unsigned short* pavg, unsigned short* pn, + unsigned short bufsize, bool test) { + if (infn == nullptr || outfn == nullptr + || buf == nullptr || pmin == nullptr + || pmax == nullptr || pavg == nullptr + || pn == nullptr) // there has to be a better way than this + { + return NOK; + } + if (bufsize != MYBUFSIZE) // I guess? + { + return NOK; + } + + ifstream readifs(infn, ios_base::binary); + ofstream outofs(outfn, ios_base::binary); + if (!readifs.is_open() || outofs.bad()) + { + return NOK; + } + char filenames[16][30]; + + *pmin = MYMAXVALUE; + *pmax = 0; + *pavg = 0; + *pn = filesystem::file_size(infn)/2; // this can't be more than MYMAXVALUE anyways, so warning dully ignored + + if (test) + { + cout << "\nSTART: " << infn << " ----> " << outfn << "\n"; + cout << "*READ\n"; + } + long long sum = 0; + unsigned short n; + int i = 0; + int j = 0; + int usedfilescnt = 0; + char filename[15]; + while (readifs.read((char*)&n, sizeof(n))) { + if (*pmax < n) { + *pmax = n; + } + if (*pmin > n) { + *pmin = n; + } + sum += n; + buf[(i * 2) % (MYBUFSIZE)] = n; + buf[((i * 2)+1) % (MYBUFSIZE)] = n >> 8; + if (test) + { + cout << i << ".\t" << n << "\n"; + } + i++; + 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) + { + unsigned short insert = (unsigned char)buf[j * 2] + (unsigned char)buf[(j * 2) + 1] * 256; + tempoutofs.write((char*)&insert, sizeof(insert)); + j++; + } + tempoutofs.close(); + usedfilescnt++; + j=0; + } + } + readifs.close(); + + int size = (*pn * 2) % MYBUFSIZE; + if (size > 0) + { + BubbleSort(buf, size); + sprintf_s(filename, "filename%d.dat", usedfilescnt); + CopyStr(filenames[usedfilescnt], filename); + ofstream tempoutofs(filename, ios_base::binary); + while (j < size / 2) + { + unsigned short insert = buf[j * 2] + buf[(j * 2) + 1] * 256; + tempoutofs.write((char*)&insert, sizeof(insert)); + j++; + } + tempoutofs.close(); + usedfilescnt++; + } + else + { + return NOK; + } + + if (test) + { + cout << "\n*SORT, WRITE\n"; + } + + i = 0; + ifstream streams[16]; + for (j = 0; j < usedfilescnt; j++) + { + streams[j].open(filenames[j], ios_base::binary); + unsigned short num; + streams[j].read((char*)&num, sizeof(num)); + buf[j * 2] = num; + buf[(j * 2) + 1] = num >> 8; + } + bool doneFile[16] = {false}; + while (i < *pn) + { + int savedJ = 0; + unsigned short smallest = MYMAXVALUE; + for (size_t j = 0; j < usedfilescnt; j++) + { + if (doneFile[j]) + { + continue; + } + int currentnum = buf[j*2] + buf[j*2 + 1] * 256; + if (currentnum < smallest) + { + smallest = currentnum; + savedJ = j; + } + } + if (test) + { + cout << i << ".\t" << smallest << "\n"; + } + outofs.write((char *)&smallest, sizeof(smallest)); + ifstream tempinifs(filenames[savedJ], ios_base::binary); + unsigned short num; + if (streams[savedJ].read((char*)&num, sizeof(num))) + { + buf[savedJ * 2] = num; + buf[savedJ * 2 + 1] = num >> 8; + } + else + { + doneFile[savedJ] = true; + } + i++; + } + + for (i = 0; i < usedfilescnt; i++) + { + streams[i].close(); + } + + *pavg = sum / *pn; + + if (test) + { + cout << "\n*DONE\n"; + cout << "átlag:\t" << *pavg << "\tmin:\t" << *pmin << "\tmax:\t" << *pmax; + } + return OK; +} + +static void BubbleSort(unsigned char* arr, int len) { //note: this is a char*. you need to sort unsigned short*. kill 7 billion people + for (int i = 0; i < len / 2; i++) + { + for (int j = 0; j < (len / 2) - i - 1; j++) + { + unsigned short first = (unsigned char)arr[j * 2] + (unsigned char)arr[j * 2 + 1] * 256; + unsigned short second = (unsigned char)arr[(j + 1) * 2] + (unsigned char)arr[(j + 1) * 2 + 1] * 256; + if (first > second) + { + unsigned char temp = arr[j * 2]; + arr[j * 2] = arr[(j + 1) * 2]; + arr[(j + 1) * 2] = temp; + + temp = arr[j * 2 + 1]; + arr[j * 2 + 1] = arr[(j + 1) * 2 + 1]; + arr[(j + 1) * 2 + 1] = temp; + } + } + } +} \ No newline at end of file diff --git a/feladat3/UnitTest1/feladat3.h b/feladat3/UnitTest1/feladat3.h new file mode 100644 index 0000000..9953de6 --- /dev/null +++ b/feladat3/UnitTest1/feladat3.h @@ -0,0 +1,15 @@ +// IFPQYY | Tóth Dániel Mátyás | V2 +#pragma once +#define MYBUFSIZE 8192 +#define MYMAXVALUE 65535 +#define MYMAXFILES 5 +#define MYMAXFN 128 +#define OK 0 +#define NOK (-1) +int GetFilenames(int argc, char* argv[], + char infilenames[MYMAXFILES][MYMAXFN], + char outfilenames[MYMAXFILES][MYMAXFN]); +int SortFile(char* infn, char* outfn, unsigned char* buf, + unsigned short* pmin, unsigned short* pmax, + unsigned short* pavg, unsigned short* pn, + unsigned short bufsize = MYBUFSIZE, bool test = false); diff --git a/feladat3/UnitTest1/main.cpp b/feladat3/UnitTest1/main.cpp new file mode 100644 index 0000000..ae914d4 --- /dev/null +++ b/feladat3/UnitTest1/main.cpp @@ -0,0 +1,27 @@ +// IFPQYY | Tóth Dániel Mátyás | V2 +#include "feladat3.h" +#include +using namespace std; + +int main(int argc, char* argv[]) { + char infn[MYMAXFILES][MYMAXFN]; + char outfn[MYMAXFILES][MYMAXFN]; + static unsigned char buf[MYBUFSIZE]; + unsigned short minv, maxv, avgv, nv; + if (GetFilenames(argc, argv, infn, outfn) == NOK) + { + return(NOK); + } + for (int n = 0; (n < MYMAXFILES) && (infn[n][0] != '\0'); n++) + { + if (SortFile(infn[n], outfn[n], buf, &minv, &maxv, &avgv, &nv, MYBUFSIZE, true) == NOK) + return(NOK); + } + /*cout << argc; + for (size_t i = 0; i < argc; i++) + { + cout << '\n'; + cout << argv[i]; + }*/ + return(OK); +} \ No newline at end of file diff --git a/feladat3/feladat3/dataset/testin10000.dat.srt b/feladat3/feladat3/dataset/testin10000.dat.srt index e69de29..7d01a70 100644 Binary files a/feladat3/feladat3/dataset/testin10000.dat.srt and b/feladat3/feladat3/dataset/testin10000.dat.srt differ diff --git a/feladat3/feladat3/dataset/testin5.dat b/feladat3/feladat3/dataset/testin5.dat index 0bea184..843dfc7 100644 --- a/feladat3/feladat3/dataset/testin5.dat +++ b/feladat3/feladat3/dataset/testin5.dat @@ -1 +1 @@ -”kÜ~:#q/# \ No newline at end of file +ÿÿÜ~:#q/# \ No newline at end of file diff --git a/feladat3/feladat3/dataset/testin5.dat.srt b/feladat3/feladat3/dataset/testin5.dat.srt index bf81402..dd04f41 100644 --- a/feladat3/feladat3/dataset/testin5.dat.srt +++ b/feladat3/feladat3/dataset/testin5.dat.srt @@ -1 +1 @@ -#:#q/”kÜ~ \ No newline at end of file +#:#q/Ü~ÿÿ \ No newline at end of file diff --git a/feladat3/feladat3/datasets.txt b/feladat3/feladat3/datasets.txt index 04f5636..2f2bc81 100644 --- a/feladat3/feladat3/datasets.txt +++ b/feladat3/feladat3/datasets.txt @@ -1,7 +1,6 @@ +C:\Users\danit\Desktop\2026-27-cpp-homework\feladat3\feladat3\dataset\testin5.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 \ No newline at end of file +C:\Users\danit\Desktop\2026-27-cpp-homework\feladat3\feladat3\dataset\testin10000.dat diff --git a/feladat3/feladat3/feladat3.cpp b/feladat3/feladat3/feladat3.cpp index b837e5c..f727140 100644 --- a/feladat3/feladat3/feladat3.cpp +++ b/feladat3/feladat3/feladat3.cpp @@ -1,4 +1,4 @@ -// IFPQYY | Tóth Dániel Mátyás | V1 +// IFPQYY | Tóth Dániel Mátyás | V2 #include #include #include @@ -16,6 +16,10 @@ int GetFilenames(int argc, char* argv[], return NOK; } + if (argv[1] == nullptr) + { + return NOK; + } cout << "Parameter allomany:\n" << argv[1] << '\n'; ifstream argifs(argv[1]); if (!argifs.is_open()) @@ -111,7 +115,7 @@ int SortFile(char* infn, char* outfn, unsigned char* buf, cout << "\nSTART: " << infn << " ----> " << outfn << "\n"; cout << "*READ\n"; } - + long long sum = 0; unsigned short n; int i = 0; int j = 0; @@ -124,7 +128,7 @@ int SortFile(char* infn, char* outfn, unsigned char* buf, if (*pmin > n) { *pmin = n; } - *pavg += n; + sum += n; buf[(i * 2) % (MYBUFSIZE)] = n; buf[((i * 2)+1) % (MYBUFSIZE)] = n >> 8; if (test) @@ -140,9 +144,9 @@ int SortFile(char* infn, char* outfn, unsigned char* buf, ofstream tempoutofs(filename, ios_base::binary); while (j < MYBUFSIZE/2) { - unsigned short insert = buf[j * 2] + buf[(j * 2) + 1] * 256; + unsigned short insert = (unsigned char)buf[j * 2] + (unsigned char)buf[(j * 2) + 1] * 256; tempoutofs.write((char*)&insert, sizeof(insert)); - j+=2; + j++; } tempoutofs.close(); usedfilescnt++; @@ -152,18 +156,25 @@ int SortFile(char* infn, char* outfn, unsigned char* buf, readifs.close(); 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) + if (size > 0) { - unsigned short insert = buf[j * 2] + buf[(j * 2) + 1] * 256; - tempoutofs.write((char*)&insert, sizeof(insert)); - j++; + BubbleSort(buf, size); + sprintf_s(filename, "filename%d.dat", usedfilescnt); + CopyStr(filenames[usedfilescnt], filename); + ofstream tempoutofs(filename, ios_base::binary); + while (j < size / 2) + { + unsigned short insert = buf[j * 2] + buf[(j * 2) + 1] * 256; + tempoutofs.write((char*)&insert, sizeof(insert)); + j++; + } + tempoutofs.close(); + usedfilescnt++; + } + else + { + return NOK; } - tempoutofs.close(); - usedfilescnt++; if (test) { @@ -171,31 +182,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) + ifstream streams[16]; + for (j = 0; j < usedfilescnt; j++) { - streams[j].open(filenames[j / 2], ios_base::binary); + streams[j].open(filenames[j], ios_base::binary); unsigned short num; streams[j].read((char*)&num, sizeof(num)); - buf[j] = num; - buf[j+1] = num >> 8; + buf[j * 2] = num; + buf[(j * 2) + 1] = num >> 8; } bool doneFile[16] = {false}; while (i < *pn) { int savedJ = 0; unsigned short smallest = MYMAXVALUE; - for (size_t j = 0; j < usedfilescnt*2; j+=2) + for (size_t j = 0; j < usedfilescnt; j++) { if (doneFile[j]) { continue; } - int currentnum = buf[j] + buf[j + 1] * 256; + int currentnum = buf[j*2] + buf[j*2 + 1] * 256; if (currentnum < smallest) { smallest = currentnum; - savedJ = j/2; + savedJ = j; } } if (test) @@ -206,21 +217,28 @@ int SortFile(char* infn, char* outfn, unsigned char* buf, ifstream tempinifs(filenames[savedJ], ios_base::binary); unsigned short num; if (streams[savedJ].read((char*)&num, sizeof(num))) + { + buf[savedJ * 2] = num; + buf[savedJ * 2 + 1] = num >> 8; + } + else { doneFile[savedJ] = true; } - buf[savedJ] = num; - buf[savedJ + 1] = num >> 8; - int debugnum = buf[savedJ] + buf[savedJ] * 256; i++; } + for (i = 0; i < usedfilescnt; i++) + { + streams[i].close(); + } - *pavg = *pavg / *pn; + *pavg = sum / *pn; if (test) { cout << "\n*DONE\n"; + cout << "átlag:\t" << *pavg << "\tmin:\t" << *pmin << "\tmax:\t" << *pmax; } return OK; } @@ -230,8 +248,8 @@ static void BubbleSort(unsigned char* arr, int len) { //note: this is a char*. y { for (int j = 0; j < (len / 2) - i - 1; j++) { - unsigned short first = arr[j * 2] + arr[j * 2 + 1] * 256; - unsigned short second = arr[(j + 1) * 2] + arr[(j + 1) * 2 + 1] * 256; + unsigned short first = (unsigned char)arr[j * 2] + (unsigned char)arr[j * 2 + 1] * 256; + unsigned short second = (unsigned char)arr[(j + 1) * 2] + (unsigned char)arr[(j + 1) * 2 + 1] * 256; if (first > second) { unsigned char temp = arr[j * 2]; diff --git a/feladat3/feladat3/feladat3.h b/feladat3/feladat3/feladat3.h index 97f28e5..9953de6 100644 --- a/feladat3/feladat3/feladat3.h +++ b/feladat3/feladat3/feladat3.h @@ -1,4 +1,4 @@ -// IFPQYY | Tóth Dániel Mátyás | V1 +// IFPQYY | Tóth Dániel Mátyás | V2 #pragma once #define MYBUFSIZE 8192 #define MYMAXVALUE 65535 diff --git a/feladat3/feladat3/filename0.dat b/feladat3/feladat3/filename0.dat index 730c646..dd04f41 100644 Binary files a/feladat3/feladat3/filename0.dat and b/feladat3/feladat3/filename0.dat differ diff --git a/feladat3/feladat3/filename1.dat b/feladat3/feladat3/filename1.dat index 43650e7..926388b 100644 Binary files a/feladat3/feladat3/filename1.dat and b/feladat3/feladat3/filename1.dat differ diff --git a/feladat3/feladat3/main.cpp b/feladat3/feladat3/main.cpp index 7e388de..ae914d4 100644 --- a/feladat3/feladat3/main.cpp +++ b/feladat3/feladat3/main.cpp @@ -1,4 +1,4 @@ -// IFPQYY | Tóth Dániel Mátyás | V1 +// IFPQYY | Tóth Dániel Mátyás | V2 #include "feladat3.h" #include using namespace std;