diff --git a/Sem1/main.cpp b/Sem1/main.cpp index 62cee07..7b340c4 100644 --- a/Sem1/main.cpp +++ b/Sem1/main.cpp @@ -35,7 +35,7 @@ public: } } - void freemem() + void freemem() { free(data); } @@ -66,24 +66,26 @@ public: int get(uint x, uint y) { - + // TODO реализовать логику return data[mx * (y) + (x)]; } void set(uint x, uint y, uint color) { - if (x >= mx) { + if (x >= mx) + { throw(x); } - if (y >= my) { + if (y >= my) + { throw(y); } data[mx * (y) + (x)] = color; // TODO реализовать логику } - void copy(Image* original) + void copy(Image *original) { freemem(); mx = original->getMx(); @@ -99,7 +101,7 @@ public: } // Returns true if equal - bool compare(Image* original) + bool compare(Image *original) { if (original->getMx() == mx && original->getMy() == my) { @@ -107,7 +109,8 @@ public: { for (uint x = 0; x < mx; x++) { - if (data[mx * y + x] != original->get(x, y)){ + if (data[mx * y + x] != original->get(x, y)) + { cout << "Not equal: x = " << x << "; y = " << y << "\n"; return false; } @@ -123,58 +126,90 @@ public: } } - void draw_horizontal(uint y, uint color) { - if (y >= my) { + void draw_horizontal(uint y, uint color) + { + if (y >= my) + { throw(y); } - for (uint x = 0; x < mx; x++) { - data[mx*y + x] = color; + for (uint x = 0; x < mx; x++) + { + data[mx * y + x] = color; } } - void draw_vertical(uint x, uint color) { - if (x >= mx) { + void draw_vertical(uint x, uint color) + { + if (x >= mx) + { throw(x); } - for (uint y = 0; y < my; y++) { - data[mx*y + x] = color; + for (uint y = 0; y < my; y++) + { + data[mx * y + x] = color; } } - void save_to_file(string filename) { + void save_to_file(string filename) + { ofstream writefile; writefile.open(filename); - if (writefile.is_open()){ - writefile << mx << "\t" << my << "\n"; + if (writefile.is_open()) + { + writefile << mx << "\n" + << my << "\n"; for (uint y = 0; y < my; y++) { for (uint x = 0; x < mx; x++) { - writefile << data[mx * y + x] << "\t"; + writefile << data[mx * y + x] << "\n"; } - writefile << "\n"; } + writefile << "\n"; writefile.close(); - } else + } + else { cerr << "saving to file failed\n"; } - } - void read_from_file(string filename) { + void read_from_file(string filename) + { string line; ifstream readfile; readfile.open(filename); - if(readfile.is_open()) { + if (readfile.is_open()) + { // TODO + try + { + getline(readfile, line); + mx = stoi(line); - } else { + getline(readfile, line); + my = stoi(line); + + for (uint y = 0; y < my; y++) + { + for (uint x = 0; x < mx; x++) + { + getline(readfile, line); + data[mx * y + x] = stoi(line); + } + } + } + catch (const exception &e) + { + cerr << e.what() << '\n'; + } + } + else + { cerr << "reading file failed"; } readfile.close(); } - }; Image a(30, 10); @@ -187,13 +222,15 @@ int main() cout << "Image b \n"; b.show(); b.copy(&a); - if (b.compare(&a) == true) { + if (b.compare(&a) == true) + { cout << "a == b after copy\n"; - } else + } + else { cout << "a != b after copy!\n"; } - + b.set(4, 3, 20); for (int i = 5; i < 9; i++) { @@ -207,5 +244,8 @@ int main() cout << "Image b, with a horizontal line \n"; b.show(); b.save_to_file("image"); + a.read_from_file("image"); + cout << "Image a, read from file \n"; + a.show(); return 0; } \ No newline at end of file