diff options
| author | Syndamia <kamen@syndamia.com> | 2024-04-04 20:11:12 +0300 |
|---|---|---|
| committer | Syndamia <kamen@syndamia.com> | 2024-04-04 20:11:12 +0300 |
| commit | a2e284b0056075e2365deaa2455be567c3b3c945 (patch) | |
| tree | 8a842d1f2ffb9f00989b39d2ab7a7ade1a858d81 /week07/Exercise3.cpp | |
| parent | 44d085f265583f0e3cbef294bbe2c8e300aaa452 (diff) | |
| download | oop-2023-solutions-a2e284b0056075e2365deaa2455be567c3b3c945.tar oop-2023-solutions-a2e284b0056075e2365deaa2455be567c3b3c945.tar.gz oop-2023-solutions-a2e284b0056075e2365deaa2455be567c3b3c945.zip | |
[w7] Added exercise descriptions and solutions
Diffstat (limited to 'week07/Exercise3.cpp')
| -rw-r--r-- | week07/Exercise3.cpp | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/week07/Exercise3.cpp b/week07/Exercise3.cpp new file mode 100644 index 0000000..3137ddd --- /dev/null +++ b/week07/Exercise3.cpp @@ -0,0 +1,77 @@ +#include "Exercise3.h" +#include <fstream> +#include <iostream> + +/* Public */ + +Thermometer::Thermometer(unsigned maxMeasurable, unsigned minMeasurable, unsigned currentTemperature) { + this->maxMeasurable = maxMeasurable; + this->minMeasurable = minMeasurable; + this->currentTemperature = currentTemperature; +} + +void Thermometer::Print() { + std::cout << maxMeasurable << " " << minMeasurable << " " << currentTemperature << std::endl; +} + +void Thermometer::SaveText(const char* fileName) { + std::ofstream outFile(fileName); + if (!outFile.is_open()) { + return; + } + + outFile << maxMeasurable << "," << minMeasurable << "," << currentTemperature; + outFile.close(); +} +void Thermometer::LoadText(const char* fileName) { + std::ifstream inFile(fileName); + if (!inFile.is_open()) { + return; + } + + inFile >> maxMeasurable; + inFile.ignore(); + inFile >> minMeasurable; + inFile.ignore(); + inFile >> currentTemperature; + inFile.close(); +} + +void Thermometer::SaveBinary(const char* fileName) { + std::ofstream outFile(fileName, std::ios::binary); + if (!outFile.is_open()) { + return; + } + + outFile.write((const char*)&maxMeasurable, sizeof(maxMeasurable)); + outFile.write((const char*)&minMeasurable, sizeof(minMeasurable)); + outFile.write((const char*)¤tTemperature, sizeof(currentTemperature)); + outFile.close(); +} +void Thermometer::LoadBinary(const char* fileName) { + std::ifstream inFile(fileName, std::ios::binary); + if (!inFile.is_open()) { + return; + } + + inFile.read((char*)&maxMeasurable, sizeof(maxMeasurable)); + inFile.read((char*)&minMeasurable, sizeof(minMeasurable)); + inFile.read((char*)¤tTemperature, sizeof(currentTemperature)); + inFile.close(); +} + +int main() { + Thermometer t1(100, 0, 20); + t1.SaveText("t1.txt"); + + Thermometer t2(50, 20, 36); + t2.SaveBinary("t2.dat"); + + Thermometer t3(5, 5, 5); + + t3.LoadText("t1.txt"); + t3.Print(); + + t3.LoadBinary("t2.dat"); + t3.Print(); +} |
