Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "picture.h"
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <conio.h>
- using namespace std;
- class picture
- {
- public:
- picture(void);
- ~picture(void);
- friend ostream & operator<<(ostream &out, const picture &pic);
- private:
- int sx, sy, color, pixels; // rozmiary w pikselach poziom i pion, kolor ( tylko jedna instancja, nie ma aż takich strat pamięci ), liczba pokolorowanych pikseli
- int *dots; // wskaźnik na tablicę pikseli
- //tablica ta zawiera w sobie numery pozycji pokolorwanych pikseli, licząc od lewego górnego rogu idąc jak normalnie w prawo i w dół
- picture* next;
- };
- picture::picture(void)
- {
- string filename;
- fstream pfile;
- {
- cout << "Podaj nazwe pliku z obrazkiem " << endl;
- getline(cin, filename);
- pfile.open(filename, ios::in);
- while (pfile.good() != true)
- {
- cout << "Podano bledna nazwe pliku. Prosze podac poprawna nazwe pliku " << endl;
- getline(cin, filename);
- pfile.open(filename, ios::in);
- }
- }
- // otwarcie pliku plik pfile
- char character;
- this->sx = 0;
- this->sy = 0;
- int white = 0;
- int black = 0;
- int linel = 0;
- while (!(pfile.eof()))
- {
- pfile.get(character);
- if (character != 10)
- {
- if (character == 32 || character == 9)white++;
- else black++;
- linel++;
- }
- else
- {
- if (linel > this->sx) this->sx = linel;
- this->sy = this->sy + 1;
- linel = 0;
- }
- }
- // pętla sprawdzania pliku - kolor dominujący, wysokość i szerokość
- {
- pfile.close();
- pfile.open(filename, ios::in);
- }
- //reset pliku - powrót na początek zamknięciem i otworzeniem
- if (white > black) this->color = 0; // 0 jesli bialy wystepuje czesciej niz czary, to czarny jest tuszem
- else this->color = 1; // w przeciwnym wypadku bialy tusz, a wartosc 1
- int positionor = 1;
- int lineor = 0;
- int ior = 0;
- int *position;
- int *line;
- int *i;
- position = &positionor;
- line = &lineor;
- i = &ior;
- if (this->color == 0)
- {
- this->dots = new int(black);
- this->pixels = black;
- cout << this->sx << ' ' << this->sy << ' ' << this->pixels << ' ' << this->color << '\n';
- while (!(pfile.eof()))
- {
- pfile.get(character);
- if (character == '\n')
- {
- *line = *line + 1;
- *position = *line * this->sx + 1;
- }
- else if (character == ' ' || character == ' ')
- {
- (*position)++;
- }
- else
- {
- this->dots[(*i)] = *position;
- (*i)++;
- (*position)++;
- }
- }
- }
- else if (this->color == 1)
- {
- this->dots = new int(white);
- this->pixels = white;
- cout << this->sx << ' ' << this->sy << ' ' << this->pixels << ' ' << this->color << '\n';
- while (!(pfile.eof()))
- {
- pfile.get(character);
- if (character == '\n')
- {
- *line = *line + 1;
- *position = *line * this->sx + 1;
- }
- else if (character == ' ' || character == ' ')
- {
- this->dots[(*i)] = *position;
- (*i)++;
- (*position)++;
- }
- else
- {
- (*position)++;
- }
- }
- }
- }
- picture::~picture(void)
- {
- delete this->dots;
- }
- ostream & operator<<(ostream &out, const picture & pic)
- {
- char background;
- char dot;
- if (pic.color == 0)
- {
- background = ' ';
- dot = '#';
- }
- else
- {
- background = '#';
- dot = ' ';
- }
- int x, y, i, position;
- i = 0;
- for (y = 0; y <= pic.sy; y++)
- {
- for (x = 1; x <= pic.sx; x++)
- {
- position = (y * (pic.sx) + x);
- if (pic.dots[i] == position)
- {
- out << dot;
- i++;
- }
- else out << background;
- }
- out << '\n';
- }
- return out;
- }
- void main()
- {
- cout << "Uruchomiony\n";
- picture testowy;
- cout << testowy;
- int j = 5;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement