Summary Files Commits Branches @commit @diff @stats
path: root / Year 1 / Object-oriented Programming / 04-Practice-15.02.2022.cpp
blob: 443416283926ae76002075ddea236cf7f1da3004 (plain) (blame)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <iostream>
#include <fstream>
#include <cstring>

// Exercise 1

int fileSize(std::ifstream& file) {
	int currPos = file.tellg();
	file.seekg(0, std::ios::end);
	int size = file.tellg();
	file.seekg(currPos, std::ios::beg);
	return size;
}

void duplicate(const char* filePath, const char* newFile, int n) {
	std::ifstream inFile(filePath);
	int inFileSize = fileSize(inFile);
	
	std::ofstream outFile(newFile);
	for (int i = 0; i < inFileSize * n; i++) {
		outFile.put(inFile.get());

		if (inFile.tellg() == inFileSize)
			inFile.seekg(0, std::ios::beg);
	}

	outFile.close();
	inFile.close();
}

void ex1() {
	duplicate("dupeSrc.txt", "dupeDest.txt", 3);
}

// Exercise 2

struct Task {
	private:
		char* name;
		char* description;
		int points;

		void setStringProp(char*& prop, const char* newVal, int valSize) {
			delete[] prop;
			prop = new char[valSize];
			for (int i = 0; i < valSize; i++)
				prop[i] = newVal[i];
		}
	
	public:
		Task() {
			Task("", "", 0);
		}
		Task(const char* name, const char* desc, int points) {
			this->set_name(name, strlen(name));
			this->set_description(desc, strlen(desc));
			this->set_points(points);
		}
	
		~Task() {
			delete[] this->name;
			delete[] this->description;
		}

		void writeToFile(std::ofstream& file) {
			file.write(this->name, sizeof(this->name));
			file.write(this->description, sizeof(this->description));
			file.write((const char*)&this->points, sizeof(this->points));
		}

#pragma region
		void set_name(const char* newName, int newNameSize) {
			this->setStringProp(this->name, newName, newNameSize);
		}
		const char* get_name() const {
			return this->name;
		}

		void set_description(const char* newDesc, int newDescSize) {
			this->setStringProp(this->description, newDesc, newDescSize);
		}
		const char* get_description() const {
			return this->description;
		}

		void set_points(int points) {
			this->points = points;
		}
		int get_points() const {
			return this->points;
		}
#pragma endregion
};

const int EXAM_TASK_CNT = 3;

struct Exam {
	private:
		Task tasks[EXAM_TASK_CNT];
		int passingPoints;

	public:
		Exam(const Task* elems) {
			for (int i = 0; i < EXAM_TASK_CNT; i++)
				this->tasks[i] = elems[i];
		}

		void writeToFile(std::ofstream& file) {
			file.write((const char*)this, sizeof(Exam));
		}
		void writeToFile(const char* fileName) {
			std::ofstream file(fileName, std::ios::binary);
			this->writeToFile(file);
			file.close();
		}

		void readFromFile(std::ifstream& file) {
			// for (int i = 0; i < EXAM_TASK_CNT; i++)
				// file.read(this->tasks[i],sizeof(this->tasks));
		}
		void readFromFile(const char* fileName) {
			std::ifstream file(fileName, std::ios::binary);
			this->readFromFile(file);
			file.close();
		}

		void set_passingPoints(int points) {
			this->passingPoints = points;
		}
		int get_passingPoints() const {
			return this->passingPoints;
		}
	
		int getMax() {
			int acc = 0;
			for (int i = 0; i < EXAM_TASK_CNT; i++)
				acc += this->tasks[i].get_points();
			return acc;
		}
};

void writeToFile(const char* fileName, Exam& exam) {
	std::ofstream file(fileName);

}

void ex2() {
	Task t1("Wow1", "Bruh1", 5), t2("Wow2", "Bruh2", 5), t3("Wow3", "Bruh3", 5), tasks[EXAM_TASK_CNT] = { t1, t2, t3 };
	Exam ex(tasks);
	ex.writeToFile("test.txt");
}

int main() {
	ex2();
}