diff --git a/feladat3/UnitTest1/UnitTest1.cpp b/feladat3/UnitTest1/UnitTest1.cpp index 8dfb1d7..ddb15a0 100644 --- a/feladat3/UnitTest1/UnitTest1.cpp +++ b/feladat3/UnitTest1/UnitTest1.cpp @@ -1,4 +1,4 @@ -// IFPQYY | Tóth Dániel Mátyás | V2 +// IFPQYY | Tóth Dániel Mátyás | V1 #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 7d01a70..23c050e 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 deleted file mode 100644 index f727140..0000000 --- a/feladat3/UnitTest1/feladat3.cpp +++ /dev/null @@ -1,265 +0,0 @@ -// 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 deleted file mode 100644 index 9953de6..0000000 --- a/feladat3/UnitTest1/feladat3.h +++ /dev/null @@ -1,15 +0,0 @@ -// 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 deleted file mode 100644 index ae914d4..0000000 --- a/feladat3/UnitTest1/main.cpp +++ /dev/null @@ -1,27 +0,0 @@ -// 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 7d01a70..e69de29 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 843dfc7..0bea184 100644 --- a/feladat3/feladat3/dataset/testin5.dat +++ b/feladat3/feladat3/dataset/testin5.dat @@ -1 +1 @@ -ÿÿÜ~:#q/# \ No newline at end of file +”kÜ~:#q/# \ No newline at end of file diff --git a/feladat3/feladat3/dataset/testin5.dat.srt b/feladat3/feladat3/dataset/testin5.dat.srt index dd04f41..bf81402 100644 --- a/feladat3/feladat3/dataset/testin5.dat.srt +++ b/feladat3/feladat3/dataset/testin5.dat.srt @@ -1 +1 @@ -#:#q/Ü~ÿÿ \ No newline at end of file +#:#q/”kÜ~ \ No newline at end of file diff --git a/feladat3/feladat3/datasets.txt b/feladat3/feladat3/datasets.txt index 2f2bc81..04f5636 100644 --- a/feladat3/feladat3/datasets.txt +++ b/feladat3/feladat3/datasets.txt @@ -1,6 +1,7 @@ -C:\Users\danit\Desktop\2026-27-cpp-homework\feladat3\feladat3\dataset\testin5.dat -C:\Users\danit\Desktop\2026-27-cpp-homework\feladat3\feladat3\dataset\testin10000.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 diff --git a/feladat3/feladat3/feladat3.cpp b/feladat3/feladat3/feladat3.cpp index f727140..b837e5c 100644 --- a/feladat3/feladat3/feladat3.cpp +++ b/feladat3/feladat3/feladat3.cpp @@ -1,4 +1,4 @@ -// IFPQYY | Tóth Dániel Mátyás | V2 +// IFPQYY | Tóth Dániel Mátyás | V1 #include #include #include @@ -16,10 +16,6 @@ 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()) @@ -115,7 +111,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; @@ -128,7 +124,7 @@ int SortFile(char* infn, char* outfn, unsigned char* buf, if (*pmin > n) { *pmin = n; } - sum += n; + *pavg += n; buf[(i * 2) % (MYBUFSIZE)] = n; buf[((i * 2)+1) % (MYBUFSIZE)] = n >> 8; if (test) @@ -144,9 +140,9 @@ int SortFile(char* infn, char* outfn, unsigned char* buf, 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; + unsigned short insert = buf[j * 2] + buf[(j * 2) + 1] * 256; tempoutofs.write((char*)&insert, sizeof(insert)); - j++; + j+=2; } tempoutofs.close(); usedfilescnt++; @@ -156,25 +152,18 @@ int SortFile(char* infn, char* outfn, unsigned char* buf, 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) { - 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; + unsigned short insert = buf[j * 2] + buf[(j * 2) + 1] * 256; + tempoutofs.write((char*)&insert, sizeof(insert)); + j++; } + tempoutofs.close(); + usedfilescnt++; if (test) { @@ -182,31 +171,31 @@ int SortFile(char* infn, char* outfn, unsigned char* buf, } i = 0; - ifstream streams[16]; - for (j = 0; j < usedfilescnt; j++) + ifstream* streams = new ifstream[16]; + for (j = 0; j < usedfilescnt*2; j+=2) { - streams[j].open(filenames[j], ios_base::binary); + streams[j].open(filenames[j / 2], ios_base::binary); unsigned short num; streams[j].read((char*)&num, sizeof(num)); - buf[j * 2] = num; - buf[(j * 2) + 1] = num >> 8; + buf[j] = num; + buf[j+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++) + for (size_t j = 0; j < usedfilescnt*2; j+=2) { if (doneFile[j]) { continue; } - int currentnum = buf[j*2] + buf[j*2 + 1] * 256; + int currentnum = buf[j] + buf[j + 1] * 256; if (currentnum < smallest) { smallest = currentnum; - savedJ = j; + savedJ = j/2; } } if (test) @@ -217,28 +206,21 @@ 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 = sum / *pn; + *pavg = *pavg / *pn; if (test) { cout << "\n*DONE\n"; - cout << "átlag:\t" << *pavg << "\tmin:\t" << *pmin << "\tmax:\t" << *pmax; } return OK; } @@ -248,8 +230,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 = (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; + unsigned short first = arr[j * 2] + arr[j * 2 + 1] * 256; + unsigned short second = arr[(j + 1) * 2] + 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 9953de6..97f28e5 100644 --- a/feladat3/feladat3/feladat3.h +++ b/feladat3/feladat3/feladat3.h @@ -1,4 +1,4 @@ -// IFPQYY | Tóth Dániel Mátyás | V2 +// IFPQYY | Tóth Dániel Mátyás | V1 #pragma once #define MYBUFSIZE 8192 #define MYMAXVALUE 65535 diff --git a/feladat3/feladat3/filename0.dat b/feladat3/feladat3/filename0.dat index dd04f41..730c646 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 926388b..43650e7 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 ae914d4..7e388de 100644 --- a/feladat3/feladat3/main.cpp +++ b/feladat3/feladat3/main.cpp @@ -1,4 +1,4 @@ -// IFPQYY | Tóth Dániel Mátyás | V2 +// IFPQYY | Tóth Dániel Mátyás | V1 #include "feladat3.h" #include using namespace std;