From 8f162cde46a34d04e3a8a655067ee5174258e249 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sat, 28 Mar 2026 16:20:14 +0200 Subject: feat(Week06): Add solutions --- Week06/Exercise01.cpp | 16 ++++++++++++++++ Week06/Exercise02.cpp | 23 +++++++++++++++++++++++ Week06/Exercise03.cpp | 22 ++++++++++++++++++++++ Week06/Exercise04.cpp | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++ Week06/Exercise05.cpp | 35 ++++++++++++++++++++++++++++++++++ Week06/Exercise06.cpp | 32 +++++++++++++++++++++++++++++++ Week06/Exercise07.cpp | 30 +++++++++++++++++++++++++++++ Week06/Exercise08.cpp | 43 ++++++++++++++++++++++++++++++++++++++++++ Week06/README.md | 3 +++ 9 files changed, 256 insertions(+) create mode 100644 Week06/Exercise01.cpp create mode 100644 Week06/Exercise02.cpp create mode 100644 Week06/Exercise03.cpp create mode 100644 Week06/Exercise04.cpp create mode 100644 Week06/Exercise05.cpp create mode 100644 Week06/Exercise06.cpp create mode 100644 Week06/Exercise07.cpp create mode 100644 Week06/Exercise08.cpp create mode 100644 Week06/README.md (limited to 'Week06') diff --git a/Week06/Exercise01.cpp b/Week06/Exercise01.cpp new file mode 100644 index 0000000..ff6153f --- /dev/null +++ b/Week06/Exercise01.cpp @@ -0,0 +1,16 @@ +#include +#include + +int main() { + std::ofstream outFile("HelloWorld.txt", std::ios::trunc); + + if (!outFile.is_open()) { + std::cout << "Couldn't open file!" << std::endl; + } + else { + const char HELLO_WORLD[] = "Hello World!"; + outFile.write(HELLO_WORLD, sizeof(HELLO_WORLD) / sizeof(*HELLO_WORLD)); + } + + return 0; +} diff --git a/Week06/Exercise02.cpp b/Week06/Exercise02.cpp new file mode 100644 index 0000000..875365f --- /dev/null +++ b/Week06/Exercise02.cpp @@ -0,0 +1,23 @@ +#include +#include + +int main() { + // Във файловите системи, имената на файлове са с ограничена дължина, + // така че и ние да работим с ограничена дължина е коректно + char fileName[1024]; + std::cin.getline(fileName, sizeof(fileName) / sizeof(*fileName)); + + std::ifstream inFile(fileName); + + if (!inFile.is_open()) { + std::cout << "Couldn't open file!" << std::endl; + } + else { + while (inFile.peek() != EOF) { + std::cout.put(inFile.get()); + } + std::cout.flush(); + } + + return 0; +} diff --git a/Week06/Exercise03.cpp b/Week06/Exercise03.cpp new file mode 100644 index 0000000..5b26705 --- /dev/null +++ b/Week06/Exercise03.cpp @@ -0,0 +1,22 @@ +#include +#include + +int main() { + // Във файловите системи, имената на файлове са с ограничена дължина, + // така че и ние да работим с ограничена дължина е коректно + char fileName[1024]; + std::cin.getline(fileName, sizeof(fileName) / sizeof(*fileName)); + + std::ofstream outFile(fileName); + + if (!outFile.is_open()) { + std::cout << "Couldn't open file!" << std::endl; + } + else { + for (char curr = std::cin.get(); curr != '@'; curr = std::cin.get()) { + outFile.put(curr); + } + } + + return 0; +} diff --git a/Week06/Exercise04.cpp b/Week06/Exercise04.cpp new file mode 100644 index 0000000..004dacc --- /dev/null +++ b/Week06/Exercise04.cpp @@ -0,0 +1,52 @@ +#include +#include + +void printFileBetweenPositions(std::ifstream& inFile, int start, int end) { + if (start >= end) + return; + + int original = inFile.tellg(); + inFile.seekg(start, std::ios::beg); + + while (inFile.tellg() <= end) { + std::cout.put(inFile.get()); + } + + inFile.seekg(original, std::ios::beg); +} + +int main() { + // Във файловите системи, имената на файлове са с ограничена дължина, + // така че и ние да работим с ограничена дължина е коректно + char fileName[1024]; + std::cin.getline(fileName, sizeof(fileName) / sizeof(*fileName)); + + std::ifstream inFile(fileName); + + if (!inFile.is_open()) { + std::cout << "Couldn't open file!" << std::endl; + } + else { + int beg = inFile.tellg(); + + // seekg(0, std::ios::end) ще ни заведе в края на файла, + // след последния байт, все едно сме отишли при терминиращата + // нула в низ. Затова трябва да отидем с една позиция назад, + // за да стигнем последния (съществуващ) байт във файла. + inFile.seekg(-1, std::ios::end); + int endOfLine = inFile.tellg(); + + while (inFile.tellg() != beg) { + if (inFile.peek() == '\n') { + printFileBetweenPositions(inFile, (int)inFile.tellg() + 1, endOfLine); + endOfLine = inFile.tellg(); + } + inFile.seekg(-1, std::ios::cur); + } + + printFileBetweenPositions(inFile, inFile.tellg(), endOfLine); + std::cout.flush(); + } + + return 0; +} diff --git a/Week06/Exercise05.cpp b/Week06/Exercise05.cpp new file mode 100644 index 0000000..0b4d434 --- /dev/null +++ b/Week06/Exercise05.cpp @@ -0,0 +1,35 @@ +#include +#include + +int main() { + char fileName[1024]; + std::cin.getline(fileName, sizeof(fileName) / sizeof(*fileName)); + + unsigned N; + std::cin >> N; + + double* numbers = new double[N]; + for (unsigned i = 0; i < N; ++i) + std::cin >> numbers[i]; + + std::ofstream outFile(fileName, std::ios::binary | std::ios::trunc); + + if (!outFile.is_open()) { + std::cout << "Couldn't open file!" << std::endl; + } + else { + for (unsigned i = 0; i < N; ++i) { + // На практика, << конвертира подадената стойност до низ и след + // това я подава на write. + // Като говорим за двоични файлове, не говорим за низове и низови + // репрезентации, т.е. искаме да запишем байтовете в паметта + // които съставят числата (double е 64-битов, т.е. 8 байтов). + // Припомняме си, че char типа също е 8 бита, 1 байт, тоест тук + // трябва да мислим за char* като все едно byte*, не като низ. + outFile.write((const char*)(numbers + i), sizeof(*numbers)); + } + } + + delete[] numbers; + return 0; +} diff --git a/Week06/Exercise06.cpp b/Week06/Exercise06.cpp new file mode 100644 index 0000000..29a6128 --- /dev/null +++ b/Week06/Exercise06.cpp @@ -0,0 +1,32 @@ +#include +#include + +int main() { + char fileName[1024]; + std::cin.getline(fileName, sizeof(fileName) / sizeof(*fileName)); + + std::ifstream inFile(fileName, std::ios::binary); + + if (!inFile.is_open()) { + std::cout << "Couldn't open file!" << std::endl; + } + else { + inFile.seekg(0, std::ios::end); + // Като сме отишли в края на файла с горния seekg, + // ако вземем tellg, ще получим общия размер на файла + // в байтове. Помним че файла се представя все едно е + // един голям масив и g/p са индекси. + unsigned N = inFile.tellg() / sizeof(double); + inFile.seekg(0, std::ios::beg); + + for (unsigned i = 0; i < N; ++i) { + double buffer; + // Мислим за този char* не като низ ами като byte* + inFile.read((char*)&buffer, sizeof(buffer)); + std::cout << buffer << ' '; + } + std::cout << std::endl; + } + + return 0; +} diff --git a/Week06/Exercise07.cpp b/Week06/Exercise07.cpp new file mode 100644 index 0000000..6e3bd35 --- /dev/null +++ b/Week06/Exercise07.cpp @@ -0,0 +1,30 @@ +#include +#include + +struct Smartphone { + short OSVersion; + unsigned long long serialNumber; + float SAR; + char hardwareRevision; +}; + +int main() { + unsigned N; + std::cin >> N; + + std::ofstream outFile("smartphones.dat", std::ios::binary | std::ios::trunc); + + if (!outFile.is_open()) { + std::cout << "Couldn't open file!" << std::endl; + } + else { + for (unsigned i = 0; i < N; ++i) { + Smartphone buffer; + std::cin >> buffer.OSVersion >> buffer.serialNumber >> buffer.SAR >> buffer.hardwareRevision; + // Мислим за този char* не като низ ами като byte* + outFile.write((const char*)&buffer, sizeof(buffer)); + } + } + + return 0; +} diff --git a/Week06/Exercise08.cpp b/Week06/Exercise08.cpp new file mode 100644 index 0000000..cfaff5c --- /dev/null +++ b/Week06/Exercise08.cpp @@ -0,0 +1,43 @@ +#include +#include + +struct Smartphone { + short OSVersion; + unsigned long serialNumber; + float SAR; + char hardwareRevision; +}; + +int main() { + char fileName[1024]; + std::cin.getline(fileName, sizeof(fileName) / sizeof(*fileName)); + + std::ifstream inFile(fileName, std::ios::binary); + if (!inFile.is_open()) { + std::cout << "Couldn't open file!" << std::endl; + } + else { + inFile.seekg(0, std::ios::end); + // Като сме отишли в края на файла с горния seekg, + // ако вземем tellg, ще получим общия размер на файла + // в байтове. Помним че файла се представя все едно е + // един голям масив и g/p са индекси. + unsigned N = inFile.tellg() / sizeof(Smartphone); + inFile.seekg(0, std::ios::beg); + + for (unsigned i = 0; i < N; ++i) { + Smartphone buffer; + // Мислим за този char* не като низ ами като byte* + inFile.read((char*)&buffer, sizeof(buffer)); + + std::cout << (i+1) << '/' << N << std::endl + << "OS version: " << buffer.OSVersion << std::endl + << "Serial number: " << buffer.serialNumber << std::endl + << "SAR: " << buffer.SAR << std::endl + << "Hardware revision: " << buffer.hardwareRevision << std::endl; + } + } + + return 0; +} + diff --git a/Week06/README.md b/Week06/README.md new file mode 100644 index 0000000..0d05aa3 --- /dev/null +++ b/Week06/README.md @@ -0,0 +1,3 @@ +# Задачи + + -- cgit v1.3.1-23-gd82d