Compare commits

...

2 commits

Author SHA1 Message Date
Damimani
fb99f36335 Merge branch 'main' of https://forgejo.damimani-alex.org/damimani/2026-27-cpp-homework 2026-10-28 16:54:26 +01:00
Damimani
be585e3fde feladat3 v2 2026-10-28 16:54:20 +01:00
14 changed files with 361 additions and 37 deletions

View file

@ -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 "pch.h"
#include "CppUnitTest.h" #include "CppUnitTest.h"
#include "feladat3.h" #include "feladat3.h"

View file

@ -0,0 +1,265 @@
// IFPQYY | Tóth Dániel Mátyás | V2
#include <iostream>
#include <fstream>
#include <filesystem>
#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;
}
}
}
}

View file

@ -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);

View file

@ -0,0 +1,27 @@
// IFPQYY | Tóth Dániel Mátyás | V2
#include "feladat3.h"
#include <iostream>
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);
}

View file

@ -1 +1 @@
婆ワ~:#q/# <EFBFBD><EFBFBD>軂:#q/#

View file

@ -1 +1 @@
#:#q/婆ワ~ #:#q/軂<EFBFBD><EFBFBD>

View file

@ -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\feladat3\dataset\testin10000.dat
C:\Users\danit\Desktop\2026-27-cpp-homework\feladat3\UnitTest1\dataset\testin5.dat

View file

@ -1,4 +1,4 @@
// IFPQYY | Tóth Dániel Mátyás | V1 // IFPQYY | Tóth Dániel Mátyás | V2
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <filesystem> #include <filesystem>
@ -16,6 +16,10 @@ int GetFilenames(int argc, char* argv[],
return NOK; return NOK;
} }
if (argv[1] == nullptr)
{
return NOK;
}
cout << "Parameter allomany:\n" << argv[1] << '\n'; cout << "Parameter allomany:\n" << argv[1] << '\n';
ifstream argifs(argv[1]); ifstream argifs(argv[1]);
if (!argifs.is_open()) if (!argifs.is_open())
@ -111,7 +115,7 @@ int SortFile(char* infn, char* outfn, unsigned char* buf,
cout << "\nSTART: " << infn << " ----> " << outfn << "\n"; cout << "\nSTART: " << infn << " ----> " << outfn << "\n";
cout << "*READ\n"; cout << "*READ\n";
} }
long long sum = 0;
unsigned short n; unsigned short n;
int i = 0; int i = 0;
int j = 0; int j = 0;
@ -124,7 +128,7 @@ int SortFile(char* infn, char* outfn, unsigned char* buf,
if (*pmin > n) { if (*pmin > n) {
*pmin = n; *pmin = n;
} }
*pavg += n; sum += n;
buf[(i * 2) % (MYBUFSIZE)] = n; buf[(i * 2) % (MYBUFSIZE)] = n;
buf[((i * 2)+1) % (MYBUFSIZE)] = n >> 8; buf[((i * 2)+1) % (MYBUFSIZE)] = n >> 8;
if (test) if (test)
@ -140,9 +144,9 @@ int SortFile(char* infn, char* outfn, unsigned char* buf,
ofstream tempoutofs(filename, ios_base::binary); ofstream tempoutofs(filename, ios_base::binary);
while (j < MYBUFSIZE/2) 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)); tempoutofs.write((char*)&insert, sizeof(insert));
j+=2; j++;
} }
tempoutofs.close(); tempoutofs.close();
usedfilescnt++; usedfilescnt++;
@ -152,6 +156,8 @@ int SortFile(char* infn, char* outfn, unsigned char* buf,
readifs.close(); readifs.close();
int size = (*pn * 2) % MYBUFSIZE; int size = (*pn * 2) % MYBUFSIZE;
if (size > 0)
{
BubbleSort(buf, size); BubbleSort(buf, size);
sprintf_s(filename, "filename%d.dat", usedfilescnt); sprintf_s(filename, "filename%d.dat", usedfilescnt);
CopyStr(filenames[usedfilescnt], filename); CopyStr(filenames[usedfilescnt], filename);
@ -164,6 +170,11 @@ int SortFile(char* infn, char* outfn, unsigned char* buf,
} }
tempoutofs.close(); tempoutofs.close();
usedfilescnt++; usedfilescnt++;
}
else
{
return NOK;
}
if (test) if (test)
{ {
@ -171,31 +182,31 @@ int SortFile(char* infn, char* outfn, unsigned char* buf,
} }
i = 0; i = 0;
ifstream* streams = new ifstream[16]; ifstream streams[16];
for (j = 0; j < usedfilescnt*2; j+=2) 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; unsigned short num;
streams[j].read((char*)&num, sizeof(num)); streams[j].read((char*)&num, sizeof(num));
buf[j] = num; buf[j * 2] = num;
buf[j+1] = num >> 8; buf[(j * 2) + 1] = num >> 8;
} }
bool doneFile[16] = {false}; bool doneFile[16] = {false};
while (i < *pn) while (i < *pn)
{ {
int savedJ = 0; int savedJ = 0;
unsigned short smallest = MYMAXVALUE; 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]) if (doneFile[j])
{ {
continue; continue;
} }
int currentnum = buf[j] + buf[j + 1] * 256; int currentnum = buf[j*2] + buf[j*2 + 1] * 256;
if (currentnum < smallest) if (currentnum < smallest)
{ {
smallest = currentnum; smallest = currentnum;
savedJ = j/2; savedJ = j;
} }
} }
if (test) if (test)
@ -206,21 +217,28 @@ int SortFile(char* infn, char* outfn, unsigned char* buf,
ifstream tempinifs(filenames[savedJ], ios_base::binary); ifstream tempinifs(filenames[savedJ], ios_base::binary);
unsigned short num; unsigned short num;
if (streams[savedJ].read((char*)&num, sizeof(num))) if (streams[savedJ].read((char*)&num, sizeof(num)))
{
buf[savedJ * 2] = num;
buf[savedJ * 2 + 1] = num >> 8;
}
else
{ {
doneFile[savedJ] = true; doneFile[savedJ] = true;
} }
buf[savedJ] = num;
buf[savedJ + 1] = num >> 8;
int debugnum = buf[savedJ] + buf[savedJ] * 256;
i++; i++;
} }
for (i = 0; i < usedfilescnt; i++)
{
streams[i].close();
}
*pavg = *pavg / *pn; *pavg = sum / *pn;
if (test) if (test)
{ {
cout << "\n*DONE\n"; cout << "\n*DONE\n";
cout << "átlag:\t" << *pavg << "\tmin:\t" << *pmin << "\tmax:\t" << *pmax;
} }
return OK; 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++) for (int j = 0; j < (len / 2) - i - 1; j++)
{ {
unsigned short first = arr[j * 2] + arr[j * 2 + 1] * 256; unsigned short first = (unsigned char)arr[j * 2] + (unsigned char)arr[j * 2 + 1] * 256;
unsigned short second = arr[(j + 1) * 2] + arr[(j + 1) * 2 + 1] * 256; unsigned short second = (unsigned char)arr[(j + 1) * 2] + (unsigned char)arr[(j + 1) * 2 + 1] * 256;
if (first > second) if (first > second)
{ {
unsigned char temp = arr[j * 2]; unsigned char temp = arr[j * 2];

View file

@ -1,4 +1,4 @@
// IFPQYY | Tóth Dániel Mátyás | V1 // IFPQYY | Tóth Dániel Mátyás | V2
#pragma once #pragma once
#define MYBUFSIZE 8192 #define MYBUFSIZE 8192
#define MYMAXVALUE 65535 #define MYMAXVALUE 65535

Binary file not shown.

Binary file not shown.

View file

@ -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 "feladat3.h"
#include <iostream> #include <iostream>
using namespace std; using namespace std;