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&, 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::picture(void)
- {
- string filename;
- cout << "Podaj nazwe pliku z obrazkiem " << endl;
- getline(cin, filename);
- fstream pfile;
- 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;
- pfile.get(character);
- this->sx = 0;
- this->sy = 0;
- int white = 0;
- int black = 0;
- int linel = 0;
- while (!(pfile.eof()))
- {
- 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;
- }
- pfile.get(character);
- }
- pfile.close();
- pfile.open(filename, ios::in);
- 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 position = 1;
- int line = 1;
- int i = 0;
- if (this->color == 0)
- {
- this->dots = new int(black);
- this->pixels = black;
- while (!(pfile.eof()))
- {
- pfile.get(character);
- if (character == 10) // znak nowej linii
- {
- line++;
- position = (line * (this->sx)) + 1;
- }
- else if (character == 32 || character == 9) //spacja lub tabulator
- {
- position++;
- }
- else // inny zapisany w pliku tekstowym znak
- {
- (this->dots)[i] = position;
- i++;
- position++;
- }
- }
- }
- if (this->color == 1)
- {
- this->dots = new int(white);
- this->pixels = white;
- while (!(pfile.eof()))
- {
- pfile.get(character);
- if (character == 10) // znak nowej linii
- {
- line++;
- position = (line * (this->sx)) + 1;
- }
- else if (character == 32 || character == 9) //spacja lub tabulator
- {
- (this->dots)[i] = position;
- i++;
- position++;
- }
- else // inny zapisany w pliku tekstowym znak
- {
- position++;
- }
- }
- }
- }
- picture::~picture(void)
- {
- delete [] this->dots;
- }
- ostream & operator<<(ostream&, const picture *pic)
- {
- cout << '\n';
- char dot, background;
- if (pic->color == 0)
- {
- background = ' ';
- dot = '#';
- }
- else
- {
- background = '#';
- dot = ' ';
- }
- int character = 0;
- int x, y;
- for (y = 0; y <= pic->sy; y++)
- {
- for (x = 1; x <= pic->sx; x++)
- {
- if (pic->dots[character] == (((pic->sx)*y) + x))
- {
- cout << dot;
- character++;
- }
- else cout << background;
- }
- cout << '\n';
- }
- return;
- }
- void main()
- {
- cout << "Uruchomiony\n";
- picture testowy;
- cout << testowy;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement